having trouble with combining 2 "IF" statements

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
301
I have these 2 statements:

Code:
For Each Cell In Range(Cells(4, 6), Cells(4, lCol))
If Cell = "a" Then Columns(Cell.Column).Hidden = True Else Columns(Cell.Column).Hidden = False
Next

Code:
For Each Cell In Range(Cells(5, 6), Cells(5, lCol))
If Cell = "R" Then Columns(Cell.Column).Hidden = True Else Columns(Cell.Column).Hidden = False
Next

The first one is to look in row 4 and any cells that have "a" in them then hide that column.
The second is to look in row 5 and any cells that have "R" in them, then hide those columns.

How can I make it so that it is looking at both row 4 for "a" 's, and row 5 for "R" 's and hides all of those columns that meet that criteria?
 

Some videos you may like

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,303
Office Version
  1. 365
Platform
  1. Windows
How about
Code:
   For Each Cell In Range(Cells(4, 6), Cells(4, lCol))
     Columns(Cell.Column).Hidden = IIf(Cell.Value = "a" And Cell.Offset(1).Value = "R", True, False)
   Next
 

wideboydixon

Well-known Member
Joined
Jun 2, 2016
Messages
3,401
Code:
For thisCol = 6 To lCol
    Columns(thisCol).Hidden = Cells(4, lCol).Value = "a" And Cells(5, lCol).Value = "R"
Next

WBD
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,303
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,108,973
Messages
5,525,988
Members
409,673
Latest member
Riseee

This Week's Hot Topics

Top