您好,所以我正在尝试开发UserForm,这是我的第一次。
我的ComboBox和ListBox出现问题(几乎无法正常运行)。
您看到的是,我在ListBox的正上方有一个ComboBox,这是我要选择的值,并且行值应同时显示在ListBox和ComboBoxes上。
问题1::这些值仅显示在ComboBoxes中,而不显示在ListBox中。
如果单击“查看数据”按钮,则电子表格中的所有值将显示在8列的列表框中。如果单击列表框中的一行(显然它将突出显示),它也将显示在下面的组合框中。
问题2:每当我尝试在ComboBox中编辑值并单击“更新”按钮时,都不会发生任何事情。它不会更新任何内容。
我到处都在寻找答案,但似乎找不到。
Private Sub btnDelete_Click()
Dim a As Integer
If MsgBox("Are you sure you want to delete this row?", vbYesNo + vbQuestion, "Yes") = vbYes Then
For a = 1 To Range("A100000").End(xlUp).Row
If Cells(a, 1) = listHeader.List(listHeader.ListIndex) Then
Rows(a).Select
Selection.Delete
End If
Next a
End If
End Sub
Private Sub btnSearch_Click()
'IM THINKING ABOUT REMOVING THE SEARCH BUTTON BECAUSE THE COMBOBOX ITSELF CAN BE USED FOR SEARCHING THE ROW
'IT MAKES THE SEARCH BUTTON USELESS
'Dim x As Long
'Dim y As Long
'x = Sheets("PRESTAGE DB").Range("A" & Rows.Count).End(xlUp).Row
'For y = 2 To x
'If Sheets("PRESTAGE DB").Cells(y, 1).Text = cmbSearch.Value Then
'cmbSchema.Text = Sheets("PRESTAGE DB").Cells(y, 1)
'cmbEnvironment.Text = Sheets("PRESTAGE DB").Cells(y, 2)
'cmbHost.Text = Sheets("PRESTAGE DB").Cells(y, 3)
'cmbIP.Text = Sheets("PRESTAGE DB").Cells(y, 4)
'cmbAccessible.Text = Sheets("PRESTAGE DB").Cells(y, 5)
'cmbLast.Text = Sheets("PRESTAGE DB").Cells(y, 6)
'cmbConfirmation.Text = Sheets("PRESTAGE DB").Cells(y, 7)
'cmbProjects.Text = Sheets("PRESTAGE DB").Cells(y, 8)
'End If
'Next y
End Sub
Private Sub btnView_Click()
listHeader.RowSource = "A4:H200"
End Sub
Private Sub cmbAdd_Click()
Dim sheet As Worksheet
Set sheet = ThisWorkbook.Sheets("PRESTAGE DB")
nextrow = sheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
sheet.Cells(nextrow, 1) = Me.cmbSchema
sheet.Cells(nextrow, 2) = Me.cmbEnvironment
sheet.Cells(nextrow, 3) = Me.cmbHost
sheet.Cells(nextrow, 4) = Me.cmbIP
sheet.Cells(nextrow, 5) = Me.cmbAccessible
sheet.Cells(nextrow, 6) = Me.cmbLast
sheet.Cells(nextrow, 7) = Me.cmbConfirmation
sheet.Cells(nextrow, 8) = Me.cmbProjects
MsgBox "Data Added!"
End Sub
Private Sub cmbClearFields_Click()
cmbSchema.Text = ""
cmbEnvironment.Text = ""
cmbHost.Text = ""
cmbIP.Text = ""
cmbAccessible.Text = ""
cmbLast.Text = ""
cmbConfirmation.Text = ""
cmbProjects.Text = ""
cmbSearch.Text = ""
End Sub
Private Sub cmbSearch_Change()
Dim x As Long
Dim y As Long
x = Sheets("PRESTAGE DB").Range("A" & Rows.Count).End(xlUp).Row
For y = 2 To x
If Sheets("PRESTAGE DB").Cells(y, 1).Text = cmbSearch.Value Then
cmbSchema.Text = Sheets("PRESTAGE DB").Cells(y, 1)
cmbEnvironment.Text = Sheets("PRESTAGE DB").Cells(y, 2)
cmbHost.Text = Sheets("PRESTAGE DB").Cells(y, 3)
cmbIP.Text = Sheets("PRESTAGE DB").Cells(y, 4)
cmbAccessible.Text = Sheets("PRESTAGE DB").Cells(y, 5)
cmbLast.Text = Sheets("PRESTAGE DB").Cells(y, 6)
cmbConfirmation.Text = Sheets("PRESTAGE DB").Cells(y, 7)
cmbProjects.Text = Sheets("PRESTAGE DB").Cells(y, 8)
End If
Next y
End Sub
Private Sub cmbUpdate_Click()
Dim x As Long
Dim y As Long
x = Sheets("PRESTAGE DB").Range("A" & Rows.Count).End(xlUp).Row
For y = 2 To x
If Sheets("PRESTAGE DB").Cells(y, 1).Text = cmbSchema.Value Then
Sheets("PRESTAGE DB").Cells(y, 2) = cmbEnvironment
Sheets("PRESTAGE DB").Cells(y, 3) = cmbHost
Sheets("PRESTAGE DB").Cells(y, 4) = cmbIP
Sheets("PRESTAGE DB").Cells(y, 5) = cmbAccessible
Sheets("PRESTAGE DB").Cells(y, 6) = cmbLast
Sheets("PRESTAGE DB").Cells(y, 7) = cmbConfirmation
Sheets("PRESTAGE DB").Cells(y, 8) = cmbProjects
End If
Next y
End Sub
Private Sub CommandButton5_Click()
listHeader.RowSource = ""
End Sub
Private Sub CommandButton7_Click()
End Sub
Private Sub listHeader_Click()
cmbSchema.Value = UserForm1.listHeader.Column(0)
cmbEnvironment.Value = UserForm1.listHeader.Column(1)
cmbHost.Value = UserForm1.listHeader.Column(2)
cmbIP.Value = UserForm1.listHeader.Column(3)
cmbAccessible.Value = UserForm1.listHeader.Column(4)
cmbLast.Value = UserForm1.listHeader.Column(5)
cmbConfirmation.Value = UserForm1.listHeader.Column(6)
cmbProjects.Value = UserForm1.listHeader.Column(7)
End Sub
Private Sub UserForm_Initialize()
cmbSearch.List = Sheets("PRESTAGE DB").Range("A4:A10000").Value
End Sub
答案 0 :(得分:1)
将以下代码用于问题1
对组合框使用了Change事件,因此,如果用户更改任何值,则将基于组合框中选择的值来填充列表框和组合框。
Private Sub ComboBox1_Change()
x = Sheets("PRESTAGEDB").Range("A" & Rows.Count).End(xlUp).Row
For y = 2 To x
If Sheets("PRESTAGEDB").Cells(y, 1).Text = ComboBox1.Value Then
cmbSchema.Text = Sheets("PRESTAGEDB").Cells(y, 1)
cmbEnvironment.Text = Sheets("PRESTAGEDB").Cells(y, 2)
cmbHost.Text = Sheets("PRESTAGEDB").Cells(y, 3)
cmbIP.Text = Sheets("PRESTAGEDB").Cells(y, 4)
cmbAccessible.Text = Sheets("PRESTAGEDB").Cells(y, 5)
cmbLast.Text = Sheets("PRESTAGEDB").Cells(y, 6)
cmbConfirmation.Text = Sheets("PRESTAGEDB").Cells(y, 7)
cmbProjects.Text = Sheets("PRESTAGEDB").Cells(y, 8)
UserForm1.ListBox1.RowSource = "A" + CStr(y) + ": H" + CStr(y)
Exit For
End If
Next y
End Sub
请解决是否解决您的问题
答案 1 :(得分:0)
对于问题2
使用.Value从字段中获取值,然后将其设置为电子表格-> cmbEnvironment.Value
Private Sub cmbUpdate_Click()
Dim x As Long
Dim y As Long
x = Sheets("PRESTAGEDB").Range("A" & Rows.Count).End(xlUp).Row
For y = 2 To x
If Sheets("PRESTAGEDB").Cells(y, 1).Text = cmbSchema.Value Then
Sheets("PRESTAGEDB").Cells(y, 2) = cmbEnvironment.Value
Sheets("PRESTAGEDB").Cells(y, 3) = cmbHost.Value
Sheets("PRESTAGEDB").Cells(y, 4) = cmbIP.Value
Sheets("PRESTAGEDB").Cells(y, 5) = cmbAccessible.Value
Sheets("PRESTAGEDB").Cells(y, 6) = cmbLast.Value
Sheets("PRESTAGEDB").Cells(y, 7) = cmbConfirmation.Value
Sheets("PRESTAGEDB").Cells(y, 8) = cmbProjects.Value
Exit For
End If
Next y
End Sub
当我更改组合框中的值时,将在单元格中设置更新的值。请调试代码并共享详细信息(如果无法使用)。