ich habe eine Reihe Buttons die ich zur Laufzeit erstelle, und eine zweite Reihe Buttons, auch zur Lauzeit geladen, die ich beim clicken auf ein Button der erste Reihen benennen möchte.
Hier mein Code für die Buttons ( Danke an Luschi ) :
'###### Button Array 1 #####
Private Sub UserForm_Initialize()
Dim ctrl As MSForms.Control
Dim Obj As Control
Dim i As Long
Dim myClass1 As clsButtonArray1
Set myColl1 = New Collection
Dim myClass2 As clsButtonArray2
Set myColl2 = New Collection
Dim a As Integer, j As Integer, k As Integer, t As Integer
k = 1 'Spalte
j = 1 'Zeile
t = 10 'Anzahl Tasten in Zeile
For i = 1 To 60
Set ctrl = Me.Frame1.Controls.Add("Forms.CommandButton.1", "myCmd1" & i, True)
With ctrl
.Tag = i
' .Caption = "cmdButton " & Format(i, CStr("00"))
.Left = k * 50.6 - 50.6
.Height = 20
.Width = 50.6
.Top = j * 20 - 20
.BackColor = &H80000002
.Font.Size = 9
End With
k = k + 1
If i Mod t = 0 Then
j = j + 1 'nächste Zeile
k = 1
End If
Set myClass1 = New clsButtonArray1
Set myClass1.myCmd1 = ctrl
myColl1.Add myClass1
' For Each Obj In Me.Frame1.Controls 'Button1 Name Vergeben
' If Obj.Name = "myCmd1" & i Then
' Obj.Caption = Sheets("Tabelle6").Range("A" & i).Value
' End If
' With myCmd2
' Select Case .Name
' Case "C"
' Obj.Caption = Sheets("Tabelle6").Range("A" & i).Value
'' Case "C#"
'' Obj.Caption = Sheets("Tabelle6").Range("C" & i).Value
'' Case "Db"
'' Obj.Caption = Sheets("Tabelle6").Range("E" & i).Value
'' Case "D"
'' Obj.Caption = Sheets("Tabelle6").Range("G" & i).Value
' End Select
' End With
' If (myCmd2.Caption) = "C" Then
' Obj.Caption = Sheets("Tabelle3").Range("C" & i).Value
' End If
' Next Obj
Next i
'###### Button Array 2 ######
For i = 1 To 13
Set ctrl = Me.Frame2.Controls.Add("Forms.CommandButton.1", "myCmd2" & i, True)
With ctrl
.Tag = i
.Caption = "cmdButton " & Format(i, CStr("00"))
.Left = i * 41 - 41
.Height = 48
.Width = 43
.Top = -3
.BackColor = &HFF0000
.ForeColor = vbWhite
.Font.Size = 10
' If .Tag = 2 Then 'C#/Db ' Button Position in Frame2
' .Left = 42
' End If
End With
Set myClass2 = New clsButtonArray2
Set myClass2.myCmd2 = ctrl
myColl2.Add myClass2
For Each Obj In Me.Frame2.Controls 'Button2 Name Vergeben
If Obj.Name = "myCmd2" & i Then
Obj.Caption = Sheets("Tabelle1").Range("A" & i).Value
End If
Next Obj
Next i
End Sub
Hier mein Code in ein Klassenmodul:Option Explicit
Public WithEvents myCmd2 As MSForms.CommandButton
Public WithEvents myCmd1 As MSForms.CommandButton
Private Sub myCmd2_Click()
Dim ctrl As MSForms.Control
Dim Obj As Control
Dim i As Long
Worksheets("Tabelle6").Activate
ActiveSheet.Cells(1, 11).Value = myCmd2.Caption
For Each Obj In UserForm1.Frame1.Controls
If Obj.Name = "myCmd1" & i Then
' Obj.Caption = Sheets("Tabelle6").Range("A" & i).Value
' End If
With myCmd2
Select Case .Caption
Case "C"
myCmd1.Caption = Sheets("Tabelle6").Range("A" & i).Value
'' Case "C#"
'' Obj.Caption = Sheets("Tabelle6").Range("C" & i).Value
'' Case "Db"
'' Obj.Caption = Sheets("Tabelle6").Range("E" & i).Value
'' Case "D"
'' Obj.Caption = Sheets("Tabelle6").Range("G" & i).Value
End Select
End With
End If
Next Obj
End Sub
Nach etliche Versuche das ist mein lezter Stand, Klappt nicht, die Buttons bekommen keine Beschriftung.Was mache ich falsch?
Danke in Voraus für die Hilfestellung.
Antonio