VBAString Tip 5: Excel VBA Loop To Find Records Matching Search Criteria

This post will show you how to compare cells and display message box if condition is true.

I will show you how to:
-how to assign values to your variables _
-how to loop through rows _
-how to evaluate cell value with if statement

In the following image, the cells in columns A and B are being looped and the 2 cell values and being compared. When the condition is met, a message box will appear telling the row the condition is met on.

excel_vba_loop_rows_condition

Sub Button1_Click()
    Dim intRow As Integer
    
    Dim intCellA As Integer
    Dim intCellB As Integer

    For intRow = 1 To 14
        intCellA = Range("A" & intRow)
        intCellB = Range("B" & intRow)
        
        If intCellA > intCellB Then
            MsgBox "On row " & intRow & ", Cell A value is greater than Cell B"
        End If
    Next
    
    
    MsgBox "End.", vbInformation, "Complete"
End Sub

Watch how I do it:

Let me know if you have any questions

[simple_contact_form]

****************************************************


Posted

in

by

Tags: