Hi all - having a bit of problem with this code. It works in one of the databases that I have but not the one I'm currently working with...I've compared references and they're both the same:
When trying to get the last column of data from each worksheet in my Excel file, it's giving me the last column as "c" when it should be "AH" or "AI,' depending on which worksheet it's on. On one worksheet, it's giving me "]" and there is no "]" anywhere in the worksheet. The lastdatarow number is correct - just the column that isn't right.
Here's the code I'm using (which I got from here-many thanks to the contributor)
Not sure where to go from here and hoping that the experts can help me out.
Also, I understand what the
does, but what does the + 64 do?
When trying to get the last column of data from each worksheet in my Excel file, it's giving me the last column as "c" when it should be "AH" or "AI,' depending on which worksheet it's on. On one worksheet, it's giving me "]" and there is no "]" anywhere in the worksheet. The lastdatarow number is correct - just the column that isn't right.
Here's the code I'm using (which I got from here-many thanks to the contributor)
Code:
Dim excelapp As Excel.Application
Dim excelbook As Excel.Workbook
Dim excelsheet As Excel.Worksheet
Dim intNoOfSheets As Integer, intCounter As Integer
Dim strFilePath As String, strLastDataColumn As String
Dim strLastDataRow As String, strLastDataCell As String
Set cdl = New CommonDlg
cdl.InitDir = CurrentProject.Path
cdl.FileName = "*"
cdl.Filter = "Excel Files (*.xls)|*.xls|All Files (*.*)|*.*"
cdl.CancelError = True
cdl.OpenFlags = cdlOFNAllowMultiselect Or cdlOFNNoChangeDir
cdl.ShowOpen
strfilename = cdl.FileName
Set excelbook = Excel.Application.Workbooks.Open(strfilename)
intNoOfSheets = excelbook.Worksheets.Count
For intCounter = 1 To intNoOfSheets
excelbook.Worksheets(intCounter).Activate
[B]strLastDataColumn = Chr(Selection.SpecialCells(xlLastCell).Column + 64)[/B]
strLastDataRow = Selection.SpecialCells(xlLastCell).Row
If strLastDataRow = 1 Then
GoTo Line1
End If
strLastDataCell = strLastDataColumn & strLastDataRow
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel11, "Cart_Data", strfilename, True, _
excelbook.Worksheets(intCounter).Name & "!A1:" & strLastDataCell
Line1:
Next
excelbook.Close
Set excelbook = Nothing
Excel.Application.Quit
Set excelapp = Nothing
End Function
Also, I understand what the
Code:
Selection.SpecialCells(xlLastCell).Column
Comment