VBA to display specific columns based on a header in a range...

sadams1

Board Regular
Joined
Aug 19, 2006
Messages
219
How much of a stretch would it be to take the following range (A1:E4) with the header row (A1:E1) & have code that would display specific columns based on a header value? For instance, "show only the column with 'Banana' in the header" or "show only columns with 'Apple' and 'Banana' in the header row".

I have the following but it only selects the columns & doesn't hide the others.

Sub FindAddressColumn()
Dim xRg As Range
Dim xRgUni As Range
Dim xFirstAddress As String
Dim xStr As String
On Error Resume Next
xStr = "Name"
Set xRg = Range("A1:P1").Find(xStr, , xlValues, xlWhole, , , True)
If Not xRg Is Nothing Then
xFirstAddress = xRg.Address
Do
Set xRg = Range("A1:P1").FindNext(xRg)
If xRgUni Is Nothing Then
Set xRgUni = xRg
Else
Set xRgUni = Application.Union(xRgUni, xRg)
End If
Loop While (Not xRg Is Nothing) And (xRg.Address <> xFirstAddress)
End If
xRgUni.EntireColumn.Select
End Sub



Untitled.png
 
Some of the cells in row 2 are not formatted as "General". For example, L2 is formatted as "Accounting". It looks like this causes a problem for the macro when trying to find the header "len". Highlight the entire row 2 and format it as "General" and try the macro again. Also, I suggest you delete all the unnecessary rows since they slow everything down including saving and opening the file. You also have some duplicate column headers in row 2 which will be a problem.
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Grrrrrr...sorry about not checking that. Otherwise, this is it! Thank you very much for going through all this.
 
Upvote 0
hello again! Sorry to bother but after using this for the past 2 years, I made some changes to the workbook & now it throws an error about "subscript out of range" with the "userform". Does this sound like something fixable?
 
Upvote 0
Please upload an updated file (not a picture) and explain in detail what the problem is.
 
Upvote 0
I kept messing around & found the reference I'd managed to screw up so sorry to bother! I use this on a regular basis even 2 years later!
 
Upvote 0
I changed the marked solution post with the actual answer in this thread.

@sadams1: In your future questions, please mark the post that answered your question as it will help future readers. If you post your own solution, it is perfectly fine to mark your own solution as the solution. No further action is required in this thread.
 
Upvote 0
I changed the marked solution post with the actual answer in this thread.

@sadams1: In your future questions, please mark the post that answered your question as it will help future readers. If you post your own solution, it is perfectly fine to mark your own solution as the solution. No further action is required in this thread.
my apologies....thought I'd marked it but it should be now.
 
Upvote 0
my apologies....thought I'd marked it but it should be now.
You keep marking your own feedback post as the solution.

Once again, I am marking the actual answer to this question. No further action is required in this question.
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,667
Members
449,462
Latest member
Chislobog

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