Adapt code VBA to hide columns instead of rows

LordVoldetort_IV

New Member
Joined
Jun 10, 2021
Messages
19
Office Version
  1. 2016
Platform
  1. Windows
VBA Code:
Sub Hide_Rows()

With Worksheets("Tax Calculation")


'BeginRow = from which row you want to start checking

'EndRow = to which row you want to check

'ChkCol = in which collom is the value to check



BeginRow = 1

EndRow = 160

ChkCol = 19



For RowCnt = BeginRow To EndRow

If Worksheets("Tax Calculation").Cells(RowCnt, ChkCol).Value = "HIDE" Then

Worksheets("Tax Calculation").Cells(RowCnt, ChkCol).EntireRow.Hidden = True

Else: Worksheets("Tax Calculation").Cells(RowCnt, ChkCol).EntireRow.Hidden = False

End If

Next RowCnt

End With

 End Sub

Hello! the above code is what i use to hide a row based on a value in the same row, but different column. I want to switch it around and hide a column based on a value in the same column, different row. i cant seem to figure it out, would appreciate any help!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,265
Office Version
  1. 365
Platform
  1. Windows
Which column(s) & row(s)?
 

LordVoldetort_IV

New Member
Joined
Jun 10, 2021
Messages
19
Office Version
  1. 2016
Platform
  1. Windows
Which column(s) & row(s)?
I would like to hide columns A:H only if the value A100:H100 is "HIDE" . so basically, if B100 = "HIDE" then hide column B, but C100 = "SHOW" then column C should not be hidden
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,265
Office Version
  1. 365
Platform
  1. Windows
Ok, how about
VBA Code:
Sub LordVoldetort()
   Dim Cl As Range
   
   For Each Cl In Range("A100:H100")
      Cl.EntireColumn.Hidden = Cl.Value = "HIDE"
   Next Cl
End Sub
 
Solution

LordVoldetort_IV

New Member
Joined
Jun 10, 2021
Messages
19
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

Ok, how about
VBA Code:
Sub LordVoldetort()
   Dim Cl As Range
  
   For Each Cl In Range("A100:H100")
      Cl.EntireColumn.Hidden = Cl.Value = "HIDE"
   Next Cl
End Sub
Works great, thank you!
 

LordVoldetort_IV

New Member
Joined
Jun 10, 2021
Messages
19
Office Version
  1. 2016
Platform
  1. Windows
Ok, how about
VBA Code:
Sub LordVoldetort()
   Dim Cl As Range
  
   For Each Cl In Range("A100:H100")
      Cl.EntireColumn.Hidden = Cl.Value = "HIDE"
   Next Cl
End Sub
If i want to do the reverse, (to unhide those same columns), how would i go about that?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,265
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Like
VBA Code:
   Range("A:H").EntireColumn.Hidden = False
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,265
Office Version
  1. 365
Platform
  1. Windows
You're welcome.
 

Forum statistics

Threads
1,141,127
Messages
5,704,442
Members
421,349
Latest member
Santhosh3188

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
Top