Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50068

SORT Method difficulty

$
0
0
:confused:I have a workbook with a worksheet named Constants. It has a Named range called ClinicTable. The named range is currently $A$9:$E$48. I am writing a macro to sort this range by column A. Since I am not a wizard at invoking the sort method with all its parameters, I used "Record Macro". I ran the macro and it worked fine. I knew that while recording the macro, the range I selected to sort would show in the recorded macro as a constant range (namely, $A$9:$E$48). My plan was then to replace all the references to $A$9:$E$48 with “ClinicTable”. (The ClinicTable will expand during other operations in the application). I did this replacement (and haven’t even changed the ClinicTable dimensions) , but now the Sort method fails for me with the following message:

" Sort Reference is not valid. Make sure that it's within the data you want to sort, and the first sort by box isn't the same or blank."

Here’s the macro as recorded:

Code:

Sub Macro4()
'
' Macro4 Macro
'

'
    Range("A9:E48").Select
    ActiveWorkbook.Worksheets("Constants").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Constants").Sort.SortFields.Add Key:=Range( _
        "A9:A48"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Constants").Sort
        .SetRange Range("A9:E48")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

And here’s the altered macro that fails:


Code:

Sub Macro4()
'
' Macro4 Macro
'

'
    Range("ClinicTable").Select
    ActiveWorkbook.Worksheets("Constants").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Constants").Sort.SortFields.Add Key:=Range( _
        "ClinicTable"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Constants").Sort
        .SetRange Range("ClinicTable")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

I don't understand the message and I don't understand many of the parameters. I can't spot the parameter that says column A is the sort field. Can someone help me please.

Viewing all articles
Browse latest Browse all 50068

Trending Articles