• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Having a brain fail - trying to identify and format specific values with VBA

PipBoy808

Member
I'm not having my brightest morning and can't seem to get around a simple problem I have.
I have a range of data in column B that I want Excel to cycle through. There are three specific values it needs to identify, and subsequently format those entire rows differently.

I've written this:

Code:
Dim j as long

For j = 12 To lastRow 'lastrow has been previously defined and is correct
If Cells(j, 2).Value = "Header 1" Or "Header 2" Or "Header 3" Then 'I keep getting an error here
Cells(j, "B").RowHeight = 25
Cells(j, "B").WrapText = False
Cells(j, "B").Font.Bold = True
Rows(j, "B").Font.Color = vbWhite
Rows(j, "B").Color = vbBlack 'black out the entire row to signify a header
End If
Next j
End With

I know the answer is staring me in the face, but can anyone help me out?
 
Pipboy

Your code is erroneous because you start by saying;

Code:
If Cells(j, 2).Value = "Header 1"

then you don't qualify the second part by saying Or Cells(j,2) = "Header 2"

The part in bold is what you are leaving out.

You need to do it like this.

Code:
If Cells(1, 2) = "Header 1" Or Cells(1, 2) = "Header 2" Or Cells(1, 2) = "Header 3" Then

Take care

Smallman
 
You can try like this also..

If Evaluate("=OR(" & Cells(j, 2).Address & "={""Header 1"",""Header 2"",""Header 3""})") Then
 
One more approach using Select
Code:
Select Case Cells(j, 2).Value
  Case "Header 1", "Header 2", "Header 3"
  'Your code here
End Select
 
Back
Top