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
 
Holy crap! got this going & keep saying, "I can't believe this!" this is AMAZING...MANY THANKS!
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
HUGE APOLOGY for coming back again about this but it is incredibly useful & has become a "go to" function at our shop. One request that is consistently asked is "can a 2nd input box be added so 2 variables can be searched?" I've been messing around with it & not having any luck. If possible, would you be willing to see if adding this would be relatively painless to do? If it's too much, please say so. What I've done is added the additional field to the input box in the attached file. From my limited programming knowledge, this would be like an "AND" condition (?)
Thank you again for considering this!

 
Upvote 0
What I've done is added the additional field to the input box in the attached file.
By "input box" are you referring to the list box in the userform? Do you want to be able to select more than one column in the userform listbox and show/hide columns based on the multiple selections? Please explain in detail using a few examples.
 
Upvote 0
Sorry for not being clear...attached is a screenshot of the userform where I added a 2nd input box outlined in yellow. The last update you made was the input box under the "Filter Header by" button which allowed for searches using wildcards like "*car*" or "*age*". I've had many requests for an additional field that would allow a similar search that would allow for "*car*" AND "*age*".
 

Attachments

  • x.jpg
    x.jpg
    231.7 KB · Views: 2
Upvote 0
Replace this line of code:
VBA Code:
If rng Like "*" & TextBox1.Value Then
with this line:
VBA Code:
If rng Like "*" & TextBox1.Value & "*" Or rng Like "*" & TextBox2.Value & "*" Then
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,582
Members
449,039
Latest member
Arbind kumar

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