How to use Excel worksheet functions in VBScript?

后端 未结 1 1129
抹茶落季
抹茶落季 2021-01-07 08:52

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

相关标签:
1条回答
  • 2021-01-07 09:18

    A few mistakes:

    1. WorksheetFunction is a method of the Excel.Application object, not Worksheet.
    2. 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
    
    0 讨论(0)
提交回复
热议问题