Excel VBA加载项问题
我想在加载加载项时运行代码。(打开excel文件->加载加载项->运行代码) 但我对事件处理程序有一个问题 我不知道我需要使用什么类型的事件处理程序以及如何加载已安装的加载项 (我尝试使用工作簿\打开处理程序,但我认为这是错误的) 我尝试使用Workbook_AddinInstall()事件处理程序,然后当安装外接程序时,它就可以工作了。但是为了使代码正常工作,我每次都需要重新安装外接程序 以及如何运行已在外接程序列表中的外接程序 这是我的密码Excel VBA加载项问题,excel,vba,event-handling,excel-addins,Excel,Vba,Event Handling,Excel Addins,我想在加载加载项时运行代码。(打开excel文件->加载加载项->运行代码) 但我对事件处理程序有一个问题 我不知道我需要使用什么类型的事件处理程序以及如何加载已安装的加载项 (我尝试使用工作簿\打开处理程序,但我认为这是错误的) 我尝试使用Workbook_AddinInstall()事件处理程序,然后当安装外接程序时,它就可以工作了。但是为了使代码正常工作,我每次都需要重新安装外接程序 以及如何运行已在外接程序列表中的外接程序 这是我的密码 Private Sub Workbook_Addi
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
Private子工作簿\u AddinInstall()
昏暗的柜台一样长
暗行大小与长行大小相同
将用户ID设置为字符串
模糊答案为字符串
作为字符串的Dim vals
作为整数的Dim i
设置currentSheet=ActiveWorkbook.ActiveSheet
MsgBox(currentSheet.Cells(1,2).Value)
rowSize=currentSheet.Rows.Count
计数器=1
'创建列
currentSheet.Cells(1,7).Value=“国家”
currentSheet.Cells(1,8).Value=“State”
currentSheet.Cells(1,9).Value=“年龄”
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).水平对齐=xlCenter
currentSheet.Cells(1,8).水平对齐=xlCenter
currentSheet.Cells(1,9).水平对齐=xlCenter
currentSheet.Cells(1,7).Borders().LineStyle=xlContinuous
currentSheet.Cells(1,8).Borders().LineStyle=xlContinuous
currentSheet.Cells(1,9).Borders().LineStyle=xlContinuous
'设定值
当计数器<行大小时执行此操作
如果currentSheet.Cells(计数器,1).Value=Null,则退出Do
如果currentSheet.Cells(计数器,4).Value=“3”,则
userId=currentSheet.Cells(计数器,2).Value
VAL=currentSheet.单元格(计数器,6).值
'MsgBox(VAL)
温度=拆分(VAL,“,”)
i=0
当我<10岁时做
目标电池=计数器+i
如果currentSheet.Cells(targetCell,2).Value=userId,则
currentSheet.Cells(目标单元格,7)。值=温度(0)
currentSheet.Cells(目标单元格,8)。值=温度(1)
currentSheet.Cells(目标单元格,9)。值=温度(2)
currentSheet.Cells(目标单元格,7)。水平对齐=xlCenter
currentSheet.Cells(目标单元格,8)。水平对齐=xlCenter
currentSheet.Cells(目标单元格,9)。水平对齐=xlCenter
currentSheet.Cells(targetCell,7).Borders().LineStyle=xlContinuous
currentSheet.Cells(targetCell,8).Borders().LineStyle=xlContinuous
currentSheet.Cells(targetCell,9).Borders().LineStyle=xlContinuous
如果结束
i=i+1
环
温度=零
'解析问题1(VAL,用户ID)
如果结束
计数器=计数器+1
环
端接头
谢谢。在加载项中创建事件: www.cpearson.com/excel/appevent.htm 此外,如果需要,您可以执行workbookopen事件,只需检查以确保工作簿与工作簿名称匹配,或者创建一个隐藏的工作表,该工作表在单元格A1中有一个值,告诉您它是否是您的工作簿
您也可以在每次工作簿关闭时卸载外接程序(再次使用事件),如果这是您喜欢的路径。为什么您认为工作簿打开不正确?这里有一个资源指示它应该去哪里:因为,我不想在另一个excel文件上运行mycode。我需要在特定的excel文件上运行vba代码,而不是其他文件。但使用工作簿_Open then,当我打开时,我将影响所有excel文件。您已经问了两个标题相同的问题。请为您的问题选择更具描述性的标题。