Auto sort three separate ranges within the same column

MTBAha

New Member
Joined
Jun 21, 2018
Messages
4
Hello,

I found this VBA that auto sorts a Multiple Column Groups Separately.
____________________________________________
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)


Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 1 Then

Range("A5:B50").Sort _

Key1:=Range("A5"), Order1:=xlAscending, _
Key2:=Range("B5"), Order2:=xlAscending, _
Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

ElseIf Target.Column = 3 Then


Range("C5:D50").Sort _

Key1:=Range("C5"), Order1:=xlAscending, _
Key2:=Range("D5"), Order2:=xlAscending, _
Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom


End If
End Sub
__________________________________________

But I need to auto sort two separate ranges within the same column like in the image below.

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]1[/TD]
[TD]Cell A[/TD]
[TD]Cell B[/TD]
[TD]Cell C[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Product[/TD]
[TD]Order Status[/TD]
[TD]Expiration Date[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Carrot[/TD]
[TD]Pending[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Orange[/TD]
[TD]Pending[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Pepper[/TD]
[TD]Complete[/TD]
[TD]2018/12/20[/TD]
[/TR]
</tbody>[/TABLE]

Need to be able to auto sort another table located below

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]Cell A[/TD]
[TD]Cell B[/TD]
[TD]Cell C[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Product[/TD]
[TD]Order Status[/TD]
[TD]Expiration Date[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Apple[/TD]
[TD]Pending[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]Pear[/TD]
[TD]Complete[/TD]
[TD]2018/10/15[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]Onion[/TD]
[TD]Complete[/TD]
[TD]2018/12/25[/TD]
[/TR]
</tbody>[/TABLE]

Please help with VBA code.

Thank you!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi,

Thanks for your response!

I tried it, but it doesn't work. What it does just selects the range of cells (I assume it's the first action of the macros I recorded). Is it at all possible to sort two separate ranges within the same column and the same sheet?
 
Upvote 0
The macro recorder records only what you do.
Turn on the recorder, perform all the stuff you want to do, turn off the recorder.
 
Upvote 0
Hi,

It recorded every step, but when I apply it to the button it only does the first line and stops. Is it possible at all to sort two different ranges within the same column?

I appreciate your response.
 
Upvote 0
If col A is values, not formulas & you have blank rows between sections. Try
Code:
Sub SortGroups()
   Dim Rng As Range
   
   For Each Rng In Range("A:A").SpecialCells(xlConstants).Areas
      Rng.CurrentRegion.Sort key1:=Rng.Resize(1, 1), order1:=xlAscending, header:=xlYes, MatchCase:=False
   Next Rng
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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