A试图使用组合框和14个文本框显示表中的数据,以便在单击下拉按钮中选择某项时,数据应显示在相应的文本框中。但是在编写代码并尝试运行它之后,就收到了该错误消息。我是VBA和编程的新手,而且自学成才,所以请帮助我了解您向我解释的内容。
这是我的代码:
Private Sub ComboBox1_DropButtonClick()
Dim i As Long, LastRow As Long
LastRow = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row
If Me.ComboBox1.ListCount = 0 Then
For i = 8 To LastRow
Me.ComboBox1.AddItem Sheets("sheet1").Cells(i, "A").Value
Next i
End If
End Sub
Private Sub ComboBox1_Change()
Dim i As Long, LastRow As Long
LastRow = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row
If Me.ComboBox1.ListCount = 0 Then
For i = 8 To LastRow
If Sheets("sheet1").Cells(i, "A").Value = Val(Me.ComboBox1) Then
Me.TextBox1 = Sheets("sheet1").Cells(i, "B").Value
Me.TextBox2 = Sheets("sheet1").Cells(i, "C").Value
Me.TextBox3 = Sheets("sheet1").Cells(i, "D").Value
Me.TextBox4 = Sheets("sheet1").Cells(i, "E").Value
Me.TextBox5 = Sheets("sheet1").Cells(i, "F").Value
Me.TextBox6 = Sheets("sheet1").Cells(i, "G").Value
Me.TextBox7 = Sheets("sheet1").Cells(i, "H").Value
Me.TextBox8 = Sheets("sheet1").Cells(i, "I").Value
Me.TextBox9 = Sheets("sheet1").Cells(i, "J").Value
Me.TextBox10 = Sheets("sheet1").Cells(i, "K").Value
Me.TextBox11 = Sheets("sheet1").Cells(i, "L").Value
Me.TextBox12 = Sheets("sheet1").Cells(i, "M").Value
Me.TextBox13 = Sheets("sheet1").Cells(i, "N").Value
Me.TextBox14 = Sheets("sheet1").Cells(i, "O").Value
End If
Next
End Sub
如果没有结束,则阻止if告诉您您已经启动了If结构,如果选择“ End If”关闭则没有关闭。 @Storax已经指出:
“如果对于If Me.ComboBox1.ListCount = 0,则缺少结尾第二个子项。“
缩进代码有助于避免此类问题。您可能想阅读Wikipedia page about indenting code和another page on gitbooks that I thought looked good。