-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
謝謝。
你爲什麼認爲Workbook_Open不正確?這是一個資源,指出它應該去哪裏:http://www.cpearson.com/excel/createaddin.aspx –
因爲,我不想在另一個excel文件上運行mycode。我需要在特定的excel文件上運行vba代碼,而不是其他。但是,使用Workbook_Open時,我會在打開時影響到所有的excel文件。 –
你已經問了兩個題目相同的問題。請爲您的問題選擇一個更具描述性的標題。 –