I need to count the number of active cells in column A
of Excel.
I can achieve this easily using \'worksheetfunction.countA\' in Excel VBA, but unable to
A few mistakes:
WorksheetFunction
is a method of the Excel.Application
object, not Worksheet
.Range
can't be used by itself, it's a method of a Worksheet
object.Here's code that will work:
Dim objXl
Dim objWorkbook
Dim objSheet
Dim iActiveCells
Set objXl = CreateObject("Excel.Application")
Set objWorkbook = objXl.Workbooks.open("C:\Temp\test2.xlsx") 'change filename
Set objSheet = objWorkbook.Worksheets(1)
objXl.Visible = True
With objSheet
.Cells(1, 1).Select
iActiveCells = objXl.WorksheetFunction.CountA(.Range("A:A"))
End With
MsgBox iActiveCells