Vba 具有文本框字符串比较的多个IF语句
我的初学者代码[工作非常好]使用了多个IF语句。我的问题是[在我继续之前,因为我会有很多文本框[txtbx#]这样编码有什么缺点吗Vba 具有文本框字符串比较的多个IF语句,vba,excel,Vba,Excel,我的初学者代码[工作非常好]使用了多个IF语句。我的问题是[在我继续之前,因为我会有很多文本框[txtbx#]这样编码有什么缺点吗 Private Sub CommandButton1_Click() Dim i As Integer Dim AbbNum As String Dim AbbNum2 As String Dim AbbNum3 As String Dim AbbNum4 As String Dim AbbNum5 As String
Private Sub CommandButton1_Click()
Dim i As Integer
Dim AbbNum As String
Dim AbbNum2 As String
Dim AbbNum3 As String
Dim AbbNum4 As String
Dim AbbNum5 As String
Dim c As Integer
Dim MySheet As Worksheet
Set MySheet = Sheets(2)
c = MySheet.Cells(Rows.Count, 2).End(xlUp).Row
For i = 2 To c
AbbNum = txtbxONE.Value
If Cells(i, 1).Value = AbbNum Then
Cells(i, 1).Interior.ColorIndex = 6
End If
AbbNum2 = txtbxTWO.Value
If Cells(i, 1).Value = AbbNum2 Then
Cells(i, 1).Interior.ColorIndex = 6
End If
AbbNum3 = txtbxTHREE.Value
If Cells(i, 1).Value = AbbNum3 Then
Cells(i, 1).Interior.ColorIndex = 6
End If
AbbNum4 = txtbxFOUR.Value
If Cells(i, 1).Value = AbbNum4 Then
Cells(i, 1).Interior.ColorIndex = 6
End If
AbbNum5 = txtbxFIVE.Value
If Cells(i, 1).Value = AbbNum5 Then
Cells(i, 1).Interior.ColorIndex = 6
End If
Next i
End Sub
用户将在文本框中键入40-50个短字符串,同时有人向他读取这些字符串,然后单击按钮以填充工作表列中所有匹配的颜色
Private Sub CommandButton1_Click()
Dim i As Integer
Dim AbbNum As String
Dim AbbNum2 As String
Dim AbbNum3 As String
Dim AbbNum4 As String
Dim AbbNum5 As String
Dim c As Integer
Dim MySheet As Worksheet
Set MySheet = Sheets(2)
c = MySheet.Cells(Rows.Count, 2).End(xlUp).Row
For i = 2 To c
AbbNum = txtbxONE.Value
If Cells(i, 1).Value = AbbNum Then
Cells(i, 1).Interior.ColorIndex = 6
End If
AbbNum2 = txtbxTWO.Value
If Cells(i, 1).Value = AbbNum2 Then
Cells(i, 1).Interior.ColorIndex = 6
End If
AbbNum3 = txtbxTHREE.Value
If Cells(i, 1).Value = AbbNum3 Then
Cells(i, 1).Interior.ColorIndex = 6
End If
AbbNum4 = txtbxFOUR.Value
If Cells(i, 1).Value = AbbNum4 Then
Cells(i, 1).Interior.ColorIndex = 6
End If
AbbNum5 = txtbxFIVE.Value
If Cells(i, 1).Value = AbbNum5 Then
Cells(i, 1).Interior.ColorIndex = 6
End If
Next i
End Sub
如果将文本框命名为“txtbx1”、“txtbx2”等,则可以执行以下操作:
Private Sub CommandButton1_Click()
Dim i As Integer
Dim AbbNum As String
Dim AbbNum2 As String
Dim AbbNum3 As String
Dim AbbNum4 As String
Dim AbbNum5 As String
Dim c As Integer
Dim MySheet As Worksheet
Set MySheet = Sheets(2)
c = MySheet.Cells(Rows.Count, 2).End(xlUp).Row
For i = 2 To c
AbbNum = txtbxONE.Value
If Cells(i, 1).Value = AbbNum Then
Cells(i, 1).Interior.ColorIndex = 6
End If
AbbNum2 = txtbxTWO.Value
If Cells(i, 1).Value = AbbNum2 Then
Cells(i, 1).Interior.ColorIndex = 6
End If
AbbNum3 = txtbxTHREE.Value
If Cells(i, 1).Value = AbbNum3 Then
Cells(i, 1).Interior.ColorIndex = 6
End If
AbbNum4 = txtbxFOUR.Value
If Cells(i, 1).Value = AbbNum4 Then
Cells(i, 1).Interior.ColorIndex = 6
End If
AbbNum5 = txtbxFIVE.Value
If Cells(i, 1).Value = AbbNum5 Then
Cells(i, 1).Interior.ColorIndex = 6
End If
Next i
End Sub
Private Sub CommandButton1_Click()
Const NUM_TEXTBOXES as Long = 10 'for example....
Dim i As Long
Dim AbbNum As String, f As Range, rngSrch As Range
Set rngSrch = Sheets(2).Range("A:A")
For i = 1 To NUM_TEXTBOXES
AbbNum = Me.Controls("txtbx" & i).Value
If Len(AbbNum) > 0 Then
'EDIT: fixed typo in next line
Set f = rngSrch.Find(AbbNum, lookin:=xlvalues, lookat:=xlWhole)
If Not f Is nothing then f.Interior.ColorIndex = 6
End If
Next i
End Sub
在每个
for
循环中,一个单元格的值将与所有复选框内容进行比较,是故意的吗?是的,Mate Juhasz。这是故意的。谢谢Tim。这里值得思考。我将文本框名称更改为txtbx1 txtbx2和NUM_文本框,只要长度=5(文本框的数量)但是代码没有给任何单元格着色?抱歉-修复了我答案中的一个错误:AddNum
>AbbNum
这个错误修复了它。印象深刻。对我来说,学习曲线是一个如此小的代码块,而不是多个if。非常感谢你,确实没有问题-你从编写大量VBA中获得的一个好处是编写的VBA要少得多…我可以因此,请注意,我可以添加任意多个文本框&我只需要更改NUM_文本框的Long=#value。这太棒了!