Forum Discussion
Method ‘Range’ of object ‘_Global’ failed
- May 16, 2022
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:=Truenear the end of ImportRsStaffTechActivityRev2 to
.Refresh BackgroundQuery:=False
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 SubI 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.
- HansVogelaarMay 16, 2022MVP
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.
- Michael_2025May 16, 2022Copper Contributor
HansVogelaar 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.
- HansVogelaarMay 16, 2022MVP
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:=Truenear the end of ImportRsStaffTechActivityRev2 to
.Refresh BackgroundQuery:=False