Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'1004': "The sort reference is not valid."

Tags:

excel

vba

I am trying to sort a range within a separate sheet. However, I keep getting this message:

 '1004': "The sort reference is not valid. Make sure it's within the data you want to sort, and the first Sort By box isn't the same or blank. 

I have checked the ranges and they all exist and are working.

The code is below:

Dim EmpBRange As String

EmpBRange = Sheets("EmployeeData").Cells(Cells.Rows.Count, "B").End(xlUp).Row

Worksheets("EmployeeData").Range("K3:K" & EmpBRange).Sort Key1:=Range("K3:K" & EmpBRange), Order1:=xlAscending, Header:=xlGuess, _
       OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
       DataOption1:=xlSortNormal

Thanks in advance

like image 494
Samantha Monti Avatar asked Mar 08 '13 14:03

Samantha Monti


1 Answers

I suspect you need to fully qualify the Key1 range, because you are calling the code from a different sheet:

Worksheets("EmployeeData").Range("K3:K" & EmpBRange).Sort Key1:=Worksheets("EmployeeData").Range("K3:K" & EmpBRange)

This is generally a good idea.

like image 79
Doug Glancy Avatar answered Sep 21 '22 18:09

Doug Glancy