vb 调用excel时不能取得类worksheetfunction 的counta属性
Dim xlApp As excel.ApplicationDim xlBook As excel.Workbook
Dim xlSheet As excel.Worksheet
Dim count1 As Integer, count2 As Integer, m As Integer
Dim cjname(300) As String, mcname(300, 300) As String, pzname(300, 300) As String
m = 0
' Set xlApp = CreateObject("Excel.Application") '创建并返回一个对 ActiveX 对象的引用
Set xlApp = New excel.Application
xlApp.Visible = True '打开新的表格
xlApp.DisplayAlerts = False '关闭时不提示保存
Set xlBook = xlApp.Workbooks.Open("E:\数据库\二部短线数据\短线交易指令数据表\插件名称.xlsx")
Set xlSheet = xlBook.Sheets(2)
count2 = Application.WorksheetFunction.CountA(xlSheet.Range("A:A").EntireColumn)
上面第一次运行不会出错,但第二次运行就出错了 错误是:不能取得类worksheetfunction 的counta属性
For i = 1 To count2 - 1
If xlSheet.Cells(i, 1) <> xlSheet.Cells(i + 1, 1) Then
n = 1
m = m + 1
cjname(m) = Trim(Cells(i + 1, 1))
mcname(m, n) = Trim(Cells(i + 1, 2)) '/////////////// 保存名称注释
pzname(m, n) = Trim(Cells(i + 1, 3)) '/////////////// 保存品种周期
Else
n = n + 1
mcname(m, n) = Trim(Cells(i + 1, 2)) '/////////////// 保存名称注释
pzname(m, n) = Trim(Cells(i + 1, 3)) '////////////// 保存品种周期
End If
Next i
pgbar1.Max = count2 * 2 - 2
pgbar1.Value = 0
xlBook.Close
xlApp.Quit
xlApp.DisplayAlerts = True '关闭时提示保存
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing 问题出在CreateObject上 楼主这个代码里貌似没有调用CreateObject吧,注释掉了.
看楼主代码,是在第二个SHEET里取数据的,但这里有一点要注意,就是获得了SHEET对象后,还需要激活它.
在Set xlSheet = xlBook.Sheets(2)这一句后面加上:
xlSheet.select
试试看吧.
有问题再跟进.
补充:VB , 基础类