SOLVED

Method ‘Range’ of object ‘_Global’ failed

%3CLINGO-SUB%20id%3D%22lingo-sub-3372592%22%20slang%3D%22en-US%22%3EMethod%20%E2%80%98Range%E2%80%99%20of%20object%20%E2%80%98_Global%E2%80%99%20failed%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3372592%22%20slang%3D%22en-US%22%3E%3CP%3EExcel%20Macro%20Error%20Run-time%20error%20%E2%80%981004%E2%80%99%3C%2FP%3E%3CP%3EMethod%20%E2%80%98Range%E2%80%99%20of%20object%20%E2%80%98_Global%E2%80%99%20failed%3C%2FP%3E%3CP%3EI%20have%20written%20a%20series%20of%20macros%20to%20import%20and%20analyze%20csv%20files%20to%20track%20the%20amount%20of%20time%20it%20takes%20for%20customers%20to%20receive%20services%20and%20flag%20situations%20where%20services%20take%20too%20long%20or%20data%20hasn%E2%80%99t%20been%20logged.%3C%2FP%3E%3CP%3EI%20have%20broken%20it%20apart%20into%205%20different%20subs%2C%20one%20to%20allow%20the%20user%20to%20select%20the%20file%20and%20import%2C%20one%20to%20sort%2C%20one%20to%20format%2C%20and%20the%20last%202%20to%20analyses%20and%20flag%20data.%20I%20then%20created%20a%20macro%20to%20call%20and%20run%20each%20in%20succession.%20When%20I%20run%20each%20macro%20sub%20individually%2C%20they%20all%20run%20without%20error.%20When%20I%20run%20the%20macro%20that%20combines%20them%20all%20together%2C%20I%20get%20the%20%E2%80%9CMethod%20%E2%80%98Range%E2%80%99%20of%20object%20%E2%80%98_Global%E2%80%99%20failed%E2%80%9D%20error%20on%20this%20piece%20of%20code%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ActiveWorkbook.Worksheets(%22Sheet1%22).ListObjects(%22rsStaffTechActivity%22).Sort.%20_%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20SortFields.Add2%20Key%3A%3DRange(%22rsStaffTechActivity%5BParticipant%5D%22)%2C%20SortOn%3A%3D%20_%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20xlSortOnValues%2C%20Order%3A%3DxlAscending%2C%20DataOption%3A%3DxlSortNormal%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20I%20click%20debug%20and%20click%20Continue%20(%20F5)%2C%20the%20code%20runs%20and%20completes%20without%20error.%20I%E2%80%99ve%20been%20looking%20for%20solutions%20for%20the%20past%20few%20days%2C%20and%20can%E2%80%99t%20seem%20to%20find%20anything%20that%20gets%20rid%20of%20the%20error.%20It%20is%20strange%20that%20everything%20is%20fine%20if%20I%20manually%20run%20each%20macro%2C%20but%20I%20can't%20combine%20them%20together.%20Any%20ideas%20would%20be%20appreciated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20whole%20sub%20looks%20like%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20SortImportedCSV()%3C%2FP%3E%3CP%3E'%20Step%202%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ActiveWorkbook.Worksheets(%22Sheet1%22).Sort.SortFields.Clear%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ActiveWorkbook.Worksheets(%22Sheet1%22).ListObjects(%22rsStaffTechActivity%22).Sort.%20_%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20SortFields.Add2%20Key%3A%3DRange(%22rsStaffTechActivity%5BParticipant%5D%22)%2C%20SortOn%3A%3D%20_%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20xlSortOnValues%2C%20Order%3A%3DxlAscending%2C%20DataOption%3A%3DxlSortNormal%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ActiveWorkbook.Worksheets(%22Sheet1%22).ListObjects(%22rsStaffTechActivity%22).Sort.%20_%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20SortFields.Add2%20Key%3A%3DRange(%22rsStaffTechActivity%5BCase_Note_Date%5D%22)%2C%20SortOn%3A%3D%20_%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20xlSortOnValues%2C%20Order%3A%3DxlAscending%2C%20DataOption%3A%3DxlSortNormal%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ActiveWorkbook.Worksheets(%22Sheet1%22).ListObjects(%22rsStaffTechActivity%22).Sort.%20_%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20SortFields.Add2%20Key%3A%3DRange(%22rsStaffTechActivity%5BActivity_Provided_Desc%5D%22)%2C%20_%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BSortOn%3A%3DxlSortOnValues%2C%20Order%3A%3DxlDescending%2C%20DataOption%3A%3DxlSortNormal%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20With%20ActiveWorkbook.Worksheets(%22Sheet1%22).ListObjects(%22rsStaffTechActivity%22).%20_%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Sort%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20.Header%20%3D%20xlYes%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20.MatchCase%20%3D%20False%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20.Orientation%20%3D%20xlTopToBottom%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20.SortMethod%20%3D%20xlPinYin%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20.Apply%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20End%20With%3C%2FP%3E%3CP%3EEnd%20Sub%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3372592%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-3372676%22%20slang%3D%22en-US%22%3ERe%3A%20Method%20%E2%80%98Range%E2%80%99%20of%20object%20%E2%80%98_Global%E2%80%99%20failed%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3372676%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1389794%22%20target%3D%22_blank%22%3E%40Michael_2025%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDoes%20this%20work%3F%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20SortImportedCSV()%0A'%20Step%202%0A%20%20%20%20Dim%20w%20As%20Worksheet%0A%20%20%20%20Dim%20t%20As%20ListObject%0A%20%20%20%20Set%20w%20%3D%20ActiveWorkbook.Worksheets(%22Sheet1%22)%0A%20%20%20%20Set%20t%20%3D%20w.ListObjects(%22rsStaffTechActivity%22)%0A%20%20%20%20With%20t.Sort%0A%20%20%20%20%20%20%20%20.SortFields.Clear%0A%20%20%20%20%20%20%20%20.SortFields.Add2%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20Key%3A%3Dw.Range(%22rsStaffTechActivity%5BParticipant%5D%22)%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20SortOn%3A%3DxlSortOnValues%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20Order%3A%3DxlAscending%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20DataOption%3A%3DxlSortNormal%0A%20%20%20%20%20%20%20%20.SortFields.Add2%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20Key%3A%3Dw.Range(%22rsStaffTechActivity%5BCase_Note_Date%5D%22)%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20SortOn%3A%3DxlSortOnValues%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20Order%3A%3DxlAscending%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20DataOption%3A%3DxlSortNormal%0A%20%20%20%20%20%20%20%20.SortFields.Add2%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20Key%3A%3Dw.Range(%22rsStaffTechActivity%5BActivity_Provided_Desc%5D%22)%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20SortOn%3A%3DxlSortOnValues%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20Order%3A%3DxlDescending%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20DataOption%3A%3DxlSortNormal%0A%20%20%20%20%20%20%20%20.Header%20%3D%20xlYes%0A%20%20%20%20%20%20%20%20.MatchCase%20%3D%20False%0A%20%20%20%20%20%20%20%20.Orientation%20%3D%20xlTopToBottom%0A%20%20%20%20%20%20%20%20.SortMethod%20%3D%20xlPinYin%0A%20%20%20%20%20%20%20%20.Apply%0A%20%20%20%20End%20With%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3380485%22%20slang%3D%22en-US%22%3ERe%3A%20Method%20%E2%80%98Range%E2%80%99%20of%20object%20%E2%80%98_Global%E2%80%99%20failed%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3380485%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%20Thank%20you%20for%20replying.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20is%20giving%20me%20an%20error%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERun-time%20error%20'9'%3A%3C%2FP%3E%3CP%3ESubscript%20out%20of%20range%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eon%20the%20following%20line%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESet%20t%20%3D%20w.ListObjects(%22rsStaffTechActivity%22)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3380883%22%20slang%3D%22en-US%22%3ERe%3A%20Method%20%E2%80%98Range%E2%80%99%20of%20object%20%E2%80%98_Global%E2%80%99%20failed%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3380883%22%20slang%3D%22en-US%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%20Thank%20you%20for%20the%20quick%20response.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20am%20still%20getting%20the%20%22Run-Time%20Error%201004%3CBR%20%2F%3EMethod%20'Range'%20of%20object%20'_Worksheet'%20failed%22%20error.%3CBR%20%2F%3E%3CBR%20%2F%3Eit%20is%20on%20line%3CBR%20%2F%3E.SortFields.Add2%20_%3CBR%20%2F%3EKey%3A%3Dw.Range(%22rsStaffTechActivity%5BParticipant%5D%22)%2C%20_%3CBR%20%2F%3ESortOn%3A%3DxlSortOnValues%2C%20_%3CBR%20%2F%3EOrder%3A%3DxlAscending%2C%20_%3CBR%20%2F%3EDataOption%3A%3DxlSortNormal%3CBR%20%2F%3E%3CBR%20%2F%3EThe%20most%20perplexing%20thing%20is%20that%20when%20I%20run%20each%20macro%20by%20itself%2C%20they%20work%20perfectly.%20But%20I%20am%20getting%20that%20range%20error%20when%20I%20run%20them%20all%20together.%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3381140%22%20slang%3D%22en-US%22%3ERe%3A%20Method%20%E2%80%98Range%E2%80%99%20of%20object%20%E2%80%98_Global%E2%80%99%20failed%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3381140%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1389794%22%20target%3D%22_blank%22%3E%40Michael_2025%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECould%20you%20attach%20a%20sample%20workbook%20(without%20sensitive%20data)%2C%20or%20if%20that%20is%20not%20possible%2C%20make%20it%20available%20through%20OneDrive%2C%20Google%20Drive%2C%20Dropbox%20or%20similar%3F%20Thanks%20in%20advance.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3381740%22%20slang%3D%22en-US%22%3ERe%3A%20Method%20%E2%80%98Range%E2%80%99%20of%20object%20%E2%80%98_Global%E2%80%99%20failed%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3381740%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1389794%22%20target%3D%22_blank%22%3E%40Michael_2025%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThank%20you.%20The%20problem%20is%20that%20the%20query%20in%20Step%201%20hasn't%20completely%20been%20refreshed%20yet%20when%20Step%202%20is%20run.%20To%20avoid%20this%2C%20change%20the%20line%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3E%20%20%20%20%20%20%20%20.Refresh%20BackgroundQuery%3A%3DTrue%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3Enear%20the%20end%20of%26nbsp%3BImportRsStaffTechActivityRev2%20to%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3E%20%20%20%20%20%20%20%20.Refresh%20BackgroundQuery%3A%3DFalse%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3381967%22%20slang%3D%22en-US%22%3ERe%3A%20Method%20%E2%80%98Range%E2%80%99%20of%20object%20%E2%80%98_Global%E2%80%99%20failed%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3381967%22%20slang%3D%22en-US%22%3EThat%20did%20it!%20I%20figured%20it%20was%20something%20simple%20like%20that%20I%20was%20missing.%3CBR%20%2F%3E%3CBR%20%2F%3EThank%20you%20so%20much%20for%20your%20help!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3381522%22%20slang%3D%22en-US%22%3ERe%3A%20Method%20%E2%80%98Range%E2%80%99%20of%20object%20%E2%80%98_Global%E2%80%99%20failed%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3381522%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3BI%20appreciate%20you%20taking%20a%20look.%20I%20have%20attached%20a%20csv%20with%20randomized%20data%20and%20macro%20enabled%20workbook.%20Each%20macro%20runs%20fine%2C%20but%20when%20combine%20into%20the%20'RunReport'%20macro%20I%20keep%20getting%20that%20Range%20error.%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Excel Macro Error Run-time error ‘1004’

