why this error? (code related to a sorting execution)

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
458
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
VBA Code:
Dim rCol2 As Long
rCol2 = ActiveWorkbook.Worksheets("ANALYTICS").UsedRange.Columns.Count
'
ActiveWorkbook.Worksheets("ANALYTICS").Activate
'
ActiveWindow.SmallScroll Down:=-48
Application.CutCopyMode = False
'
ActiveWorkbook.Worksheets("ANALYTICS").Range(Cells(1, 13), Cells(rCol2, 14)).Select
ActiveWorkbook.Worksheets("ANALYTICS").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("ANALYTICS").Sort.SortFields.Add2 Key:=Range(Cells(1, 13), _
    Cells(rCol2, 14)), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
'
With ActiveWorkbook.Worksheets("ANALYTICS").Sort
    .SetRange Range(Cells(1, 13), Cells(20, 14))
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With


So all of this works fine, except for the very last section. If I take this out, it runs without incident. If I leave it in, I get : "Run-time error '1004': Application-defined or object-defined error

VBA Code:
With ActiveWorkbook.Worksheets("ANALYTICS").Sort
    .SetRange Range(Cells(1, 13), Cells(20, 14))
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With


Anyone have any ideas on why this would be? I used the exact same code in several other places within the workbook with no issues (primarily as a workbook open event where I have it 'refresh' data at the top of a worksheet that gets captured, tallied and sorted.)

This code is inside a module that is directed to from a userform code.

Thanks for any suggestions.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Sorry, but I forgot to point out where the code stops with the error and what specific line is being highlighted.

I only discovered that the sorting code (this)

VBA Code:
With ActiveWorkbook.Worksheets("ANALYTICS").Sort
    .SetRange Range(Cells(1, 13), Cells(20, 14))
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

is what is responsible for the error and what is tripping it up because if I remove just that section, then the entire code (the userform code and the 1 module that runs) runs without incident.

At the bottom of this screen shot you can see where it calls on the module to run ('call IncidentTally'.)

Why it bounces back to the userform and highlights this line, I dont know or understand. ?‍♂️

Capture.PNG
 
Upvote 0
Thanks, I got it figured out.

I had to change how I had the range listed from this:

VBA Code:
ActiveWorkbook.Worksheets("ANALYTICS").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("ANALYTICS").Sort.SortFields.Add2 Key:=Range(Cells(1, 13), _
    Cells(rCol2, 14)), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal

to like this:

VBA Code:
ActiveWorkbook.Worksheets("ANALYTICS").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("ANALYTICS").Sort.SortFields.Add Key:=Range("M" & Rows.Count).End(xlUp), _
    SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,937
Members
449,196
Latest member
Maxkapoor

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