Sort one column A-Z while but only if adjacent column has data

Extivalis

New Member
Joined
Mar 21, 2024
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hi All,
Attached is a screenshot showing an example of what I'm trying to do, but:

Is there a why using macros/VBA to look at columns C&D, move group everything with data in Column D together, and then sort just the items with data in Column D alphabetically?

Currently we're manually doing this by highlighting columns C&D, going to Data > Sort, sorting column D alphabetically, then highlighting appropriate range (in the attached example, it'd be C2:D7), and then sorting column C alphabetically. It works, I'm just trying to see if I can't streamline things for my team some

Thanks.
 

Attachments

  • SnipImage1.JPG
    SnipImage1.JPG
    55.4 KB · Views: 5

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Please try the following on a copy of your workbook:

VBA Code:
Sub Sort_Twice()
    Range("C:D").Sort Key1:=Range("D1"), order1:=xlAscending, Header:=xlYes
    Range("C1:C" & Cells(Rows.Count, "D").End(xlUp).Row).Sort , Key1:=Range("C1"), order1:=xlAscending, Header:=xlYes
End Sub
 
Upvote 0
To get the results shown in your image, i think that you need to make one small change to the code from post 2

Rich (BB code):
Sub Sort_Twice()
  Range("C:D").Sort Key1:=Range("D1"), order1:=xlAscending, Header:=xlYes
  Range("C1:D" & Cells(Rows.Count, "D").End(xlUp).Row).Sort , Key1:=Range("C1"), order1:=xlAscending, Header:=xlYes
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,047
Members
449,206
Latest member
Healthydogs

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