Search code examples
excelexcel-2007vba

Need to run a macro based on values in a column


More specifically, what I need is to look up all the unique values in a column (these values are number representations of the months of the year) and use the value(s) returned to create a pop up box allowing the user to choose which macro to run. As an example, I import report data into a worksheet that contains 100 records. In column B we will see 11's and 12's representing November and December. What I am looking to do is capture those two numbers and use that to allow the user to run the code for November OR December. I have added code that I use for November as an example.

Sub Extract_Sort_1511_November()
'
'

' This line renames the worksheet to "Extract"
Application.ScreenUpdating = False
    ActiveSheet.Name = "Extract"

' This line autofits the columns C, D, O, and P
    Range("C:C,D:D,O:O,P:P").Select
    Range("P1").Activate
    Selection.Columns.AutoFit
    Range("A2").Select

' This unhides any hidden rows
Cells.Select
    Selection.EntireRow.Hidden = False
    Range("A2").Select

' Want to alter the code below to perform a query in column B to determine
' which months are listed from the import, then use the results in a dialog box to
' allow the user to choose which month to view

Dim LR As Long

    For LR = Range("B" & Rows.Count).End(xlUp).Row To 2 Step -1
        If Range("B" & LR).Value <> "11" Then
            Rows(LR).EntireRow.Hidden = True
        End If
    Next LR

With ActiveWorkbook.Worksheets("Extract").Sort
    With .SortFields
        .Clear
        .Add Key:=Range("B2:B2000"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    End With
    .SetRange Range("A2:Z2000")
    .Apply
End With
Cells.WrapText = False
Sheets("Extract").Range("A2").Select
Application.ScreenUpdating = True
End Sub

Solution

  • Found a quicker way to achieve the true goal for this issue. Changed the order of the sort and hide sections. This now produces the desired results.

    Sub Extract_Sort_1511_November()
    '
    '
    
    ' This line renames the worksheet to "Extract"
    Application.ScreenUpdating = False
        ActiveSheet.Name = "Extract"
    
    ' This line autofits the columns C, D, O, and P
        Range("C:C,D:D,O:O,P:P").Select
        Range("P1").Activate
        Selection.Columns.AutoFit
        Range("A2").Select
    
    ' This unhides any hidden rows
    Cells.Select
        Selection.EntireRow.Hidden = False
        Range("A2").Select
    
    ' Want to alter the code below to perform a query in column B to determine
    ' which months are listed from the import, then use the results in a dialog box to
    ' allow the user to choose which month to view
    
    Dim LR As Long
    
    With ActiveWorkbook.Worksheets("Extract").Sort
        With .SortFields
            .Clear
            .Add Key:=Range("B2:B2000"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        End With
        .SetRange Range("A2:Z2000")
        .Apply
    End With
    
        For LR = Range("B" & Rows.Count).End(xlUp).Row To 2 Step -1
            If Range("B" & LR).Value <> "11" Then
                Rows(LR).EntireRow.Hidden = True
            End If
        Next LR
    
    Cells.WrapText = False
    Sheets("Extract").Range("A2").Select
    Application.ScreenUpdating = True
    End Sub