‘1004’: “The sort reference is not valid.”

calendar_today Asked Mar 8, 2013
thumb_up 13 upvotes
history Updated April 14, 2026

Direct Answer

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…. This is a prose walkthrough, ranked #36th of 95 by community upvote score, from 2013.


The Problem (Q-score 8, ranked #36th of 95 in the VBA Core archive)

The scenario as originally posted in 2013

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

Why this Range / Worksheet targeting trips people up

The question centers on reaching a specific cell, range, or workbook object. In VBA Core, this is the #1 source of failures after activation events: every property (.Value, .Formula, .Address) behaves differently depending on whether the parent Workbook is explicit or implicit.


The Verified Solution — solid answer (above median) (+13)

Verbal answer — walkthrough without a code block

Note: the verified answer is a prose walkthrough. If you need a runnable sample, check VBA Core entries ranked in the top 10 of the same archive.

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.


When to Use It — classic (2013–2016)

Ranked #36th in its category — specialized fit

This pattern sits in the 89% tail relative to the top answer. Reach for it when your scenario closely matches the question title; otherwise browse the VBA Core archive for a higher-consensus alternative.

What changed between 2013 and 2026

The answer is 13 years old. The VBA Core object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.

help
Frequently Asked Questions

Is this above-median answer still worth copying?
expand_more

Answer score +13 vs the VBA Core archive median ~4; this entry is solid. The score plus 8 supporting upvotes on the question itself (+8) means the asker and 12 subsequent voters all validated the approach.

The answer has no code block — how do I turn it into a snippet?
expand_more

Use the walkthrough above as a checklist, then open a top-10 VBA Core archive entry for a concrete starting template you can adapt.

Published around 2013 — what’s changed since?
expand_more

Published 2013, which is 13 year(s) before today’s Office 2026 build. The VBA Core object model has had no breaking changes in that window. Three things to re-test: (1) blocked macros on downloaded files (Mark-of-the-Web), (2) 64-bit API declarations (PtrSafe, LongPtr), (3) any shift toward Office Scripts for web scenarios.

Which VBA Core pattern ranks just above this one at #35?
expand_more

The pattern one rank above is “Get the content of a sharepoint folder with Excel VBA”. If your use case overlaps, compare both before committing.

Data source: Community-verified Q&A snapshot. Q-score 8, Answer-score 13, original post 2013, ranked #36th of 95 in the VBA Core archive. Last regenerated April 14, 2026.

vba