SOLVED
Home

Mystery error happens randomly, but more often when running from a formcontrol button...

%3CLINGO-SUB%20id%3D%22lingo-sub-880100%22%20slang%3D%22en-US%22%3EMystery%20error%20happens%20randomly%2C%20but%20more%20often%20when%20running%20from%20a%20formcontrol%20button...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-880100%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3ESo%20I%20have%20a%20macro%20that%20creates%20sheets%2C%20transfers%20data%20to%20them%2C%20and%20then%20cleans%20it.%20All%20is%20well%20and%20good%2C%20but%20I%20often%20get%20the%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CI%3E'Runtime%20Error%201004%3A%20The%20sort%20reference%20isn't%20valid.%20Make%20sure%20that%20it's%20within%20the%20data%20you%20want%20to%20sort%2C%20and%20the%20first%20Sort%20By%20box%20isn't%20the%20same%20or%20blank'%3C%2FI%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3Eerror%20when%20I'm%20trying%20to%20sort%20the%20data.%20At%20the%20start%20of%20the%20macro%2C%20it%20checks%20whether%20specific%20sheets%20exist%20in%20the%20data%20workbook%20(not%20the%20one%20running%20the%20macro)%20and%20deletes%20them%20if%20they%20do%2C%20before%20creating%20new%20ones.%20I've%20noticed%20about%2050%25%20of%20the%20time%20I%20get%20this%20error%20when%20trying%20to%20sort%20the%20data%20in%20the%20sheets%3A%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Ews.Range(%22A1%3AZ200%22).Sort%20Key1%3A%3DColumns(%22A%22)%2C%20Order1%3A%3DxlAscending%2C%20Header%3A%3DxlNo%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3EBut%20when%20I%20delete%20the%20sheets%20manually%20before%20running%20the%20macro%20again%2C%20it%20always%20works.%20The%20weirdest%20part%20is%20that%20I%20added%20a%20formcontrol%20button%20to%20run%20the%20macro%2C%20but%20when%20I%20use%20it%2C%20I%20get%20the%20error%20message%20every%20single%20time.%20I've%20also%20tried%20clearing%20the%20sheet's%20contents%20instead%20of%20deleting%20it%20but%20I%20get%20the%20same%20results.%20Anyone%20seen%20inconsistent%20mystery%20errors%20before%20and%20know%20what%20I%20can%20do%20to%20fix%20it%3F%20Thanks%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-880100%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-880299%22%20slang%3D%22en-US%22%3ERe%3A%20Mystery%20error%20happens%20randomly%2C%20but%20more%20often%20when%20running%20from%20a%20formcontrol%20button...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-880299%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F338348%22%20target%3D%22_blank%22%3E%40seanrm100_%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20should%20qualify%20the%20key%20with%20the%20Sheet%20reference%20like%20this...%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Ews.Range(%22A1%3AZ200%22).Sort%20Key1%3A%3Dws.Columns(%22A%22)%2C%20Order1%3A%3DxlAscending%2C%20Header%3A%3DxlNo%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOr%20more%20precisely%20this...%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Ews.Range(%22A1%3AZ200%22).Sort%20Key1%3A%3Dws.Range(%22A1%22)%2C%20Order1%3A%3DxlAscending%2C%20Header%3A%3DxlNo%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20should%20resolve%20the%20issue.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-882440%22%20slang%3D%22en-US%22%3ERe%3A%20Mystery%20error%20happens%20randomly%2C%20but%20more%20often%20when%20running%20from%20a%20formcontrol%20button...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-882440%22%20slang%3D%22en-US%22%3EWow%2C%20that's%20what%20I%20get%20for%20copying%20and%20pasting%20code%2C%20I%20can't%20believe%20I've%20been%20I've%20been%20dealing%20with%20this%20for%20weeks%20over%20something%20so%20silly.%20Thanks!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-882513%22%20slang%3D%22en-US%22%3ERe%3A%20Mystery%20error%20happens%20randomly%2C%20but%20more%20often%20when%20running%20from%20a%20formcontrol%20button...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-882513%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F338348%22%20target%3D%22_blank%22%3E%40seanrm100_%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou're%20welcome!%20Glad%20it%20resolved%20your%20issue.%3C%2FP%3E%3CP%3EDon't%20worry%2C%20this%20is%20the%20most%20common%20and%20obvious%20mistake.%20%3CLI-EMOJI%20id%3D%22lia_slightly-smiling-face%22%20title%3D%22%3Aslightly_smiling_face%3A%22%3E%3C%2FLI-EMOJI%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
seanrm100_
Occasional Contributor

So I have a macro that creates sheets, transfers data to them, and then cleans it. All is well and good, but I often get the

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

error when I'm trying to sort the data. At the start of the macro, it checks whether specific sheets exist in the data workbook (not the one running the macro) and deletes them if they do, before creating new ones. I've noticed about 50% of the time I get this error when trying to sort the data in the sheets:
ws.Range("A1:Z200").Sort Key1:=Columns("A"), Order1:=xlAscending, Header:=xlNo

But when I delete the sheets manually before running the macro again, it always works. The weirdest part is that I added a formcontrol button to run the macro, but when I use it, I get the error message every single time. I've also tried clearing the sheet's contents instead of deleting it but I get the same results. Anyone seen inconsistent mystery errors before and know what I can do to fix it? Thanks

3 Replies
Solution

@seanrm100_ 

You should qualify the key with the Sheet reference like this...

ws.Range("A1:Z200").Sort Key1:=ws.Columns("A"), Order1:=xlAscending, Header:=xlNo

 

Or more precisely this...

ws.Range("A1:Z200").Sort Key1:=ws.Range("A1"), Order1:=xlAscending, Header:=xlNo

 

This should resolve the issue.

Highlighted
Wow, that's what I get for copying and pasting code, I can't believe I've been I've been dealing with this for weeks over something so silly. Thanks!

@seanrm100_ 

You're welcome! Glad it resolved your issue.

Don't worry, this is the most common and obvious mistake.

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
38 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
29 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies