Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88854

VBA/Excel If statement is ignored (even if it is true), and Always executes Else

$
0
0

Any help will be greatly appreciated, as I am very new to VBA in Excel. I want to check column 1 for any matches with a TextBox. If there is a match update a few textboxes with worksheet data. If there is no match, then display a MsgBox. Here's a snippet of my code:

Private Sub CheckPCButton_Click()                                                                      
    Dim totRows As Long, i As Long
    totRows = Worksheets("Sheet4").Range("A1").CurrentRegion.Rows.Count

    For i = 2 To totRows
        If Trim(UCase(Sheet4.Cells(i, 1))) = Trim(UCase(TextBox22.Text)) Then           
            TextBox22.Text = Sheet4.Cells(i, 1)
            TextBox25.Text = Sheet4.Cells(i, 2)
            TextBox24.Text = Sheet4.Cells(i, 3)
            TextBox23.Text = "Approved"
        Else
            MsgBox "Not approved"

            Exit For
        End If 
    Next i

End Sub

Viewing all articles
Browse latest Browse all 88854