Link to home
Start Free TrialLog in
Avatar of willie108
willie108

asked on

Runtime Error and macro setting "use relative references"

Hi. I was recording a macro and using the "use relative references" setting. I tried doing a sort with this setting but get a runtime error which highlights this code
 .SetRange ActiveCell.Offset(-1, 0).Range("A1:G3846")

any ideas about this?
    Range("A1").Select
    Application.CutCopyMode = False
    ActiveWorkbook.Worksheets("perStockTweets").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("perStockTweets").Sort.SortFields.Add Key:= _
        ActiveCell.Offset(0, 1).Range("A1:A3845"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("perStockTweets").Sort
        .SetRange ActiveCell.Offset(-1, 0).Range("A1:G3846")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

Open in new window

SOLUTION
Avatar of Elton Pascua
Elton Pascua
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of willie108
willie108

ASKER

Hi. If I record it without relative references and run it I don't get the error. Here it is
Sub Macro7()
'
' Macro7 Macro
'

'
    Sheets("perStockTweets").Select
    Range("A1").Select
    ActiveWorkbook.Worksheets("perStockTweets").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("perStockTweets").Sort.SortFields.Add Key:=Range( _
        "B2:B3846"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("perStockTweets").Sort
        .SetRange Range("A1:G3846")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, I said pivot table in one of the messages above but I am trying to sort
Try this workbook. Paste the data and corresponding headers then run the macro.
Sort.xlsm
Thanks.