Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Sort Dynamic Range
Issue: 1) I have a dynamic data range, (i.e. # of rows and cols may vary). 2) example range: A4:D10 3) Problem: the sort function is static and not dynamic, I tried to give it a name range but I get an error. Current Macro: Sub test1() ' ' test1 Macro ' ' ActiveWorkbook.Names("sortrange").Delete Application.Goto Reference:="client1" Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlToRight)).Select ActiveWorkbook.Names.Add Name:="sortrange", RefersToR1C1:= _ ActiveWindow.RangeSelection.Address ' "=Sheet1!R4C1:R8C4" ActiveWorkbook.Names("sortrange").Comment = "" ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Clear ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add Key:=ActiveCell, _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Sheet1").Sort .SetRange ActiveCell.Range("sortrange") <===ISSUE: needs to be dynamic .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Application.Goto Reference:="R1C1" End Sub |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Mar 21, 12:24*pm, lightjag
wrote: Sort Dynamic Range Issue: * 1) *I have a *dynamic data range, *(i.e. # of rows and cols may vary). 2) *example range: A4:D10 3) *Problem: *the sort function is static and not dynamic, I tried to give it a name range but I get an error. Current *Macro: Sub test1() ' ' test1 Macro ' ' * * ActiveWorkbook.Names("sortrange").Delete * * Application.Goto Reference:="client1" * * Range(Selection, Selection.End(xlDown)).Select * * Range(Selection, Selection.End(xlToRight)).Select * * ActiveWorkbook.Names.Add Name:="sortrange", RefersToR1C1:= _ * * * * ActiveWindow.RangeSelection.Address ' * * * *"=Sheet1!R4C1:R8C4" * * ActiveWorkbook.Names("sortrange").Comment = "" * * ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Clear * * ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add Key:=ActiveCell, _ * * * * SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal * * With ActiveWorkbook.Worksheets("Sheet1").Sort * * * * .SetRange ActiveCell.Range("sortrange") * *<===ISSUE: needs to be dynamic * * * * .Header = xlGuess * * * * .MatchCase = False * * * * .Orientation = xlTopToBottom * * * * .SortMethod = xlPinYin * * * * .Apply * * End With * * Application.Goto Reference:="R1C1" End Sub Ligthjag, Rather than using a Name to refer to your sort range, try using a range object. I simply recorded a sort macro and then added my own code to it. See below. (Also, I'm not familiar with .SetRange. I'm using Office 2003 and the Object Browser doesn't have .SetRange as a property). Best, Matt Herbert Sub SortRange() Dim rngSort As Range Dim rngSortKey As Range Set rngSort = Range("A4:D10") Set rngSortKey = Range("A4") rngSort.Sort Key1:=rngSortKey, Order1:=xlAscending, _ Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _ Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal End Sub |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() " wrote: On Mar 21, 12:24 pm, lightjag wrote: Sort Dynamic Range Issue: 1) I have a dynamic data range, (i.e. # of rows and cols may vary). 2) example range: A4:D10 3) Problem: the sort function is static and not dynamic, I tried to give it a name range but I get an error. Current Macro: Sub test1() ' ' test1 Macro ' ' ActiveWorkbook.Names("sortrange").Delete Application.Goto Reference:="client1" Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlToRight)).Select ActiveWorkbook.Names.Add Name:="sortrange", RefersToR1C1:= _ ActiveWindow.RangeSelection.Address ' "=Sheet1!R4C1:R8C4" ActiveWorkbook.Names("sortrange").Comment = "" ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Clear ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add Key:=ActiveCell, _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Sheet1").Sort .SetRange ActiveCell.Range("sortrange") <===ISSUE: needs to be dynamic .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Application.Goto Reference:="R1C1" End Sub Ligthjag, Rather than using a Name to refer to your sort range, try using a range object. I simply recorded a sort macro and then added my own code to it. See below. (Also, I'm not familiar with .SetRange. I'm using Office 2003 and the Object Browser doesn't have .SetRange as a property). Best, Matt Herbert Sub SortRange() Dim rngSort As Range Dim rngSortKey As Range Set rngSort = Range("A4:D10") Set rngSortKey = Range("A4") rngSort.Sort Key1:=rngSortKey, Order1:=xlAscending, _ Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _ Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal End Sub Matt, with regard to your solution: "Set rngSort = Range("A4:D10")" <== this is static, how would I make it dynamic (as # of row & col will chg)? |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Mar 21, 3:10*pm, lightjag
wrote: " wrote: On Mar 21, 12:24 pm, lightjag wrote: Sort Dynamic Range Issue: * 1) *I have a *dynamic data range, *(i.e. # of rows and cols may vary). 2) *example range: A4:D10 3) *Problem: *the sort function is static and not dynamic, I tried to give it a name range but I get an error. Current *Macro: Sub test1() ' ' test1 Macro ' ' * * ActiveWorkbook.Names("sortrange").Delete * * Application.Goto Reference:="client1" * * Range(Selection, Selection.End(xlDown)).Select * * Range(Selection, Selection.End(xlToRight)).Select * * ActiveWorkbook.Names.Add Name:="sortrange", RefersToR1C1:= _ * * * * ActiveWindow.RangeSelection.Address ' * * * *"=Sheet1!R4C1:R8C4" * * ActiveWorkbook.Names("sortrange").Comment = "" * * ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Clear * * ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add Key:=ActiveCell, _ * * * * SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal * * With ActiveWorkbook.Worksheets("Sheet1").Sort * * * * .SetRange ActiveCell.Range("sortrange") * *<===ISSUE: needs to be dynamic * * * * .Header = xlGuess * * * * .MatchCase = False * * * * .Orientation = xlTopToBottom * * * * .SortMethod = xlPinYin * * * * .Apply * * End With * * Application.Goto Reference:="R1C1" End Sub Ligthjag, Rather than using a Name to refer to your sort range, try using a range object. *I simply recorded a sort macro and then added my own code to it. *See below. *(Also, I'm not familiar with .SetRange. *I'm using Office 2003 and the Object Browser doesn't have .SetRange as a property). Best, Matt Herbert Sub SortRange() Dim rngSort As Range Dim rngSortKey As Range Set rngSort = Range("A4:D10") Set rngSortKey = Range("A4") rngSort.Sort Key1:=rngSortKey, Order1:=xlAscending, _ * * * * Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _ * * * * Orientation:=xlTopToBottom, _ * * * * DataOption1:=xlSortNormal End Sub Matt, with regard to your solution: "Set rngSort = Range("A4:D10")" *<== this is static, how would I make it dynamic (as # of row & col will chg)?- Hide quoted text - - Show quoted text - Lightjag, There are two issues you need to consider, assuming that your data is increasing in row or column size: (1) how the data set expands, and (2) how to set the Key1 argument of the Sort Method. (1) If your sort range is contiguous, then there needs to be a way to access at least one cell of the dynamic range. If you can find one of these cells, then expanding your range to the contiguous cells in the data set will be relatively easy. (2) There needs to be a consistent key. For example, maybe you always sort by the left most column, or maybe the right most column, or maybe one to the right of the left most column, etc. Dave's response has one method of making your range dynamic. It uses the .End property (in Excel this is equivalent to Ctrl + Arrow Keys); .End moves through contiguous cells. You can also accomplish this through .CurrentRegion (see VBE help). I've provided an example of an imaginary data set that has the upper- left corner of the data always in C10, and sorts according to the right most column of the data set. Again, feel free to get your "anchor" cell in a way that fits your procedure (e.g. InputBox, Loop through a row or column, etc.). If you provide more detail regarding the positioning of your data then we can help you narrow down how to make it dynamic for what you are doing. Best, Matt Sub SortRange() Dim rngSort As Range Dim rngSortKey As Range Set rngSort = Range("C10").CurrentRegion Set rngSortKey = rngSort.Cells(1, 1) Set rngSortKey = Cells(rngSortKey.Row, rngSortKey.Column _ + rngSort.Columns.Count - 1) rngSort.Sort Key1:=rngSortKey, Order1:=xlAscending, _ Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _ Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal End Sub |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Can you use a column in your data to determine the last row to sort?
Can you use a row in your data to determine the last column to sort? Dim LastRow As Long Dim LastCol As Long With ActiveSheet 'I'm using column A to determine the last row LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 'and row 4 to determine the last column LastCol = .Cells(4, .Columns.Count).End(xlToLeft).Row With .Range("A4", .Cells(LastRow, LastCol)) .Cells.Sort _ Key1:=.Columns(1), _ Order1:=xlAscending, _ Header:=xlYes, _ OrderCustom:=1, MatchCase:=False, _ Orientation:=xlTopToBottom, _ DataOption1:=xlSortTextAsNumbers End With End With ======= I sorted by the first column (column A) and I guessed that you had a header. It's your data. It's better if you specify it than let excel guess. lightjag wrote: Sort Dynamic Range Issue: 1) I have a dynamic data range, (i.e. # of rows and cols may vary). 2) example range: A4:D10 3) Problem: the sort function is static and not dynamic, I tried to give it a name range but I get an error. Current Macro: Sub test1() ' ' test1 Macro ' ' ActiveWorkbook.Names("sortrange").Delete Application.Goto Reference:="client1" Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlToRight)).Select ActiveWorkbook.Names.Add Name:="sortrange", RefersToR1C1:= _ ActiveWindow.RangeSelection.Address ' "=Sheet1!R4C1:R8C4" ActiveWorkbook.Names("sortrange").Comment = "" ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Clear ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add Key:=ActiveCell, _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Sheet1").Sort .SetRange ActiveCell.Range("sortrange") <===ISSUE: needs to be dynamic .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Application.Goto Reference:="R1C1" End Sub -- Dave Peterson |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
dynamic range based on criteria, within a dynamic range, passed to a function | Excel Programming | |||
creating a dynamic range based on criteria, within a dynamic range, and passing it to a function | Excel Programming | |||
Dynamic column chart - auto sort on data range | Charts and Charting in Excel | |||
Dynamic Range with unused formula messing up x axis on dynamic graph | Charts and Charting in Excel | |||
select dynamic range with dynamic start point | Excel Programming |