Forum Discussion

Linzer's avatar
Linzer
Copper Contributor
Jul 23, 2024
Solved

Macro not activating

Hi, I'm a VBA beginner and I'm trying to set up a macro to auto-sort the contents of the sheet "ranked masterlist" when the database in "deviation masterlist" (which the ranked masterlist references)...
  • HansVogelaar's avatar
    Jul 23, 2024

    Linzer 

    Do you update the data in the deviation masterlist sheet manually? If so, the code should work if it is placed in the worksheet module of that sheet (not in the worksheet module of the ranked masterlist sheet!)

    But I'd prefer to sort the ranked masterlist sheet when it is activated:

    Right-click the sheet tab of the ranked masterlist sheet.

    Select View Code from the context menu.

    Copy the following code into the worksheet module:

    Private Sub Worksheet_Activate()
        With Me.Sort
            .SortFields.Clear
            .SortFields.Add2 Key:=Range("B1"), SortOn:=xlSortOnValues, _
                    Order:=xlDescending, DataOption:=xlSortNormal
            .SortFields.Add2 Key:=Range("F1"), SortOn:=xlSortOnValues, _
                    Order:=xlDescending, DataOption:=xlSortTextAsNumbers
            .SetRange Range("B1").CurrentRegion
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub

Resources