VBA HIde/Unhide Columns Based on Value

gsimons85

New Member
Joined
Apr 1, 2014
Messages
32
Good evening

I'm trying to hide some columns based on an IF statement in row. I used a previous post (http://www.mrexcel.com/forum/excel-...ications-hide-unhide-columns-based-value.html) and got the following formula to hide "0" :

Sub Macro1()

Dim i As Long

Application.ScreenUpdating = False

For i = Cells(1, Columns.Count).End(xlToLeft).Column To 1 Step -1
If Cells(1, i) = 0 Then Cells(1, i).EntireColumn.Hidden = True
Next i

Application.ScreenUpdating = True

End Sub

This works fine if my data set starts in A1.

I'd like to edit this formula to start in a different row (e.g. Row 5 or Row 87) and to look for a different value besides 0 (e.g. A or 3).

I'm sure this is an easy fix, but I'm not the brightest in VBA terms :eeek:

Thanks for the assistance
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
structure for Cell argument is

Cells(Row number, Column number)

so in above code you should replace Cell(1, column number) with Cell (Your row number 5 or 87, column number)


 
Upvote 0
Good evening

I'm trying to hide some columns based on an IF statement in row. I used a previous post (http://www.mrexcel.com/forum/excel-...ications-hide-unhide-columns-based-value.html) and got the following formula to hide "0" :

Sub Macro1()

Dim i As Long

Application.ScreenUpdating = False

For i = Cells(1, Columns.Count).End(xlToLeft).Column To 1 Step -1
If Cells(1, i) = 0 Then Cells(1, i).EntireColumn.Hidden = True
Next i

Application.ScreenUpdating = True

End Sub

This works fine if my data set starts in A1.

I'd like to edit this formula to start in a different row (e.g. Row 5 or Row 87) and to look for a different value besides 0 (e.g. A or 3).

I'm sure this is an easy fix, but I'm not the brightest in VBA terms :eeek:

Thanks for the assistance


Try:

Starts with Row 5, with value "A"
Code:
[COLOR=#ff0000]For i = Cells(5, Columns.Count).End(xlToLeft).Column To 1 Step -1
If Cells(1, i) = "A" Then Cells(1, i).EntireColumn.Hidden = True
Next i

[/COLOR]Starts with Row 87, with value 3
Code:
[COLOR=#ff0000]For i = Cells(87, Columns.Count).End(xlToLeft).Column To 1 Step -1
If Cells(1, i) = 3 Then Cells(1, i).EntireColumn.Hidden = True
Next i

[/COLOR]Change the code accordingly to your requirements.

Cheers
Rich
 
Upvote 0

Forum statistics

Threads
1,223,779
Messages
6,174,492
Members
452,567
Latest member
ONEIL290

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top