Method ‘Range’ of object ‘_Global’ failed

I have written a series of macros to import and analyze csv files to track the amount of time it takes for customers to receive services and flag situations where services take too long or data hasn’t been logged.

I have broken it apart into 5 different subs, one to allow the user to select the file and import, one to sort, one to format, and the last 2 to analyses and flag data. I then created a macro to call and run each in succession. When I run each macro sub individually, they all run without error. When I run the macro that combines them all together, I get the “Method ‘Range’ of object ‘_Global’ failed” error on this piece of code:

 

    ActiveWorkbook.Worksheets("Sheet1").ListObjects("rsStaffTechActivity").Sort. _

        SortFields.Add2 Key:=Range("rsStaffTechActivity[Participant]"), SortOn:= _

        xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

 

When I click debug and click Continue ( F5), the code runs and completes without error. I’ve been looking for solutions for the past few days, and can’t seem to find anything that gets rid of the error. It is strange that everything is fine if I manually run each macro, but I can't combine them together. Any ideas would be appreciated.

 

The whole sub looks like this:

 

Sub SortImportedCSV()

' Step 2

    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear

 

    ActiveWorkbook.Worksheets("Sheet1").ListObjects("rsStaffTechActivity").Sort. _

        SortFields.Add2 Key:=Range("rsStaffTechActivity[Participant]"), SortOn:= _

        xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

   

    ActiveWorkbook.Worksheets("Sheet1").ListObjects("rsStaffTechActivity").Sort. _

        SortFields.Add2 Key:=Range("rsStaffTechActivity[Case_Note_Date]"), SortOn:= _

        xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

    ActiveWorkbook.Worksheets("Sheet1").ListObjects("rsStaffTechActivity").Sort. _

        SortFields.Add2 Key:=Range("rsStaffTechActivity[Activity_Provided_Desc]"), _

        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal

    With ActiveWorkbook.Worksheets("Sheet1").ListObjects("rsStaffTechActivity"). _

        Sort

        .Header = xlYes

        .MatchCase = False

        .Orientation = xlTopToBottom

        .SortMethod = xlPinYin

        .Apply

    End With

