How to use VB script for sorting columns

djanu

Board Regular
Joined
Oct 30, 2006
Messages
55
Hi,

Is there a way to use VB script to sort data ascending in A column?

-A--------B
tom------2
john-----1
mike-----3

It should be:

-A--------B
john-----1
mike-----3
tom------2


Thanks,
djanu
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I am trying to use this script:

With Sheets(2).Columns("A:B").Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End With

I get this error:

Run-time error '1004': Unable to get the Select property of the Range class

If I click debug it points to:

With Sheets(2).Columns("A:B").Select


Any ideas?

Thanks,
djanu
 
Upvote 0
Code:
With Sheets(2)
    .Columns("A:B").Sort Key1:=.Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
End With

Are you sure you want to use Sheets(2) rather than the sheet name?
 
Upvote 0
Thank Boller. It works great. Yes, I'm using names for the sheets. This was just an example.
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

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