I have an Excel worksheet which has data I'd like to import into Access. Problem is that the data starts at around row 900 with the top part being filled with statistics on the data below.
So... I wrote a nifty little Excel macro to parse through the file until it gets to the top left cell of the data, puts the cell address into a variable then finds the bottom right cell of the data and puts its cell address into another variable. I've message boxed out the results and it all works fine.
The problem is when I try using the same code through an Access module. Basically this is what I want my module to do.
1. Create an instance of Excel
2. Run the aforementioned code putting the top left and bottom right cell addresses into two variables
3. Use the TransferSpreadSheet function to import the data into Access using the two variables as the range to import
Here's the code:
Sub GetData()
On Error Resume Next
Dim oXL As Object
Dim Range1, Range2 As String
Set oXL = CreateObject("Excel.Application"
'Create an instance of Excel
With oXL.Application
.Visible = False
.Workbooks.Open "licences.xls"
For i = 1 To 65536 'Find the top left cell
.Range("A" & i).Select
If .ActiveCell = "Microchip Number" Then
Range1 = .ActiveCell.Address 'Put cell address into a variable
ActiveCell.End(xlToRight).Select 'Go to the far right of the data
ActiveCell.End(xlDown).Select 'Go to the bottom of the data
Range2 = .ActiveCell.Address 'Put the bottom right cell address into another variable
Exit For
End If
Next i
.Activeworkbook.Close
.Quit
End With
Set oXL = Nothing
MsgBox (Range1 & " " & Range2) 'Output the result
End Sub
It doesn't work like in Excel. Range2 takes on the same address as Range1. E.g. I get the output: $A$891 $A$891 instead of $A$891 $Y$5200. Why if it works when I run the code as a macro in Excel doesn't it work when I'm running the same code through Access?
So... I wrote a nifty little Excel macro to parse through the file until it gets to the top left cell of the data, puts the cell address into a variable then finds the bottom right cell of the data and puts its cell address into another variable. I've message boxed out the results and it all works fine.
The problem is when I try using the same code through an Access module. Basically this is what I want my module to do.
1. Create an instance of Excel
2. Run the aforementioned code putting the top left and bottom right cell addresses into two variables
3. Use the TransferSpreadSheet function to import the data into Access using the two variables as the range to import
Here's the code:
Sub GetData()
On Error Resume Next
Dim oXL As Object
Dim Range1, Range2 As String
Set oXL = CreateObject("Excel.Application"
With oXL.Application
.Visible = False
.Workbooks.Open "licences.xls"
For i = 1 To 65536 'Find the top left cell
.Range("A" & i).Select
If .ActiveCell = "Microchip Number" Then
Range1 = .ActiveCell.Address 'Put cell address into a variable
ActiveCell.End(xlToRight).Select 'Go to the far right of the data
ActiveCell.End(xlDown).Select 'Go to the bottom of the data
Range2 = .ActiveCell.Address 'Put the bottom right cell address into another variable
Exit For
End If
Next i
.Activeworkbook.Close
.Quit
End With
Set oXL = Nothing
MsgBox (Range1 & " " & Range2) 'Output the result
End Sub
It doesn't work like in Excel. Range2 takes on the same address as Range1. E.g. I get the output: $A$891 $A$891 instead of $A$891 $Y$5200. Why if it works when I run the code as a macro in Excel doesn't it work when I'm running the same code through Access?