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.
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.