PC Review


Reply
Thread Tools Rate Thread

Controlling Excel from Access VB

 
 
CDM
Guest
Posts: n/a
 
      27th Jan 2010
My company software generates spreadsheet reports that I import into Access.
Before importing, I have to manually format the report by deleting the first
header row and changing the headings in the second row, etc. I would like to
automate this process by doing it with VB from within Access. However, when
selecting the first row I get an error message that says "Select method of
range class failed". What am I doing wrong? Here's my code:

Dim objWkBook as object
Dim objWkSheet as object
Set objWkBook = Getobject("Path\MyFile.xls")
Set objWkSheet = objWkbook.sheets(1)
objWkSheet.Range("A1:AZ1").Select

Thanks for any help you can offer.
 
Reply With Quote
 
 
 
 
GeoffG
Guest
Posts: n/a
 
      28th Jan 2010
I would tackle it like this:

Sub ChangeExcelHeadings()

Const strcWkBkPathName As String = _
"C:\CDM.xls"

Dim objXL As Excel.Application
Dim objWkBook As Excel.Workbook
Dim objWkSheet As Excel.Worksheet
Dim objRNG As Excel.Range

Dim I As Integer
Dim astrHeadings() As String

On Error GoTo Error_GetExcelData

' Open Excel:
On Error Resume Next
Set objXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear
Set objXL = CreateObject("Excel.Application")
End If
objXL.Visible = True

' Open Workbook:
Set objWkBook = objXL.Workbooks.Open(strcWkBkPathName)
Set objWkSheet = objWkBook.Worksheets(1)

' Delete cells A1:AZ1:
Set objRNG = objWkSheet.Range("A1:AZ1")
objRNG.Delete xlShiftUp

' Reset range:
Set objRNG = objWkSheet.Range("A1:AZ1")

' Store sample new row headings in array
' for demo purposes:
ReDim astrHeadings(1 To objRNG.Cells.Count)
For I = LBound(astrHeadings) To UBound(astrHeadings)
astrHeadings(I) = "NewHeading" & CStr(I)
Next

' Write new row headings:
For I = 1 To objRNG.Cells.Count
objRNG.Cells(1, I) = astrHeadings(I)
Next

' Adjust column widths:
objRNG.Columns.AutoFit

' Clean up:
Set objRNG = Nothing
Set objWkSheet = Nothing
If Not objWkBook Is Nothing Then
objWkBook.Close SaveChanges:=True
Set objWkBook = Nothing
End If
objXL.Quit

Exit_GetExcelData:

Exit Sub

Error_GetExcelData:

MsgBox "Error No: " & Err.Number _
& vbNewLine _
& Err.Description, _
vbExclamation + vbOKOnly, _
"Error Information"
Resume Exit_GetExcelData:

End Sub


Geoff






"CDM" <(E-Mail Removed)> wrote in message
news:27929768-168D-41CB-996E-(E-Mail Removed)...
> My company software generates spreadsheet reports that I
> import into Access.
> Before importing, I have to manually format the report by
> deleting the first
> header row and changing the headings in the second row,
> etc. I would like to
> automate this process by doing it with VB from within
> Access. However, when
> selecting the first row I get an error message that says
> "Select method of
> range class failed". What am I doing wrong? Here's my
> code:
>
> Dim objWkBook as object
> Dim objWkSheet as object
> Set objWkBook = Getobject("Path\MyFile.xls")
> Set objWkSheet = objWkbook.sheets(1)
> objWkSheet.Range("A1:AZ1").Select
>
> Thanks for any help you can offer.


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Controlling Excel From Access Keith Wilby Microsoft Excel Programming 2 12th Jun 2008 04:42 PM
Controlling Access from Excel =?Utf-8?B?TmVpbHk=?= Microsoft Excel Programming 6 12th Nov 2005 08:20 PM
Controlling Excel from Access Andy Microsoft Access VBA Modules 1 25th Nov 2004 11:03 AM
Controlling MS Excel 97 from Access 97 (Using VBA) =?Utf-8?B?c3F1aXJlbGxl?= Microsoft Access External Data 1 28th Oct 2004 01:39 PM
Controlling MS Excel 97 from Access 97 (Using VBA) =?Utf-8?B?c3F1aXJlbGxl?= Microsoft Access External Data 0 28th Oct 2004 04:41 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:00 AM.