End Sub                                                                                                                                                                                               

 

6 Replies

@Michael_2025 

Does this work?

Sub SortImportedCSV()
' Step 2
    Dim w As Worksheet
    Dim t As ListObject
    Set w = ActiveWorkbook.Worksheets("Sheet1")
    Set t = w.ListObjects("rsStaffTechActivity")
    With t.Sort
        .SortFields.Clear
        .SortFields.Add2 _
            Key:=w.Range("rsStaffTechActivity[Participant]"), _
            SortOn:=xlSortOnValues, _
            Order:=xlAscending, _
            DataOption:=xlSortNormal
        .SortFields.Add2 _
            Key:=w.Range("rsStaffTechActivity[Case_Note_Date]"), _
            SortOn:=xlSortOnValues, _
            Order:=xlAscending, _
            DataOption:=xlSortNormal
        .SortFields.Add2 _
            Key:=w.Range("rsStaffTechActivity[Activity_Provided_Desc]"), _
            SortOn:=xlSortOnValues, _
            Order:=xlDescending, _
            DataOption:=xlSortNormal
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
@Hans Vogelaar Thank you for the quick response.

I am still getting the "Run-Time Error 1004
Method 'Range' of object '_Worksheet' failed" error.

it is on line
.SortFields.Add2 _
Key:=w.Range("rsStaffTechActivity[Participant]"), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal

The most perplexing thing is that when I run each macro by itself, they work perfectly. But I am getting that range error when I run them all together.

@Michael_2025 

Could you attach a sample workbook (without sensitive data), or if that is not possible, make it available through OneDrive, Google Drive, Dropbox or similar? Thanks in advance.

@Hans Vogelaar I appreciate you taking a look. I have attached a csv with randomized data and macro enabled workbook. Each macro runs fine, but when combine into the 'RunReport' macro I keep getting that Range error.

best response confirmed by Grahmfs13 (Microsoft)
Solution

@Michael_2025 

Thank you. The problem is that the query in Step 1 hasn't completely been refreshed yet when Step 2 is run. To avoid this, change the line

        .Refresh BackgroundQuery:=True

near the end of ImportRsStaffTechActivityRev2 to

        .Refresh BackgroundQuery:=False

 

That did it! I figured it was something simple like that I was missing.

Thank you so much for your help!