当我在ComboBox1中选择一个值时,ComboBox2会自动填充指定范围内的数据。在ComboBox2中选择一个值时,相应的值将放在TextBox中。 看来,当我只有ComboBox值中的数字时,代码将无法运行。 如果值由字母或数字和字母组成,一切正常。 我尝试了范围内细胞的不同格式,但没有成功。在互联网上找到答案是有问题的,因为我不知道搜索的关键字。
到目前为止,这是我的代码:
Private Sub ComboBox1_Change()
Dim index1 As Integer
Dim cLoc1 As Range
Dim index2 As Integer
Dim cLoc2 As Range
Dim ws As Worksheet
Set ws = Worksheets("Formlists")
index1 = ComboBox1.ListIndex
'index2 = ComboBox2.ListIndex
ComboBox2.Clear
ComboBox3.Clear
Select Case index1
Case Is = 0
With ComboBox2
For Each cLoc1 In ws.Range("Codelist1")
With Me.ComboBox2
.AddItem cLoc1.Value
End With
Next cLoc1
End With
Case Is = 1
With ComboBox2
For Each cLoc1 In ws.Range("Codelist2")
With Me.ComboBox2
.AddItem cLoc1.Value
End With
Next cLoc1
End With
End Select
End Sub
Private Sub combobox2_change()
Dim index2 As Integer
Dim ws As Worksheet
Set ws = Worksheets("Formlists")
index2 = ComboBox1.ListIndex
Select Case index2
Case Is = 0
With TextBox4
For i = 1 To 10
If ws.Cells(i + 1, Columns(90).Column).Value = ComboBox2.Value Then
TextBox4.Value = ws.Cells(i + 1, Columns(91).Column).Value
Else
'do nothing
End If
Next i
End With
Case Is = 1
With TextBox4
For i = 1 To 10
If ws.Cells(i + 1, Columns(92).Column).Value = ComboBox2.Value Then
TextBox4.Value = ws.Cells(i + 1, Columns(93).Column).Value
Else
'do nothing
End If
Next i
End With
End Select
End Sub
有没有办法让代码/ ComboBox接受任何inputformat?
提前致谢
罗布
答案 0 :(得分:0)
看起来你只需要将值强制转换为代码中的字符串:
If CStr(ws.Cells(i + 1, 90).Value) = ComboBox2.Value Then
与其他测试类似。请注意,使用Columns(90).Column
而不仅仅是90
没有任何意义。 ;)