2011-09-08 30 views
-1

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

但我有一个事件处理程序的问题。

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

(我尝试使用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 

谢谢。

+0

你为什么认为Workbook_Open不正确?这是一个资源,指出它应该去哪里:http://www.cpearson.com/excel/createaddin.aspx –

+0

因为,我不想在另一个excel文件上运行mycode。我需要在特定的excel文件上运行vba代码,而不是其他。但是,使用Workbook_Open时,我会在打开时影响到所有的excel文件。 –

+0

你已经问了两个题目相同的问题。请为您的问题选择一个更具描述性的标题。 –

回答

0

创建的插件事件:

www.cpearson.com/excel/appevent.htm

另外,如果你想你可以做workbookopen事件,只是检查,以确保工作簿比赛工作簿名称,或者创建一个隐藏的工作表,该工作表在单元格A1中具有值,告诉您它是否是您的工作簿。

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