VBA: Hide All Unspecified Columns

canyon

New Member
Joined
Jan 5, 2022
Messages
24
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 show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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
 
Upvote 0
Solution
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.
 
Upvote 0
So, did my code suggestion work for you, and do you still have issues?
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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