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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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