PIYUSH VAID
PIYUSH VAID

Reputation: 13

Sort range of cells in single column using excel vba macro

I want to sort a range of cells using Excel VBA macro

I have searched many websites but everywhere the examples show how to sort multiple columns with a single key

e.g. this example

Example Excel

In example above I want to sort cells A2 to A8, that's it. For this my code is

Range("A2:A8").Sort

But it is giving Runtime error 1004

What is the correct way to do it?

Upvotes: 1

Views: 9990

Answers (1)

Mick17
Mick17

Reputation: 196

Try this

sub test()
Sheets("sheet1").Range("A2:A8").sort key1:=Range("A2:A8"), order1:=xlAscending, Header:=xlNo
End sub

Upvotes: 4

Related Questions