列表框选择未按预期显示在相应的文本框中。尽管代码逻辑将列表框中的选定值连接并显示到文本框中,但文本框仍为空。
这是代码:
Private Sub UserForm_Initialize()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
ListBoxSector.MultiSelect = fmMultiSelectSingle
OptionButton1.Caption = "Nota Unica"
OptionButton1.Value = True
OptionButton2.Caption = "Multiples Notas"
' Especifica la hoja de trabajo donde se encuentra la columna
Set ws = ThisWorkbook.Sheets("Tabla")
' Encuentra la última fila en la columna A con datos
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Llena la ListBoxSector con los valores de la columna A a partir de la fila 4
For i = 4 To lastRow
If ws.Cells(i, "A").Value <> "" Then ' Evitar celdas vacías
ListBoxSector.AddItem ws.Cells(i, "A").Value
End If
Next i
' Muestra la fecha de hoy en el TextBoxFecha en el formato deseado
TextBoxFecha.Value = Format(Date, "dd ""de"" mmmm ""de"" yyyy")
fechaOriginal = TextBoxFecha.Value ' Almacena la fecha original
End Sub
Private Sub OptionButton1_Click()
ListBoxSector.MultiSelect = fmMultiSelectSingle
End Sub
Private Sub OptionButton2_Click()
ListBoxSector.MultiSelect = fmMultiSelectMulti
End Sub
Private Sub ListBoxSector_Click()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim selectedPersons As String
Dim selectedFunctions As String
TextBoxPersona.Text = ""
TextBoxFuncion.Text = ""
' Especifica la hoja de trabajo donde se encuentra la columna
Set ws = ThisWorkbook.Sheets("Tabla")
' Encuentra la última fila en la columna A con datos
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Verifica si se ha seleccionado algún elemento en la ListBox
If ListBoxSector.ListIndex >= 0 Then
' Construye cadenas con los nombres y funciones correspondientes de los sectores seleccionados
For i = 0 To ListBoxSector.ListCount - 1
If ListBoxSector.Selected(i) Then
For j = 4 To lastRow
If ws.Cells(j, "A").Value = ListBoxSector.List(i) Then
If selectedPersons <> "" Then
selectedPersons = selectedPersons & " "
selectedFunctions = selectedFunctions & " "
End If
selectedPersons = selectedPersons & ws.Cells(j, "B").Value
selectedFunctions = selectedFunctions & ws.Cells(j, "C").Value
Exit For
End If
Next j
End If
Next i
End If
TextBoxPersona.Text = ""
TextBoxFuncion.Text = ""
' Muestra los nombres y funciones correspondientes en los TextBox
TextBoxPersona.Value = selectedPersons
TextBoxFuncion.Value = selectedFunctions
End Sub
Private Sub CheckBoxFecha_Change()
If CheckBoxFecha.Value Then
' Marcar el checkbox, restablecer la fecha
TextBoxFecha.Value = Format(Date, "dd ""de"" mmmm ""de"" yyyy")
Else
' Desmarcar el checkbox, restaurar la fecha modificada
TextBoxFecha.Value = fechaOriginal
End If
End Sub
Private Sub TextBoxFecha_Change()
' Actualizar la fecha original cuando el usuario modifica manualmente
fechaOriginal = TextBoxFecha.Value
End Sub
Private Sub CommandButtonEnviar_Click()
' Verificar si se ha seleccionado un sector en ListBoxSector
MsgBox "Selected Persons: " & selectedPersons
MsgBox "Selected Functions: " & selectedFunctions
If ListBoxSector.ListIndex = -1 Then
MsgBox "Por favor, selecciona una Cámara de Turismo.", vbExclamation, "Campo Obligatorio"
Exit Sub
End If
If TextBoxNota.Text = "" Then
MsgBox "El campo 'Nota' no puede estar vacío.", vbExclamation, "Error"
Exit Sub
End If
我补充说:
MsgBox "Selected Persons: " & selectedPersons
MsgBox "Selected Functions: " & selectedFunctions
查看代码在做什么,并查看它是否正在创建链但仍为空。
至少我找到了问题的根源。
当您选择OptionButton2时,这意味着
fmMultiSelectMulti
。
在这种情况下,在选择 ListBox 中的项目时“不会调用”ListBox_Click 事件。
更改事件不太适合解决该问题,因为每次单击列表中的项目时都会调用它。
可以做什么来将 CommandButton 添加到表单,并将整个事件过程分配给 CommandButton_Click 事件。