Sort排序
Question: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.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.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
Answer
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. |
相關文章
- 排序sort排序
- Collections sort()排序方法排序
- 堆排序 Heap Sort排序
- Elasticsearch script sort 排序Elasticsearch排序
- 桶排序bucket sort排序
- 快速排序(Quick Sort)排序UI
- 希爾排序(Shell Sort)排序
- 快速排序 (Quick Sort)排序UI
- 氣泡排序 bubble sort排序
- Lucene 排序 Sort與SortField排序
- [Shell] Sort排序的例子排序
- golang sort.Sort () 排序演算法學習Golang排序演算法
- 排序演算法-氣泡排序(Bubble Sort)排序演算法
- linux sort 多列正排序,倒排序Linux排序
- _sort_elimination_cost_radit和sort排序排序
- 計數排序 - Counting Sort排序
- Array.sort排序問題排序
- Collections.sort()方法,字元排序字元排序
- 插入排序(Insertion Sort)排序
- go sort.Interface 排序介面Go排序
- R排序sort、order、rank、arrange排序
- 拓撲排序 - Topological Sort排序
- [CareerCup] 3.6 Sort Stack 棧排序排序
- Sorting 排序演算法: Quick Sort 快速排序排序演算法UI
- 經典排序演算法 - 快速排序Quick sort排序演算法UI
- 線性時間的排序 - Decision Tree Model & Counting Sort & Radix Sort & Bucket Sort排序
- 【Oracle】排序與sort_area_sizeOracle排序
- 【c++】結構體sort排序C++結構體排序
- 75. Sort Colors:計數排序排序
- 《shell下sort排序命令的使用》排序
- sort按照數值大小排序排序
- C# 字典排序Array.SortC#排序
- nls_sort與漢字排序排序
- 排序演算法(3)插入排序(Insertion Sort)排序演算法
- 排序演算法之「快速排序(Quick Sort) _c++ 」排序演算法UIC++
- 排序演算法之「插入排序(Insertion Sort)」排序演算法
- 排序演算法之「歸併排序(Merge Sort)」排序演算法
- Javascript陣列排序sort方法和自定義排序方法JavaScript陣列排序