Sorting, one last time

R

Richard

Embrassed to ask again but I've tried everything. The code is finally working
as is the command button but it's not sorting Column "D" I need it to sort
only Column's A,B,C,D
Private Sub CommandButton1_Click()
Me.CommandButton1.Caption = "Sort"
Dim rng As Range, rng1 As Range
With Worksheets("HList")
Set rng = .Range(.Cells(3, "A"), .Cells(Rows.Count, "A").End(xlUp))
Set rng1 = .Cells(3, "A").End(xlToRight)
Set rng = rng.Resize(, rng1.Column)
rng.Sort _
Key1:=.Range("A3"), _
Order1:=xlAscending, _
Header:=xlNo
End With
Application.OnTime Now + TimeSerial(0, 0, 2), _
ThisWorkbook.Name & "!ResetCaption"
CommandButton1.Caption = "Sorting..."
End Sub
 
D

Dave Peterson

If you know it's exactly 4 columns wide, how about:

Set rng = rng.Resize(, 4)

And drop the rng1 stuff completely???
 
G

Gary Keramidas

as dave mentioned, get rid of the rng1. and if you want to sort by column D,
change the key address

Private Sub CommandButton1_Click()
Me.CommandButton1.Caption = "Sort"
Dim rng As Range, rng1 As Range
With Worksheets("HList")
Set rng = .Range(.Cells(3, "A"), .Cells(Rows.Count, "A").End(xlUp)).Resize(, 4)
rng.Select
rng.Sort Key1:=.Range("A3"), Order1:=xlAscending, Header:=xlNo
End With
Application.OnTime Now + TimeSerial(0, 0, 2), ThisWorkbook.Name & _
"!ResetCaption"
CommandButton1.Caption = "Sorting..."
End Sub
 
R

Richard

Thanks, that worked!

Dave Peterson said:
If you know it's exactly 4 columns wide, how about:

Set rng = rng.Resize(, 4)

And drop the rng1 stuff completely???
 

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

Range class failed 8
run time error 91 1
runtime 1004 error 3
Sort Errors 4
List sorting and macros 3
sorting unknown range 1
Sorting help !!!!!!!!!!!!!!!!! 1
sorting problem with range definition 2

Top