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
 

Some videos you may like

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

djanu

Board Regular
Joined
Oct 30, 2006
Messages
55
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
 

Boller

Banned
Joined
Apr 11, 2006
Messages
2,328
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?
 

djanu

Board Regular
Joined
Oct 30, 2006
Messages
55
Thank Boller. It works great. Yes, I'm using names for the sheets. This was just an example.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,193
Messages
5,594,767
Members
413,933
Latest member
Msar5586

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
Top