I have a problem with a VBA Add-in in Excel 2010.
I’ve created some code for parsing my excel data. which I made into an Add-in.
However, when I load the Add-in and run, an error occurs.
The error message says: runtime error 91 object variable or With block variable not set
The error points to rowSize = ActiveSheet.Rows.Count.
Does anyone know how to fix this error?
Here is the code,
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
Thank you!
An add-in is just code – no user interface. Since there’s no user interface, there’s technically no sheet in the addin file that’s the ActiveSheet. There are actually sheets in an add-in, but none of them can be “active”.
If you want to work with worksheet within the add-in, you need to reference those sheets in a different way. For instance, if you want to work with the first sheet in your add-in, you can use code like
The Me keyword refers to the class you’re in. In this case, you’re in the ThisWorkbook module of the add-in, so Me refers the Workbook object that is the add-in.
If you want to work on a particular sheet that’s not in your add-in, you can open that workbook in your open event and refer to that sheet. Such as
Finally, if you want to run code whenever any workbook opens, you have to create a custom class module that listens for application level events. First create a custom class module call CAppEvents. In that custom class module, put this code
In a standard module, put this code