-1

我想在加载项加载时运行我的代码。(打开excel文件->加载插件->运行代码)

但我对事件处理程序有疑问。

我不知道我需要使用哪种事件处理程序以及如何加载已安装的插件。

(我尝试使用 Workbook_open 处理程序,但我认为这是错误的)

我尝试使用 Workbook_AddinInstall() 事件处理程序,然后在安装加载项时,它可以工作。但是为了使我的代码工作,我每次都需要重新安装加载项。

以及如何运行已在加载项列表中的加载项。

这是我的代码,

Private Sub Workbook_AddinInstall()
Dim counter As Long
Dim rowSize As Long
Dim userId As String
Dim answers As String
Dim vals As String

Dim i As Integer

Set currentSheet = ActiveWorkbook.ActiveSheet

MsgBox (currentSheet.Cells(1, 2).Value)

rowSize = currentSheet.Rows.Count
counter = 1


'Create Column

currentSheet.Cells(1, 7).Value = "Country"
currentSheet.Cells(1, 8).Value = "State"
currentSheet.Cells(1, 9).Value = "Age"

currentSheet.Cells(1, 7).Font.Bold = True
currentSheet.Cells(1, 8).Font.Bold = True
currentSheet.Cells(1, 9).Font.Bold = True

currentSheet.Cells(1, 7).HorizontalAlignment = xlCenter
currentSheet.Cells(1, 8).HorizontalAlignment = xlCenter
currentSheet.Cells(1, 9).HorizontalAlignment = xlCenter

currentSheet.Cells(1, 7).Borders().LineStyle = xlContinuous
currentSheet.Cells(1, 8).Borders().LineStyle = xlContinuous
currentSheet.Cells(1, 9).Borders().LineStyle = xlContinuous

'Set Value
Do While counter < rowSize

    If currentSheet.Cells(counter, 1).Value = Null Then Exit Do
    If currentSheet.Cells(counter, 4).Value = "3" Then

        userId = currentSheet.Cells(counter, 2).Value
        vals = currentSheet.Cells(counter, 6).Value
        'MsgBox (vals)

        temp = Split(vals, ",")
        i = 0

        Do While i < 10
            targetCell = counter + i
            If currentSheet.Cells(targetCell, 2).Value = userId Then
               currentSheet.Cells(targetCell, 7).Value = temp(0)
               currentSheet.Cells(targetCell, 8).Value = temp(1)
               currentSheet.Cells(targetCell, 9).Value = temp(2)

               currentSheet.Cells(targetCell, 7).HorizontalAlignment = xlCenter
               currentSheet.Cells(targetCell, 8).HorizontalAlignment = xlCenter
               currentSheet.Cells(targetCell, 9).HorizontalAlignment = xlCenter

               currentSheet.Cells(targetCell, 7).Borders().LineStyle = xlContinuous
               currentSheet.Cells(targetCell, 8).Borders().LineStyle = xlContinuous
               currentSheet.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

谢谢你。

4

1 回答 1

0

在插件中创建事件:

www.cpearson.com/excel/appevent.htm

此外,如果您愿意,您可以执行 workbookopen 事件并检查以确保工作簿与工作簿名称匹配,或者创建一个隐藏的工作表,该工作表在单元格 A1 中有一个值,告诉您它是否是您的工作簿。

您还可以在每次工作簿关闭时卸载加载项(再次使用事件),如果这是您喜欢的路线。

于 2011-09-08T18:10:07.987 回答