LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Dynamically sorting a range

I'm not very experienced in VBA, but I need to generate code that will sort a
range on another worksheet by clicking a command button. The range may
change as rows and columns are added. So far I have come up with the
following, but I seem to have a problem when it comes to actually reading the
range. I am getting an "Application-defined or object-defined error" when I
try to set the range.

Private Sub CommandButton1_Click()

Dim ws As Worksheet
Set ws = Worksheets("Sheet1")

Dim lastrow As Integer
Dim lastcol As String
Dim rng As Range

lastrow = ws.Range("A65536").End(xlUp).Row
lastcol = ws.Range("A1").End(xlToRight).Column

Set rng = Range(ws.Cells(1, 2), ws.Cells(lastrow, lastcol))

ws.Range(rng).Sort Key1:=ws.Range("L2"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

End Sub
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sum a dynamically-defined range Hershmab Excel Discussion (Misc queries) 3 December 7th 09 05:14 PM
Setting Range Dynamically DJS Excel Discussion (Misc queries) 5 December 7th 06 09:51 PM
Dynamically set a range? BKGT Excel Worksheet Functions 3 April 27th 06 03:26 PM
Dynamically obtaining a range from another range Sharad Vyas Excel Programming 2 December 20th 05 05:14 AM
Name a range dynamically Jerry[_12_] Excel Programming 1 October 8th 03 12:53 PM


All times are GMT +1. The time now is 01:43 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"