Excel VBA加载项问题

Excel VBA加载项问题,excel,vba,excel-addins,Excel,Vba,Excel Addins,我对Excel 2010中的VBA加载项有问题 我已经创建了一些用于解析excel数据的代码。我把它做成了一个插件 但是,当我加载并运行外接程序时,会发生错误 错误消息显示:runtime error 91 object variable或With block variable not set 错误指向rowSize=ActiveSheet.Rows.Count 有人知道如何修复此错误吗 这是密码 Private Sub Workbook_Open() Dim counter As Long

我对Excel 2010中的VBA加载项有问题

我已经创建了一些用于解析excel数据的代码。我把它做成了一个插件

但是,当我加载并运行外接程序时,会发生错误

错误消息显示:
runtime error 91 object variable或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
Private子工作簿\u Open()
昏暗的柜台一样长
暗行大小与长行大小相同
将用户ID设置为字符串
模糊答案为字符串
作为字符串的Dim vals
作为整数的Dim i
rowSize=ActiveSheet.Rows.Count
计数器=1
'创建列
ActiveSheet.Cells(1,7).Value=“Country”
ActiveSheet.Cells(1,8).Value=“State”
ActiveSheet.Cells(1,9).Value=“年龄”
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).水平对齐=xlCenter
ActiveSheet.Cells(1,8).水平对齐=xlCenter
ActiveSheet.Cells(1,9).水平对齐=xlCenter
ActiveSheet.Cells(1,7).Borders().LineStyle=xlContinuous
ActiveSheet.Cells(1,8).Borders().LineStyle=xlContinuous
ActiveSheet.Cells(1,9).Borders().LineStyle=xlContinuous
'设定值
当计数器<行大小时执行此操作
如果ActiveSheet.Cells(计数器,1).Value=Null,则退出Do
如果ActiveSheet.Cells(计数器,4).Value=“3”,则
userId=ActiveSheet.Cells(计数器,2).Value
VAL=ActiveSheet.Cells(计数器,6).Value
'MsgBox(VAL)
温度=拆分(VAL,“,”)
i=0
当我<10岁时做
目标电池=计数器+i
如果ActiveSheet.Cells(targetCell,2).Value=userId,则
ActiveSheet.Cells(targetCell,7)。值=温度(0)
ActiveSheet.Cells(targetCell,8)。值=温度(1)
ActiveSheet.Cells(targetCell,9).Value=temp(2)
ActiveSheet.Cells(targetCell,7)。水平对齐=xlCenter
ActiveSheet.Cells(targetCell,8)。水平对齐=xlCenter
ActiveSheet.Cells(targetCell,9)。水平对齐=xlCenter
ActiveSheet.Cells(targetCell,7).Borders().LineStyle=xlContinuous
ActiveSheet.Cells(targetCell,8).Borders().LineStyle=xlContinuous
ActiveSheet.Cells(targetCell,9).Borders().LineStyle=xlContinuous
如果结束
i=i+1
环
温度=零
'解析问题1(VAL,用户ID)
如果结束
计数器=计数器+1
环
端接头

谢谢大家!

由于加载项的
Woorkbook\u Open
事件在第一张可见工作表打开之前运行,因此在该时间点没有活动工作表,因此未设置
ActiveSheet

正如Tim所说,您可能无论如何都不想在addin\u Open事件中使用此代码


由于加载项的
Woorkbook\u Open
事件在第一张可见工作表打开之前运行,因此在该时间点没有活动工作表,因此未设置
ActiveSheet

正如Tim所说,您可能无论如何都不想在addin\u Open事件中使用此代码


外接程序只是代码,没有用户界面。因为没有用户界面,所以从技术上讲,加载项文件中没有一个工作表是ActiveSheet。外接程序中实际上有一些工作表,但它们都不能是“活动”的

如果要在外接程序中使用工作表,则需要以不同的方式引用这些工作表。例如,如果要处理外接程序中的第一张工作表,可以使用如下代码

Me.Sheets(1).Rows.Count
Me关键字指的是您所在的班级。在本例中,您位于外接程序的ThisWorkbook模块中,因此Me引用作为外接程序的工作簿对象

如果要处理外接程序中没有的特定工作表,可以在“打开”事件中打开该工作簿并引用该工作表。比如

Dim sh As Worksheet

Set sh = Workbooks.Open("C:\MyPath\MyBook.xls").Sheets(1)

rowSize = sh.Rows.Count
最后,如果希望在任何工作簿打开时运行代码,则必须创建一个侦听应用程序级事件的自定义类模块。首先创建一个自定义类模块调用CAppEvents。在该自定义类模块中,放入以下代码

Private WithEvents mApp As Application

Public Property Set App(oApp As Application)
    Set mApp = oApp
End Property

Public Property Get App() As Application
    Set App = mApp
End Property

Private Sub mApp_WorkbookOpen(ByVal wb As Workbook)
    FormatWorkbook wb
End Sub

'or to limit which workbook it runs on - in this example based on the path
'but you may use some other condition like the existence of a particular
'custom document property
Private Sub mApp_WorkbookOpen(ByVal wb As Workbook)
    If wb.Path = "\\Server1\mypath" Then
        FormatWorkbook wb
    End If
