ich möchte per AddIn in bestehende Excelblätter folgenden Code in schreiben.
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Range("A1").Select
End Sub
ich habe so etwas Ähnliches schon gemacht, aber das oben genannte will mir nicht gelingen.
Hier mein bisheriger funktionsfähiger Code, welchen in das Workbook_activate Ereignis schreibt
Sub clsMakro_in_Workbook_zufügen()
'Fügt in erste Tabelle des Workbooks ein Macro in Deactivate_Ereignis
Dim x As Variant, x1 As Long, x2 As Long, Anzahl_der_Zeilen As Long, Macrotext As String
Macrotext = "Range(" & Chr$(34) & "A1" & Chr$(34) & ").Select"
With ActiveWorkbook.VBProject.VBComponents("DieseArbeitsmappe").CodeModule
On Error GoTo error_1
x1 = .ProcBodyLine("Workbook_Activate", vbext_pk_Proc)
On Error GoTo 0
Anzahl_der_Zeilen = .CountOfLines
If x1 > 0 Then
x2 = .ProcBodyLine("Workbook_Activate", vbext_pk_Proc)
.DeleteLines 1, Anzahl_der_Zeilen
End If
continue_1:
x1 = .CreateEventProc("Activate", "Workbook")
.InsertLines x1 + 1, "'dieses Activate Makro wurde durch das Add-In per Makro eingefügt"
.InsertLines x2 + 2, "Sheets(1).Select"
.InsertLines x1 + 3, Macrotext
End With
Exit Sub
error_1:
On Error GoTo 0
GoTo continue_1
End Sub
wie lautet das gleiche für mein Problemkind Workbook_SheetActivate
bisher habe ich folgendes ausprobiert:
Sub A1_beim_Activieren_auswählen()
'Fügt in erste Tabelle des Workbooks ein Macro in Workbook_Open_Ereignis
Dim x As Variant, x1 As Long, x2 As Long, Anzahl_der_Zeilen As Long
Dim Macrotext As String
Macrotext = "Range(" & Chr$(34) & "A1" & Chr$(34) & ").Select"
With ActiveWorkbook.VBProject.VBComponents("DieseArbeitsmappe").CodeModule
On Error GoTo error_1
x1 = .ProcBodyLine("Workbook_SheetActivate", vbext_pk_Proc)
On Error GoTo 0
Anzahl_der_Zeilen = .CountOfLines
If x1 > 0 Then
x2 = .ProcBodyLine("Workbook_SheetActivate", vbext_pk_Proc)
.DeleteLines 1, Anzahl_der_Zeilen
End If
continue_1:
x1 = .CreateEventProc("SheetActivate", "Workbook")
.InsertLines x1 + 1, "'dieses Activate Makro wurde durch das Add-In per Makro eingefügt"
.InsertLines x1 + 2, Macrotext
End With
Exit Sub
error_1:
On Error GoTo 0
GoTo continue_1
End Sub
vielen Dank für eure Hilfe
Viele Grüße
Oliver Breith