Sort leading to debug error... any thoughts?

braindiesel

Well-known Member
Joined
Mar 16, 2009
Messages
520
Office Version
365, 2019, 2010
Platform
Windows
I have the following code working as part of a macro without any issue... of course, when I get to my client, I get a debug error...
The topRow and botRow variables are storing numbers and in debug mode, when I hover over them, the correct numbers are showing.
Any thoughts on a better way to write this or am I missing something?

ActiveWorkbook.Worksheets("REPORT5").Sort.SortFields.Add2 Key:=Range( _
"D" & topRow & ":D" & botrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("REPORT5").Sort.SortFields.Add2 Key:=Range( _
"E" & topRow & ":E" & botrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
 

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,746
Office Version
2010
Platform
Windows
There is no Add2 method, and if REPORT5 is not the active sheet you'll get a run-time error.

Code:
  With Worksheets("REPORT5")
    .Sort.SortFields.Add Key:=.Cells(toprow, "D")
    .Sort.SortFields.Add Key:=.Cells(toprow, "E")
  End With
 

Watch MrExcel Video

Forum statistics

Threads
1,098,860
Messages
5,465,115
Members
406,414
Latest member
Discorz

This Week's Hot Topics

Top