Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi rjvega,
Define the lastcol as integer aswell and it works. Mika "rjvega" wrote in message ... 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 |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Avoid using Integer. Best is to declare integer variables as Long to avoid
overflow. For example, this works fine one month. So fine, in fact, that next month you change the For-Loop to do Rows.Count and wonder why it fails. Dim iRow As Integer 'This month For iRow = 1 To 10 .... Next iRow 'Next month For iRow = 1 To Rows.Count .... Next iRow "Mika Oukka" wrote: Hi rjvega, Define the lastcol as integer aswell and it works. Mika "rjvega" wrote in message ... 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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Sum a dynamically-defined range | Excel Discussion (Misc queries) | |||
Setting Range Dynamically | Excel Discussion (Misc queries) | |||
Dynamically set a range? | Excel Worksheet Functions | |||
Dynamically obtaining a range from another range | Excel Programming | |||
Name a range dynamically | Excel Programming |