Excel VBA - Repeated sorting causes Runtime Error '1004'

KeithRoberts

New Member
Joined
Oct 23, 2008
Messages
4
I am using MS Project VBA to create a status spreadsheet in Excel. I am receiving a runtime error '1004' Method 'Range' of object '_Global' failed the second time I run the status report.

I have created a form that creates the spreadsheet. If I comment out the sort code, I can run the application multiple times with no problems. However, whenever I try to sort, it abends with that error. I have tried two different sort syntaxes with the same result:

'xlApp.Selection.Sort Key1:=Range("H7"), _
' Order1:=xlDescending, _
' Key2:=Range("A7"), _
' Order2:=xlAscending, _
' Header:=xlNo, _
' OrderCustom:=1, _
' MatchCase:=False, _
' Orientation:=xlTopToBottom, _
' DataOption1:=xlSortNormal, _
' DataOption2:=xlSortNormal

xlApp.Worksheets(sSheetName).Range(MyRange).Sort
Key1:=Range("H7"), _
Order1:=xlDescending, _
Key2:=Range("A7"), _
Order2:=xlAscending, _
Header:=xlNo, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal

It should be noted that I am using late binding (Dim xlApp As Object) and then use the CreateObject("excel.application"). I changed to this strategy as Excel will not quit after the form is closed, but this is an issue for a later time.

Any information would be greatly appreciated.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I finally figured it out. During the process of a late bind and the sort statement, the enumerated values used in the sort statement prevented Excel from closing and instead, hanging out as an orphan process. The original sort statement was:

xlApp.Worksheets(sSheetName).Range(MyRange).Sort
Key1:=Range("H7"), _
Order1:=xlDescending, _
Key2:=Range("A7"), _
Order2:=xlAscending, _
Header:=xlNo, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal

I changed it to:
xlApp.Worksheets(sSheetName).Range(MyRange).Sort _
Key1:=xlApp.Worksheets(sSheetName).Range("H7"), _
Key2:=xlApp.Worksheets(sSheetName).Range("A7"), _
Order1:=2, _
Order2:=1

As can be seen, I had to remove the header and all keywords below in order to allow Excel to close and quit without leaving a hanging process.

I hope that this may help someone else in the future.
 
Upvote 0
The constants would not be the problem - it was the unqualified Range statements that were the issue. You could define those constants and put back the additional arguments if required.
 
Upvote 0
Thank you for your response. However, the code with the constants would leave Excel running after a quit was performed. When I commented out the sort command, Excel quit gracefully. When I changed the constants to 1 or 2, Excel quit gracefully. Please note that I did try using the constants with the fully qualified range statements and Excel would still be left as a running process after performing am xlApp.Quit.

However, it is now working as expected and that is what is important.
 
Upvote 0
Did you have the Excel application visible?
 
Upvote 0
Thanks for raising this - I had same problem and it was down to not qualifying the key used in the sort range. I was just changing the sort range itself.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top