vb怎样调用Excel中的函数

如题所述


'用 Application.WorksheetFunction.****,
'举个例子如下
'需要引用Microsoft Excel 12.0 Object Library,版本可能不同

Private Sub Command2_Click() 
Dim arr() As String
Dim b As Integer
Dim xlsApp As Excel.Application
Dim xlsWorkbook As Excel.Workbook
Dim xlssheet As Excel.Worksheet 
Set xlsApp = CreateObject("Excel.Application")
Set xlsWorkbook = xlsApp.Workbooks.Open("D:\test.xlsx") '按路径打开对应excel
Set xlssheet = xlsWorkbook.Worksheets(1) 
b = Application.WorksheetFunction.CountA(Range("A:A")) '调用excel现有函数countA
Print "A列共有行数:" & b

Set xlssheet = Nothing 
Set xlsWorkbook = Nothing 
Set xlsApp = Nothing 
End Sub

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-04-16
工程—引用:Microsoft Excel XX.X Object Library
dim ExcelFunction As New Excel.Application
ExcelFunction.WorksheetFunction.Excel中的函数
Set ExcelFunction = Nothing
相似回答