1

My beginners code [which works perfectly well] uses multiple IF statements. My question is [before I go any further because I will have lots of textboxes [txtbx#] is there any downside of coding this way ?

The user will be typing 40-50 short strings into textboxes while someone reads them to him then clicks a button to fill color all the matches in the worksheet column;

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
2
  • 1
    at each for loop the value of a cell will be compared against all the checkbox contents, is it intentional? Commented May 26, 2015 at 20:33
  • Yes Mate Juhasz. That was intentional. Commented May 26, 2015 at 21:23

1 Answer 1

3

If you name your textboxes "txtbx1", "txtbx2", etc you can do something like this:

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
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Tim. Food for thought there. I changed the textbox names to txtbx1 txtbx2 and NUM_TEXTBOXES as Long = 5 (amount of textboxes) but the code didn't color any cells ?
Sorry - fixed a typo in my answer: AddNum >> AbbNum
The typo fixed it. Massively impressed. Learning curve for me such a small code block instead of multiple IFs. Thank you very much indeed Tim
No problem - one benefit you get from writing lots of VBA is writing much less VBA...
I can also see that I can add as many textboxes as I like & I only need to change the NUM_TEXTBOXES as Long = #value. That's great !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.