VBA: Hide All Unspecified Columns

canyon

New Member
Joined
Jan 5, 2022
Messages
23
Office Version
  1. 2019
Platform
  1. Windows
I was able to find and write code which hides all specified columns for a report at work that I am slowly trying to automate. The report in question has 20 or so columns and I only really need 8 from the report.

I can easily use the code to write 12 or so lines to hide them. However it seems that it would easier to read and much more efficient to write the inverse of that code. I am fairly new to VBA and this is my first real project I am trying to utilize the language for so its been a mixture of trial and error.

Current Project:

VBA Code:
Sub Hide_Me()

Dim Lastcolumn As Long
Lastcolumn = Cells(3, Columns.Count).End(xlToLeft).Column

For i = 1 To Lastcolumn

If Cells(3, i).Value = "Customer Name" Or Cells(3, i).Value = "Address" Or Cells(3, i).Value = "Phone Number" or ... 'continue to perpetuity
Then Columns(i).Hidden = true

Next

End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
65,556
Office Version
  1. 365
Platform
  1. Windows
Welcome to the Board!
I can easily use the code to write 12 or so lines to hide them. However it seems that it would easier to read and much more efficient to write the inverse of that code.

So, do you mean rather than checking to see if the cell is equal to a certain value, do you mean to check to see that it is NOT a certain value?
If so, you not only need to change the "=" to "<>", but you also need to change the "Or" to "And", i.e.
VBA Code:
If Cells(3, i).Value <> "Customer Name" And Cells(3, i).Value <> "Address" And Cells(3, i).Value <> "Phone Number" And ... 'continue to perpetuity
 
Solution

canyon

New Member
Joined
Jan 5, 2022
Messages
23
Office Version
  1. 2019
Platform
  1. Windows
Welcome to the Board!


So, do you mean rather than checking to see if the cell is equal to a certain value, do you mean to check to see that it is NOT a certain value?
If so, you not only need to change the "=" to "<>", but you also need to change the "Or" to "And", i.e.
VBA Code:
If Cells(3, i).Value <> "Customer Name" And Cells(3, i).Value <> "Address" And Cells(3, i).Value <> "Phone Number" And ... 'continue to perpetuity

Yes that's exactly what I'm trying to do, if the cell value/text doesn't match the parameters then I want them to be hidden.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
65,556
Office Version
  1. 365
Platform
  1. Windows
So, did my code suggestion work for you, and do you still have issues?
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
65,556
Office Version
  1. 365
Platform
  1. Windows
You are welcome.
Glad I was able to help!
 

Forum statistics

Threads
1,176,513
Messages
5,903,481
Members
435,032
Latest member
crone123456

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