Setting a variable range for a custom sort.

Agerial

New Member
Joined
Oct 25, 2013
Messages
1
Hi I am trying to save some processing time by reducing how much the computer needs to sort. Currently I have it set for 90,000 rows to handle the variable amount of information. I was wondering if there was a way to set the sort size based on how many rows actually have values/information in them.</SPAN>
Thanks</SPAN>
Sub WORK7()
'
' WORK7 Macro
'
'
Sheets("WORKSHEET").Select
Columns("A:D").Select
ActiveWorkbook.Worksheets("WORKSHEET").SORT.SortFields.Clear
ActiveWorkbook.Worksheets("WORKSHEET").SORT.SortFields.Add Key:=Range( _
"D1:D90000"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("WORKSHEET").SORT
.SetRange Range("A1:D90000")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("G1").Select
ActiveCell.FormulaR1C1 = "=SUM(C[-3])"
Range("A1:C" & Range("G1").Value).Select
Selection.COPY
Sheets("Sheet1").Select
Range("C1").Select
ActiveSheet.Paste
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Please put your code between code tags.
Code:
Dim lr As Long
lr = Cells(Rows.Count, 4).End(xlUp).Row
The Range then becomes
Code:
Range("D1:D" & lr)
At the beginning of the code use
Code:
Application.Screenupdating = False
and at the end use
Code:
Application.Screenupdating = True

Have a read here on optimizing and speeding up your code.
Optimize VBA
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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