Añadir controles y evento en tiempo de ejecución excel
Tengo un formulario en excel que en su evento initialize crea tantos labels como trabajadores encuentra en la hoja "personal" y sus correspondientes textbox para introducir la cantidad de horas trabajadas y dos commandbutton, uno para validar los datos introducidos y copiarlos en la hoja "horas" y otro para salir. El problema es que no sé como añadir los eventos en tiempo de ejecución a cada uno de los botones. Pego el código a continuación para que se comprenda mejor:
Dim lrow As Long
Dim ws As Worksheet
Private Sub UserForm_Initialize()
Dim n As Integer
Set ws = Worksheets("Personal")
lrow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Row - 1
Txtfecha.Value = Format(Date, "medium date")
Txtfecha.SetFocus
Me.ScrollHeight = (lrow * 25) + 95
For n = 1 To lrow
With Me.Controls.Add("Forms.Label.1")
.Name = "Label" & n: .Caption = ws.Range("A" & n + 1) & " " & _
ws.Range("B" & n + 1) & " " & ws.Range("C" & n + 1)
.Top = ((n - 1) * 25) + 40: .Height = 25
.Left = 20: .Width = 175: .Font.Size = 11
End With
With Me.Controls.Add("Forms.Textbox.1")
.Name = "Txtbox" & n: .Top = ((n - 1) * 25) + 35: .Height = 20
.Left = 200: .Width = 30: .Font.Size = 11
End With
Next
With Me.Controls.Add("Forms.Commandbutton.1")
.Name = "CmdValidar": .Caption = "VALIDAR"
.Top = (lrow * 25) + 45: .Height = 30
.Left = 25: .Width = 75: .Font.Size = 12
.BackColor = &H80FFFF: .ForeColor = &H4000&
.Font.Bold = True
End With
With Me.Controls.Add("Forms.Commandbutton.1")
.Name = "CmdSalir": .Caption = "SALIR"
.Top = (lrow * 25) + 45: .Height = 30
.Left = 145: .Width = 75: .Font.Size = 12
.BackColor = &H80FFFF: .ForeColor = &H4000&
.Font.Bold = True
End With
End Sub
Private Sub CmdValidar_Click()
Dim n As Integer
Dim ltrow As Long
Dim wsh As Worksheet
wsh = Worksheets("Horas")
Set ws = Worksheets("Personal")
lrow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Row - 1
ltrow = wsh.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
For n = 1 To lrow
With wsh
.Cells(ltrow, 1).Value = Txtfecha.Value
.Cells(ltrow, 2).Value = Label & n.Caption
.Cells(ltrow, 3).Value = txtbox & n.Value
End With
ltrow = ltrow + 1
Next
End Sub
Dim lrow As Long
Dim ws As Worksheet
Private Sub UserForm_Initialize()
Dim n As Integer
Set ws = Worksheets("Personal")
lrow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Row - 1
Txtfecha.Value = Format(Date, "medium date")
Txtfecha.SetFocus
Me.ScrollHeight = (lrow * 25) + 95
For n = 1 To lrow
With Me.Controls.Add("Forms.Label.1")
.Name = "Label" & n: .Caption = ws.Range("A" & n + 1) & " " & _
ws.Range("B" & n + 1) & " " & ws.Range("C" & n + 1)
.Top = ((n - 1) * 25) + 40: .Height = 25
.Left = 20: .Width = 175: .Font.Size = 11
End With
With Me.Controls.Add("Forms.Textbox.1")
.Name = "Txtbox" & n: .Top = ((n - 1) * 25) + 35: .Height = 20
.Left = 200: .Width = 30: .Font.Size = 11
End With
Next
With Me.Controls.Add("Forms.Commandbutton.1")
.Name = "CmdValidar": .Caption = "VALIDAR"
.Top = (lrow * 25) + 45: .Height = 30
.Left = 25: .Width = 75: .Font.Size = 12
.BackColor = &H80FFFF: .ForeColor = &H4000&
.Font.Bold = True
End With
With Me.Controls.Add("Forms.Commandbutton.1")
.Name = "CmdSalir": .Caption = "SALIR"
.Top = (lrow * 25) + 45: .Height = 30
.Left = 145: .Width = 75: .Font.Size = 12
.BackColor = &H80FFFF: .ForeColor = &H4000&
.Font.Bold = True
End With
End Sub
Private Sub CmdValidar_Click()
Dim n As Integer
Dim ltrow As Long
Dim wsh As Worksheet
wsh = Worksheets("Horas")
Set ws = Worksheets("Personal")
lrow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Row - 1
ltrow = wsh.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
For n = 1 To lrow
With wsh
.Cells(ltrow, 1).Value = Txtfecha.Value
.Cells(ltrow, 2).Value = Label & n.Caption
.Cells(ltrow, 3).Value = txtbox & n.Value
End With
ltrow = ltrow + 1
Next
End Sub
3 Respuestas
Respuesta de Mario Lugo
2
Respuesta de Pedro Luis Hernández Velásquez
1
Respuesta de borisbarrera
1