sorting a range

H

hawk

Trying to sort a range A5:AZ350 on colum 'H' and then colum 'A' however I
would like to vary the first colum.
I created a macro to do the sort, but would like the macro to ask me what
colum to sort on when it runs , as the first colum varies all the time.
Any help appreciated

Roger
 
D

Dave Peterson

One way:

Option Explicit
Sub testme()

Dim myRng As Range
Dim myCol As Range

With Worksheets("sheet1")
.Select 'makes selecting the column easier
Set myRng = .Range("a5:az350")

Set myCol = Nothing
On Error Resume Next
Set myCol = Application.InputBox _
(prompt:="Please click on the column to use as the secondary key", _
Type:=8).Cells(1)
On Error GoTo 0

If myCol Is Nothing Then
'user hit cancel--just quit
Exit Sub
Else
If Intersect(myCol.Cells(1).EntireColumn, myRng) Is Nothing Then
'outside the range to sort, use H again
Set myCol = .Range("H1")
End If
End If

myRng.Sort key1:=.Range("h1"), order1:=xlAscending, _
key2:=myCol, order2:=xlAscending, _
header:=xlYes
End With

End Sub

I guessed at the orders and the headers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

printiing range 1
Date selection loop 17
Macro to copy a range 2
how entries in a column can be put into a row 2
return to next line question 2
AutoSum Syntax 1
Filtering 2
Copying a line 4

Top