Macro to hide a column based on criteria

dtryon

New Member
Joined
Feb 26, 2019
Messages
3
I need help trying to auto hide a column based on a group of cells

for example i need it to hide column J if all cells J19:J200 return blanks.

Thank you for any help
 

Some videos you may like

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
9,598
Try:
Code:
Sub HideCol()
    If WorksheetFunction.CountA(Range("J19:J200")) = 0 Then Columns("J").Hidden = True
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,659
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
Maybe
Code:
Sub dtryon()
Columns(10).Hidden = Application.CountA(Range("J19:J200")) = 0
End Sub
 

dtryon

New Member
Joined
Feb 26, 2019
Messages
3
Thank you, this works if i delete my formulas out of the cells, is there a way to keep the formulas and have it hide based on if the formula returns a blank?
 

Fluff

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

ADVERTISEMENT

How about
Code:
Sub dtryon()
Columns(10).Hidden = Application.CountBlank(Range("J19:J200")) = 182
End Sub
 

dtryon

New Member
Joined
Feb 26, 2019
Messages
3
How about
Code:
Sub dtryon()
Columns(10).Hidden = Application.CountBlank(Range("J19:J200")) = 182
End Sub


Awesome that worked. Thank you.

One last question. now if i wanted to use that for multiple columns would i just past it over and over replacing column number and the range? and how do i get it to run auto...thanks again for the help. i am very early in my VBA learning
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,659
Office Version
  1. 365
Platform
  1. Windows
Which columns would you want it to work on & would it be the same rows each time?
 

Watch MrExcel Video

Forum statistics

Threads
1,109,550
Messages
5,529,472
Members
409,884
Latest member
Msinmath
Top