我在 Excel 2010 中遇到了 VBA 加载项问题。
我创建了一些代码来解析我的 excel 数据。我把它做成了一个插件。
但是,当我加载加载项并运行时,会发生错误。
错误消息说:runtime error 91 object variable or With block variable not set
错误指向rowSize = ActiveSheet.Rows.Count
。
有谁知道如何解决这个错误?
这是代码,
Private Sub Workbook_Open()
Dim counter As Long
Dim rowSize As Long
Dim userId As String
Dim answers As String
Dim vals As String
Dim i As Integer
rowSize = ActiveSheet.Rows.Count
counter = 1
'Create Column
ActiveSheet.Cells(1, 7).Value = "Country"
ActiveSheet.Cells(1, 8).Value = "State"
ActiveSheet.Cells(1, 9).Value = "Age"
ActiveSheet.Cells(1, 7).Font.Bold = True
ActiveSheet.Cells(1, 8).Font.Bold = True
ActiveSheet.Cells(1, 9).Font.Bold = True
ActiveSheet.Cells(1, 7).HorizontalAlignment = xlCenter
ActiveSheet.Cells(1, 8).HorizontalAlignment = xlCenter
ActiveSheet.Cells(1, 9).HorizontalAlignment = xlCenter
ActiveSheet.Cells(1, 7).Borders().LineStyle = xlContinuous
ActiveSheet.Cells(1, 8).Borders().LineStyle = xlContinuous
ActiveSheet.Cells(1, 9).Borders().LineStyle = xlContinuous
'Set Value
Do While counter < rowSize
If ActiveSheet.Cells(counter, 1).Value = Null Then Exit Do
If ActiveSheet.Cells(counter, 4).Value = "3" Then
userId = ActiveSheet.Cells(counter, 2).Value
vals = ActiveSheet.Cells(counter, 6).Value
'MsgBox (vals)
temp = Split(vals, ",")
i = 0
Do While i < 10
targetCell = counter + i
If ActiveSheet.Cells(targetCell, 2).Value = userId Then
ActiveSheet.Cells(targetCell, 7).Value = temp(0)
ActiveSheet.Cells(targetCell, 8).Value = temp(1)
ActiveSheet.Cells(targetCell, 9).Value = temp(2)
ActiveSheet.Cells(targetCell, 7).HorizontalAlignment = xlCenter
ActiveSheet.Cells(targetCell, 8).HorizontalAlignment = xlCenter
ActiveSheet.Cells(targetCell, 9).HorizontalAlignment = xlCenter
ActiveSheet.Cells(targetCell, 7).Borders().LineStyle = xlContinuous
ActiveSheet.Cells(targetCell, 8).Borders().LineStyle = xlContinuous
ActiveSheet.Cells(targetCell, 9).Borders().LineStyle = xlContinuous
End If
i = i + 1
Loop
temp = Null
'parsing_question_1(vals, userId)
End If
counter = counter + 1
Loop
End Sub
谢谢!