I'm beginning my foray into VBA and, not suprisingly, am now stuck.
The workbook is far too large to post, so here's some background:
- - - - - - - - - - - - -
I'm receiving an error in my VBA at the point where I'm attempting to sort. This is the error message:
Run-time error '1004':
The sort reference is not 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.
My workbook consists of the following sheets:
Orders
SortSheet
Operations
Staging
BWInput
Analysis
Summary
I'm attempting to move/manipulate/sort data from the Orders & Operations sheets (via the SortSheet & Staging Sheets) to the BWInput sheet with this VBA:
Sub SetupOptimizer()
' Move two columns to a better location
Range("Orders!Z:AA").Cut Destination:=Range("Orders!T:U")
' Move the block of raw orders data to the sort sheet
Range("OrdersData").Copy Destination:=Range("SortSheet!E2")
' Get rid of the redundant data on the orders sheet
Range("tblOrders").Clear
' Autofill the formulas down alongside the orders
Range("SortSheet!A2:D2").AutoFill Destination:=Range("SortCodeDestination")
' Ensure the formulas run
Calculate
' Sort the orders based on the formulas
Range("tblSort").Sort _
Key1:=Range("A2"), _
Order1:=xlAscending, _
Header:=xlYes, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
' Move the sorted orders to the next step
Range("SortResults").Copy Destination:=Range("Staging!A2")
' Put another copy of the sorted orders on the analysis sheet
Range("Analysis!C9").PasteSpecial (xlPasteValues)
' Get rid of the redundant data on the SortSheet sheet
Range("tblSort").Clear
' Autofill the formulas down alongside the sorted orders
Range("Staging!Z2:EV2").AutoFill Destination:=Range("StagingCodeDestination")
Calculate
Range("tblStaging").Copy
' Replace the formulas with values
Range("tblStaging").PasteSpecial (xlPasteValues)
Range("tblStaging").Copy
' Move the resulting block of data to the BWInput sheet
Range("BWInput!A1").PasteSpecial (xlPasteAll)
' Get rid of the redundant data on the operations sheet
Range("tblOperations").Clear
' Get rid of the redundant data on the staging sheet
Range("tblStaging").Clear
' Remove unnecessary rows from the Analysis sheet
Range("AnalysisTrim").Clear
Calculate
' Go to the summary sheet to finish
Range("Summary!M17").Select
End Sub
Here are the Dynamic Range Names (DRN) I'm using:
AnalysisTrim =OFFSET(Analysis!$A$1,COUNTA(Analysis!$C:$C)+7,0,10000,250)
OperationsHeaders =OFFSET(Operations!$A$1,0,0,1,COUNTA(Operations!$1:$1))
OperationsOrders =OFFSET(Operations!$A$1,0,0,COUNTA(Operations!$A:$A),1)
OrdersData =OFFSET(Orders!$A$1,1,0,COUNTA(Orders!$A:$A)-1,25)
SortCodeDestination =OFFSET(SortSheet!$A$1,1,0,COUNTA(SortSheet!$E:$E)-1,4)
SortData =OFFSET(SortSheet!$A$1,1,0,COUNTA(SortSheet!$E:$E)-1,29)
SortOrders =OFFSET(SortSheet!$E$1,1,0,COUNTA(SortSheet!$E:$E)-1,1)
SortResults =OFFSET(SortSheet!$E$1,1,0,COUNTA(SortSheet!$E:$E)-1,25)
StagingCodeDestination =OFFSET(Staging!$Z$1,1,0,COUNTA(Staging!$A:$A)-1,COUNTA(Staging!$1:$1)-COLUMN(Staging!$Y$1))
tblOperations =OFFSET(Operations!$A$1,0,0,COUNTA(Operations!$A:$A),COUNTA(Operations!$1:$1))
tblOrders =OFFSET(Orders!$A$1,0,0,COUNTA(Orders!$A:$A),COUNTA(Orders!$1:$1))
tblSort =OFFSET(SortSheet!$A$1,0,0,COUNTA(SortSheet!$E:$E),COUNTA(SortSheet!$1:$1))
tblStaging =OFFSET(Staging!$A$1,0,0,COUNTA(Staging!$A:$A),COUNTA(Staging!$1:$1))
- - - - - - - - - - - - -
I don't know if it is the DRN that's causing the error, but I think I need to use one since the size of data block will change from run to run.
Any suggestions?
Bookmarks