Data sort loop for each worksheet

Human_doing

Board Regular
Joined
Feb 16, 2011
Messages
137
Hi all,

the below snippet of code is part of a project I'm working on, the final part I need is to be able to sort the cells A3:D + final row of data on each worksheet by the generated number in row D ascending, can anyone please help with the final line(s) of code, I think it may need to start with .Range("A3:D" & LR).Sort but not sure what comes after that?

Many thanks

Code:
Dim Sh As Worksheet
Dim LR As Long
For Each Sh In ActiveWorkbook.Worksheets
With Sh
 
'Unmerge all cells
.Cells.MergeCells = False
'Delete unnecessary columns and rows
.Columns("A").Delete
.Rows("1:3").Delete
Sh.Name = Sh.Range("A2").Value
LR = .Range("C" & .Rows.Count).End(xlUp).Row
.Range("D4:D" & LR).Formula = "=RAND()*(1000-1)+1"
.Range("D4:D" & LR).Copy
.Range("D4:D" & LR).PasteSpecial xlPasteValuesAndNumberFormats
 
.Range("A3:D" & LR).Sort
End With
Next Sh
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try

Code:
.Range("A3:D" & LR).Sort Key1:=.Range("D3"), Order1:=xlAscending, _
        Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

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