hiding columns using a drop down menu and list of column names in a range.

Joined
Mar 22, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello!

I'm relatively new to VBA and working on some existing code.

At this time, the way it works is the user will pick option from a drop down menu (ie. compact, medium, long, etc) and the code will look in the list/range for the column name then hide it.

Currently the code works in a way that is backwards for the end user. If the column name doesn't appear in the list/range, then the code will not hide it. I've racked my brain trying to figure out how to reverse it to be more user friendly (ie. If the column name is in the list/range then keep, if you don't want it then hide)

Thank you!



Sub FilterComboBox_Change()


Dim xRange As Range
Dim ColumnName As String

Application.ScreenUpdating = False

ActiveSheet.Unprotect

Set xRange = Range("Bid")

'this is the databodyrange where the column names are housed
Set tblSource = ActiveWorkbook.Sheets("HelperTab").ListObjects("View").DataBodyRange
tSourceRows = tblSource.Rows.Count

Set tblSourceFilter = ActiveWorkbook.Sheets("HelperTab").ListObjects("Name").DataBodyRange

strSelectedItem = ActiveSheet.DropDowns("FilterComboBox").value
strSelectedFilter = tblSourceFilter(strSelectedItem, 1)

If strSelectedItem > 1 Then

Columns.EntireColumn.Hidden = False
For tRow = 1 To tSourceRows
ColumnName = tblSource(tRow, tblSource.ListObject.ListColumns(strSelectedFilter).index)
If ColumnName <> "" Then

On Error GoTo DoesNotExist
TNT = xRange.ListObject.HeaderRowRange.Cells.Find(ColumnName, lookat:=xlWhole)

xRange.ListObject.HeaderRowRange.Cells.Find(ColumnName, lookat:=xlWhole).EntireColumn.Hidden = True

DoesNotExist:

End If
Next tRow
End If

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,214,813
Messages
6,121,705
Members
449,048
Latest member
81jamesacct

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