NeedHelpmonkey
NeedHelpmonkey

Reputation: 15

VBA: Format and change cell value based on specific values in cell

I would like the below to change the value of the particular cell to "Rank" if two consecutive cells in the column are "#Name"

In addition, it would be helpful if the formatting of the cell could be red and font color white, while the value in the preceding cell would be nothing, and color blue

This is what I wrote but it isn't working, really appreciate guidance-

Sub FormatRankColmn()
'
' FormatRankColmn Macro
'

'
Dim cell As Range

For Each cell In Range("D1:D250").SpecialCells(xlCellTypeConstants)

If cell.Offset(-1, 0).Value = "#NAME" Then
    If cell.Value = "#Name" Then
        Range(cell).Value = "Rank"
    End If
End If
Next
End Sub

Upvotes: 1

Views: 872

Answers (1)

Pm Duda
Pm Duda

Reputation: 740

You need to check for cell in first row, otherwise this raise an error (no cell in row 0): cell.Offset(-1, 0)

I believe this is what you are after:

Sub FormatRankColmn()

Dim cell As Range

For Each cell In Range("D1:D250").SpecialCells(xlCellTypeConstants)

If cell.Value = "#NAME" And cell.Row <> 1 Then
    If cell.Offset(-1, 0).Value = "#NAME" Then
        cell.Value = "Rank"
    End If
End If
Next
End Sub

Upvotes: 1

Related Questions