Pregunta de complementos de VBA
-
27-10-2019 - |
Pregunta
Quiero ejecutar mi código cuando se carga el complemento. (Abra el archivo de Excel -> cargar complemento -> Código de ejecución)
Pero tengo un problema con el controlador de eventos.
No sé qué tipo de manejador de eventos necesito usar y cómo cargar la complicación instalada.
(Trato de usar Workbook_open Handler pero creo que está mal)
E intento usar el controlador de eventos Workbook_addininstall () y luego, cuando la instalación de complemento, funciona. Pero para hacer el trabajo mi código, necesito reinstalar el complemento cada vez.
¿Y cómo puedo ejecutar complementos que ya están en la lista de complementos?
Aquí está mi código,
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
Gracias.
Solución
Crea el evento en el complemento:
www.cpearson.com/excel/appevent.htm
Además, si lo desea, puede hacer el evento Workbookopen y simplemente verificar para asegurarse de que el libro de trabajo coincida con el nombre del libro de trabajo, o crear una hoja de trabajo oculta que tenga un valor en la celda A1 que le indique si es su libro de trabajo.
También puede desinstalar el complemento cada vez que se cierre su libro de trabajo (usando los eventos nuevamente), si esa es la ruta que prefiere.