Using “If cell contains” in VBA excel

后端 未结 6 2406
孤独总比滥情好
孤独总比滥情好 2020-12-10 16:55

I\'m trying to write a macro where if there is a cell with the word \"TOTAL\" then it will input a dash in the cell below it. For example:

6条回答
  •  被撕碎了的回忆
    2020-12-10 17:37

    This will loop through all cells in a given range that you define ("RANGE TO SEARCH") and add dashes at the cell below using the Offset() method. As a best practice in VBA, you should never use the Select method.

    Sub AddDashes()
    
    Dim SrchRng As Range, cel As Range
    
    Set SrchRng = Range("RANGE TO SEARCH")
    
    For Each cel In SrchRng
        If InStr(1, cel.Value, "TOTAL") > 0 Then
            cel.Offset(1, 0).Value = "-"
        End If
    Next cel
    
    End Sub
    

提交回复
热议问题