473,503 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access - Excel format

1 New Member
I am very new to all this technical stuff, but I am trying to format an excel spreadsheet via access vba but getting an error. Can some one please advice?
My code is:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command3_Click()
  2. DoCmd.Hourglass True
  3. DoCmd.SetWarnings False
  4.     'On Error GoTo ErrHandler:
  5. ' *************** RUN EUR RI DATA RECON ********************
  6.  
  7.     Dim objXL As Object
  8.     Dim strWin As String
  9.     Dim sglRep As Single
  10.     Dim intPos As Integer
  11.     'Set newName = Sheets("Data")
  12.     'newName.Visible
  13.  
  14.     strFile = "P:\test.xls"
  15.  
  16.     If strFile = "" Then Exit Sub
  17.  
  18.     'Saves changes and closes the speradsheet
  19.     Set objXL = GetObject(strFile)
  20.     objXL.Close savechanges:=True
  21.  
  22.     'Transfers the data to the Excel tabs depending on the file being updated
  23.     'DoCmd.TransferSpreadsheet acExport, 8, "tbl_2004 UW Data", strFile, True, "2004 UWY"
  24.     DoCmd.TransferSpreadsheet acExport, 8, "test", strFile, True, "test"
  25.  
  26.  
  27.  
  28.     Set objXL = GetObject(strFile)
  29.     objXL.Application.Visible = True
  30.  
  31.     strWin = strFile
  32.     intPos = 1
  33.  
  34.     'Finds name of window
  35.     Do Until intPos = 0
  36.  
  37.         intPos = InStr(strWin, "\")
  38.         strWin = Right(strWin, Len(strWin) - intPos)
  39.  
  40.     Loop
  41.  
  42.     'Makes spreadsheet current and unhidden
  43.     objXL.Parent.Windows(strWin).Activate
  44.     objXL.Parent.Windows(strWin).Visible = True
  45.  
  46.     'Sets file to read only
  47.     'If strFile = "P:\AgedDebtors\CashDataMaster.xls" Then
  48.      '   objXL.Application.DisplayAlerts = False
  49.       '  objXL.ChangeFileAccess Mode:=xlReadOnly
  50.         'Sheets("Data").Select
  51.         'Cells.Select
  52.     'Cells.EntireColumn.AutoFit
  53.     'Range("A1").Select
  54.     'Range("A1:R48").Sort Key1:=Range("O2"), Order1:=xlDescending, Header:= _
  55.      '   xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
  56.       '  DataOption1:=xlSortNormal
  57.     'End If
  58.  
  59.     strFile = ""
  60.     strWin = ""
  61.     sglRep = 0
  62.     DoCmd.Hourglass False
  63.     DoCmd.SetWarnings True
  64.  
  65.     Range("a:c").select
  66.  
  67.     'MsgBox "Run Complete"
  68.  
  69. 'ErrHandler:
  70.     'MsgBox (Err.Source & " - " & Err.Description)
  71. End Sub

Thanks in advance.
Oct 9 '08 #1
2 1684
puppydogbuddy
1,923 Recognized Expert Top Contributor
To use Linq's expression, Carnac the Magnificent is not available at this site, and we can't read your mind....LOL! Please provide more details. What is the error you are getting? Could the error be related to some of the code you have commented out?
Oct 9 '08 #2
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi. I have added code tags before and after your code section, which makes it much easier to refer to specific lines.

As PDog has said, you really must tell us what is going wrong and what kind of error message you are getting - we should not be guessing.

As an observation, I can say for certain that the commented out code at lines 50-54 will not function reliably in Access, as you are not using the automation object variable you have defined (objXL) to communicate with the Excel instance. Referring to ranges and so on works within Excel itself without qualification by the worksheet object, but not when running as automation code from Access.

ALL references to ranges and so on will need to be qualified with the current object and worksheet variables. For example,

ObjXL.ActiveSheet.Range("A1").Select

and not

Range("A1").Select

As an aside, it is much more efficient not to select ranges and cells before setting them to specific values or formats; refer directly to the range or cell object to set its properties.

Many of these problems arise when using the Macro Recorder in Excel to record actions. The code recorded is neither truly portable when used in an automation context nor particularly efficient.

-Stewart
Oct 9 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
1144
by: Bones | last post by:
How can I import an Excel 97 spread sheet to Access 2.0? I'm new to Access. Bones
11
4025
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit excel. My problem is that if I try and do it...
1
3306
by: com | last post by:
Extreme Web Reports 2005 - Soft30.com The wizard scans the specified MS Access database and records information such as report names, parameters and subqueries. ......
15
2393
by: sparks | last post by:
We get more and more data done in excel and then they want it imported into access. The data is just stupid....values of 1 to 5 we get a lot of 0's ok that alright but 1-jan ? we get colums...
9
3923
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result...
8
9619
by: shenkel55 | last post by:
I'm using Access and Excel 2003. Using either the import wizard or code, I have the same problem. This problem only happens with Excel files automatically generated by Corp IT. If I try to do an...
1
3043
by: accessvbanewbie | last post by:
I would like to export a recordset from access to excel but after each record is exported I want to insert a new row. The first recordset does this ok. However, the second recordset onwards does not...
1
6293
by: JFKJr | last post by:
Hello everyone, the following Access VBA code opens an excel file and creates textboxes in a given range of cells dynamically. The code attaches "MouseUP" and "Exit" events to the textboxes (using...
1
3714
by: Joe Humburg | last post by:
Hi everyone, Looking for some help or ideas, on the folloiwng problem. Have an Access 2003 application that creates an Excel file containing data from an Access parameter query. This is...
15
5225
by: OzNet | last post by:
I have a query (with calculated fields) in Access (2007) and the data changes depending on the dates and staff person selected. I need to produce a series of graphs based on the data in this query...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.