End Sub
Public clsAppEvents As CAppEvents

Sub Auto_Open()

    Set clsAppEvents = New CAppEvents
    Set clsAppEvents.App = Application

End Sub

Sub FormatWorkbook(wb As Workbook)

    Dim sh As Worksheet

    Set sh = wb.Sheets(1)

    'do stuff here

End Sub
在标准模块中,输入以下代码

Private WithEvents mApp As Application

Public Property Set App(oApp As Application)
    Set mApp = oApp
End Property

Public Property Get App() As Application
    Set App = mApp
End Property

Private Sub mApp_WorkbookOpen(ByVal wb As Workbook)
    FormatWorkbook wb
End Sub

'or to limit which workbook it runs on - in this example based on the path
'but you may use some other condition like the existence of a particular
'custom document property
Private Sub mApp_WorkbookOpen(ByVal wb As Workbook)
    If wb.Path = "\\Server1\mypath" Then
        FormatWorkbook wb
    End If
End Sub
Public clsAppEvents As CAppEvents

Sub Auto_Open()

    Set clsAppEvents = New CAppEvents
    Set clsAppEvents.App = Application

End Sub

Sub FormatWorkbook(wb As Workbook)

    Dim sh As Worksheet

    Set sh = wb.Sheets(1)

    'do stuff here

End Sub

外接程序只是代码,没有用户界面。因为没有用户界面,所以从技术上讲,加载项文件中没有一个工作表是ActiveSheet。外接程序中实际上有一些工作表,但它们都不能是“活动”的

如果要在外接程序中使用工作表,则需要以不同的方式引用这些工作表。例如,如果要处理外接程序中的第一张工作表,可以使用如下代码

Me.Sheets(1).Rows.Count
Me关键字指的是您所在的班级。在本例中,您位于外接程序的ThisWorkbook模块中,因此Me引用作为外接程序的工作簿对象

如果要处理外接程序中没有的特定工作表,可以在“打开”事件中打开该工作簿并引用该工作表。比如

Dim sh As Worksheet

Set sh = Workbooks.Open("C:\MyPath\MyBook.xls").Sheets(1)

rowSize = sh.Rows.Count
最后,如果希望在任何工作簿打开时运行代码,则必须创建一个侦听应用程序级事件的自定义类模块。首先创建一个自定义类模块调用CAppEvents。在该自定义类模块中,放入以下代码

Private WithEvents mApp As Application

Public Property Set App(oApp As Application)
    Set mApp = oApp
End Property

Public Property Get App() As Application
    Set App = mApp
End Property

Private Sub mApp_WorkbookOpen(ByVal wb As Workbook)
    FormatWorkbook wb
End Sub

'or to limit which workbook it runs on - in this example based on the path
'but you may use some other condition like the existence of a particular
'custom document property
Private Sub mApp_WorkbookOpen(ByVal wb As Workbook)
    If wb.Path = "\\Server1\mypath" Then
        FormatWorkbook wb
    End If
End Sub
Public clsAppEvents As CAppEvents

Sub Auto_Open()

    Set clsAppEvents = New CAppEvents
    Set clsAppEvents.App = Application

End Sub

Sub FormatWorkbook(wb As Workbook)

    Dim sh As Worksheet

    Set sh = wb.Sheets(1)

    'do stuff here

End Sub
在标准模块中,输入以下代码

Private WithEvents mApp As Application

Public Property Set App(oApp As Application)
    Set mApp = oApp
End Property

Public Property Get App() As Application
    Set App = mApp
End Property

Private Sub mApp_WorkbookOpen(ByVal wb As Workbook)
    FormatWorkbook wb
End Sub

'or to limit which workbook it runs on - in this example based on the path
'but you may use some other condition like the existence of a particular
'custom document property
Private Sub mApp_WorkbookOpen(ByVal wb As Workbook)
    If wb.Path = "\\Server1\mypath" Then
        FormatWorkbook wb
    End If
End Sub
Public clsAppEvents As CAppEvents

Sub Auto_Open()

    Set clsAppEvents = New CAppEvents
    Set clsAppEvents.App = Application

End Sub

Sub FormatWorkbook(wb As Workbook)

    Dim sh As Worksheet

    Set sh = wb.Sheets(1)

    'do stuff here

End Sub

很抱歉,现在我知道如何接受答案了:(为什么在工作簿\u open事件处理程序中有这个?只有加载外接程序时才会运行。是否要在Excel中打开的任何工作簿上运行它?如果是,请查找“应用程序事件”。我只想运行加载项。我需要使用什么事件处理程序?很抱歉,现在我知道如何接受以下答案:(为什么工作簿中有它?打开事件处理程序?只有加载加载项时才会运行它。是否要在Excel中打开的任何工作簿上运行它?如果是,请查找“应用程序事件”。我只想运行加载项。我需要使用什么事件处理程序?开头的段落不正确:
ActiveSheet
引用当前活动的工作表(在GUI中具有焦点的工作表),而不考虑