Home » excel » vba – If a cell inside a range is the same as a string, do calculation in the cell under it

vba – If a cell inside a range is the same as a string, do calculation in the cell under it

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to write up a macro in VBA.
I want my macro to check if a value inside a range of cells is the same as the value of another cell. Then i want it to identify the cell and do something in the cell under that one.

So, i want to check if the range B2:M6 has a cell with the same value as A1. Is this is true for cell D6, i want to perform a calculation on the cell under it, in this case B7.

For the moment i´ve only achieved to check the values inside the range but now i dont know what to do.

Sub CellCheck()
Dim DateString As String
DateString = Range("A1")

    result = IsNumeric(WorksheetFunction.Match(DateString, Range("B6:M6"), 0))
    If result = True Then MsgBox "It is True"
End Sub
How to&Answers:

You could use .FIND() or a simple loop:

Sub FindIt()
    Dim V As Variant, rBig As Range, r As Range
    V = Range("A1").Value
    Set rBig = Range("B2:M6")
    For Each r In rBig
        If r.Value = V Then
            r.Offset(1, 0) = "XXX"
        End If
    Next r
End Sub