anyroomforalittlechang
New Member
- Joined
- Mar 22, 2022
- Messages
- 4
- Office Version
- 365
- Platform
- 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
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