I'm trying to count the number of non-empty cells in an Excel spreadsheet from MS-Access VBA.
The idea is that I click a button on a form in MS-Access and the number of non-empty cells are stored as string.
The Excel workbook file is called "MattExcelFile.xls"
, the sheet within that workbook is called "Sheet1"
and the range I want to use Excel's COUNTA
function on is "C1:C500"
.
Here's the code I've pieced together so far:
Option Compare Database
Sub ImportDataFromRange()
Dim xlFilePath As String
Dim rowVariable As String
xlFilePath = "C:\Users\Matt\Desktop\MattExcelFile.xls"
rowVariable = Excel.Application.WorksheetFunction.CountA(Workbooks(xlFilePath).Sheets("Sheet1").Range("C1:C500"))
Debug.Print rowVariable
End Sub
Private Sub Command0_Click()
ImportDataFromRange
End Sub
And here's the error message I receive when running this code, highlighting the rowVariable = ...
line:
Run-time error '9':
Subscript out of range