Find text and show value if condition is met

I’ve got an answer that will find the first instance of USD and check if TOTAL PURCHASE is 9 rows below it, if found then copy the adjacent cell to D1, if not found then search for the next USD and repeat the checks:

Option Explicit

Sub vbavjezba()
Dim total As Variant
Dim usd As Variant
Dim rng As Range

Set rng = Worksheets(1).Range("A1:A21")
Set usd = Worksheets(1).Range("A1:A21").Find(what:="USD") 'Find the first instance of USD

repeatcheck:
If usd.Offset(9, 0).Value = "TOTAL PURCHASE" Then
    Worksheets(1).Range("D1").Value = usd.Offset(9, 1).Value
Else
    Set usd = rng.FindNext(After:=usd) 'Find the next instance of USD
    If Not usd Is Nothing Then 'IF found
        GoTo repeatcheck 'go back to repeatcheck to see if TOTAL PURCHASE is 9 rows below it
    Else
        MsgBox "Not Found!"
    End If
End If
End Sub

Leave a Comment