Sorting with Loop

beeworlding

New Member
Joined
Mar 6, 2019
Messages
2
Hi All,

I'm trying to run the Macro below but it crashes every time it get to the highlighted .Apply line. I have no idea why since it did not crash on the first .Apply line. Can someone please point me to the right direction?

Thanks

Code:
Dim arng As Range
Dim brng As Range
Dim crng As Range
Set arng = wsSAdj.Range("A:A")
Set brng = wsSAdj.Range("J:J")
Set crng = wsSAdj.Range("S:S")

For Each icell In brng.Cells
If Right(icell.Value, 5) = "Total" Then
Worksheets("Slides - ADJ").Sort.SortFields.Add Key:=Range( _
icell.Offset(-1, 3), icell.Offset(-1, 3).End(xlUp)), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Slides - ADJ").Sort
.SetRange Range(icell.Offset(-1, 1), icell.Offset(-1, 1).End(xlToRight).End(xlUp))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End If
Next
For Each icell In crng.Cells
If Right(icell.Value, 5) = "Total" Then
Worksheets("Slides - ADJ").Sort.SortFields.Add Key:=Range( _
icell.Offset(-1, 3), icell.Offset(-1, 3).End(xlUp)), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Slides - ADJ").Sort
.SetRange Range(icell.Offset(-1, 1), icell.Offset(-1, 1).End(xlToRight).End(xlUp))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
[U][B].Apply[/B][/U]
End With
End If
Next
 
Last edited by a moderator:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Re: Need Help - Sorting with Loop

Hi all,
I was able to resolve the problem myself, but just in case people wants to learn. all i've done to clear the error is putting in one extra line to clear the previous sort.
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,223,935
Messages
6,175,494
Members
452,649
Latest member
mr_bhavesh

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