Code to find value based on dropdown selection

bemisdt

New Member
Joined
Nov 13, 2012
Messages
30
Hope someone can help...

I have a dropdown list of employee names to filter a matrix-style spreadsheet. On selection, all columns except the selected are hidden.

My problem is that every time I add a new employee, I have to modify the code to match the name to a specific column. I need code that will search the columns in a range for the value from the dropdown, then hide all other columns. Here is a shot of my current code:

Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Range("$E$2").Value
Case Is = "ALL OPERATORS"
Columns("F:AW").EntireColumn.Hidden = False
Range("Table12LL").Select
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
Range("A1").Select
Case Is = "Al Castro"
Columns("F:F").EntireColumn.Hidden = False
Columns("G:AW").EntireColumn.Hidden = True
Case Is = "Austin Furrow"
Columns("G:G").EntireColumn.Hidden = False
Columns("F:F").EntireColumn.Hidden = True
Columns("H:AW").EntireColumn.Hidden = True
Case Is = "Bobby Dykman"
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Assuming that your employee names are located in row 1 and that the first employee name starts at F1.
Code:
Sub mySelection()
     myDropDownListValue = Range("E2").Value
     firstColumn = 6
     lastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
     Columns(firstColumn & ":" & lastColumn).Hidden = False
     If myDropDownListValue = "ALL OPERATORS" Then
          Exit Sub
     End If
     c = firstColumn
     Do Until c > lastColumn
          myValue = Cells(1, c).Value
          If myValue = myDropDownListValue Then
               Exit Do
          End If
          c = c + 1
     Loop
     If c > lastColumn Then
          Exit Sub
     End If
     dontHideThisColumn = c
     c = firstColumn
     Do Until c > lastColumn
          If c <> dontHideThisColumn Then
               Columns(c).Hidden = True
          End If
          c = c + 1
     Loop
End Sub
 
Last edited:
Upvote 0
Thanks for the response, however nothing happens when I select from the dropdown. The names begin in F9, so I changed the 1 to 9 here... [myValue = Cells(9, c).Value] assuming it was referencing the row. Any ideas?

I wanted to send the file, but I'm sorry, I don't know how on this site.

Thanks for the help
 
Upvote 0
Code:
Sub mySelection()
     myDropDownListValue = Range("E2").Value
     firstColumn = 6
     lastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
     firstColumnLetter = ColumnLetter(firstColumn)
     lastColumnLetter = ColumnLetter(lastColumn)
     Columns(firstColumnLetter & ":" & lastColumnLetter).Hidden = False
     If myDropDownListValue = "ALL OPERATORS" Then
          Exit Sub
     End If
     c = firstColumn
     Do Until c > lastColumn
          myValue = Cells(9, c).Value
          If myValue = myDropDownListValue Then
               Exit Do
          End If
          c = c + 1
     Loop
     If c > lastColumn Then
          Exit Sub
     End If
     dontHideThisColumn = c
     c = firstColumn
     Do Until c > lastColumn
          If c <> dontHideThisColumn Then
               Columns(c).Hidden = True
          End If
          c = c + 1
     Loop
End Sub

Function ColumnLetter(ColumnNumber As Long) As String
    Dim n As Long
    Dim c As Byte
    Dim s As String

    n = ColumnNumber
    Do
        c = ((n - 1) Mod 26)
        s = Chr(c + 65) & s
        n = (n - c) \ 26
    Loop While n > 0
    ColumnLetter = s
End Function
 
Upvote 0

Forum statistics

Threads
1,215,757
Messages
6,126,694
Members
449,331
Latest member
smckenzie2016

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