VBA code to sort alphabetically

Giancar

Board Regular
Joined
Nov 29, 2017
Messages
52
Hi,

Could you please help me with a code that sort Range B2:B1027 from A to Z?
Sheet Name is "Viaggio"?

Then a different code that sort Range A2:A1027 from smallest to largest?
Sheet name is "Viaggio".

Thank you so much
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
VBA Code:
Sub Sort_A()
    With Sheets("Viaggio")
        .Range("A2:A1027").Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    End With
End Sub
 
Last edited:
Upvote 0
It does not do what I would like.
Your first Macro sort range B2:B1027, but I want to keep the value of RANGE A2:A1027.
Let suppose I have this table.

ABCDEF
2DOMA$9,000,000.00150,00098%$24.00
3DOMB$12,000,000.00178,00090%$42.00
4DOMC$18,000,000.00220,00086%$28.00
5DOMA$4,200,000.0036,00094%$40.00
6DOMB$8,000,000.00120,00086%$36.00
7DOMC$5,400,000.00132,00080%$49.00
8DOMG$2,000,000.0058,00081%$39.00
9DOMH$3,000,000.0040,00085%$29.00
10DOMK$6,000,000.0085,00082%$56.00
11DOMZ$4,000,000.0042,00071%$45.00

<tbody>
</tbody>

TThe result of your first Macro will be:

ABCDEF
2DOMA$9,000,000.00150,00098%$24.00
3DOMA$12,000,000.00178,00090%$42.00
4DOMB$18,000,000.00220,00086%$28.00
5DOMC$4,200,000.0036,00094%$40.00

<tbody>
</tbody>

But I would like:
ABCDEF
2DOMA$9,000,000.00150,00098%$24.00
5DOMA$12,000,000.00178,00090%$42.00
3DOMB$18,000,000.00220,00086%$28.00
4DOMC$4,200,000.0036,00094%$40.00

<tbody>
</tbody>


Then the 2nd Macro will sort A2:A1027 keeping the value of B2:D1027 (in this way I want to return to the starting table).

Thanks
 
Last edited:
Upvote 0
This should sort rows based on columns A or B values.

VBA Code:
Sub Sort_B()
    With Sheets("Viaggio").UsedRange
        .Sort Key1:=.Range("B1"), Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    End With
End Sub




Sub Sort_A()
    With Sheets("Viaggio").UsedRange
        .Sort Key1:=.Range("A1"), Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    End With
End Sub
 
Last edited:
Upvote 0
Code:
[COLOR=darkblue]Sub[/COLOR] Sort_B()
    [COLOR=darkblue]With[/COLOR] Sheets("Viaggio")
        .Range("B2:B1027").Sort Key1:=.Range("B2"), Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]


[COLOR=darkblue]Sub[/COLOR] Sort_A()
    [COLOR=darkblue]With[/COLOR] Sheets("Viaggio")
        .Range("A2:A1027").Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] Sub

Hi AlphaFrog,
Good work.. A1:C1 to A4:C4 but all rows from A1 to H4
How about sort 3 merge ranges
Like so:
ABCDEFGH
1Name 1101511218
2Name 2516146119
3Name 36135128113
4Name 4010111016

<tbody>
</tbody>


thanks
 
Upvote 0

Forum statistics

Threads
1,215,648
Messages
6,126,007
Members
449,280
Latest member
Miahr

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