Excel 2010中的VBA加載項存在問題。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
謝謝!
如何接受的答案? –
對不起,現在我知道如何接受答案:( –
爲什麼在workbook_open事件處理程序中有這樣的內容?只有當您的外接程序加載時纔會運行。是否想要在Excel中打開的任何工作簿上運行它?如果是這樣,查找「應用程序事件」 –