microsoft_excel:macros:sort:sort
Differences
This shows you the differences between two versions of the page.
microsoft_excel:macros:sort:sort [2021/08/04 14:47] – created peter | microsoft_excel:macros:sort:sort [2021/08/04 14:47] (current) – peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Microsoft Excel - Macros - Sort - Sort ====== | ====== Microsoft Excel - Macros - Sort - Sort ====== | ||
+ | |||
+ | An example showing how to sort a worksheet. | ||
+ | |||
+ | <code excel> | ||
+ | ' Sorts all queries on all sheets. | ||
+ | ' Unfortunately, | ||
+ | Sub Sort_Queries() | ||
+ | |||
+ | Dim lastrow_Test1 As Long | ||
+ | Dim lastrow_Test2 As Long | ||
+ | |||
+ | | ||
+ | | ||
+ | ' Ask user. | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | |||
+ | |||
+ | ' Update StatusBar. | ||
+ | Application.StatusBar = " | ||
+ | |||
+ | |||
+ | With Workbooks(wb_name) | ||
+ | | ||
+ | With .Sheets(" | ||
+ | | ||
+ | ' Activates the sheet. | ||
+ | .Activate | ||
+ | | ||
+ | | ||
+ | ' Update StatusBar. | ||
+ | Application.StatusBar = " | ||
+ | | ||
+ | | ||
+ | ' Determine the number of rows. | ||
+ | lastrow_Test1 = .Cells(Rows.Count, | ||
+ | | ||
+ | |||
+ | ' Do the sort. | ||
+ | With .Sort | ||
+ | | ||
+ | ' | ||
+ | With .SortFields | ||
+ | .Clear | ||
+ | .Add Key: | ||
+ | End With | ||
+ | | ||
+ | .SetRange Range(" | ||
+ | .Header = xlYes | ||
+ | .MatchCase = False | ||
+ | .Orientation = xlTopToBottom | ||
+ | .SortMethod = xlPinYin | ||
+ | .Apply | ||
+ | | ||
+ | End With | ||
+ | |||
+ | |||
+ | ' Select A1. | ||
+ | ScrollTo ActiveSheet.name, | ||
+ | |||
+ | End With | ||
+ | | ||
+ | | ||
+ | With .Sheets(" | ||
+ | | ||
+ | ' Activates the sheet. | ||
+ | .Activate | ||
+ | | ||
+ | | ||
+ | ' Update StatusBar. | ||
+ | Application.StatusBar = " | ||
+ | | ||
+ | | ||
+ | ' Determine the number of rows. | ||
+ | lastrow_Test2 = .Cells(Rows.Count, | ||
+ | | ||
+ | |||
+ | ' Do the sort. | ||
+ | With .Sort | ||
+ | | ||
+ | ' | ||
+ | With .SortFields | ||
+ | .Clear | ||
+ | .Add Key: | ||
+ | End With | ||
+ | | ||
+ | .SetRange Range(" | ||
+ | .Header = xlYes | ||
+ | .MatchCase = False | ||
+ | .Orientation = xlTopToBottom | ||
+ | .SortMethod = xlPinYin | ||
+ | .Apply | ||
+ | | ||
+ | End With | ||
+ | |||
+ | |||
+ | ' Select A1. | ||
+ | ScrollTo ActiveSheet.name, | ||
+ | |||
+ | End With | ||
+ | | ||
+ | End With | ||
+ | End Sub | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | < | ||
+ | Sub b_SortData() | ||
+ | Cells.Select | ||
+ | ActiveWorkbook.Worksheets(" | ||
+ | ActiveWorkbook.Worksheets(" | ||
+ | " | ||
+ | xlSortNormal | ||
+ | ActiveWorkbook.Worksheets(" | ||
+ | " | ||
+ | xlSortNormal | ||
+ | With ActiveWorkbook.Worksheets(" | ||
+ | .SetRange range(" | ||
+ | .Header = xlYes | ||
+ | .MatchCase = False | ||
+ | .Orientation = xlTopToBottom | ||
+ | .SortMethod = xlPinYin | ||
+ | .Apply | ||
+ | End With | ||
+ | End Sub | ||
+ | </ | ||
microsoft_excel/macros/sort/sort.1628088445.txt.gz · Last modified: 2021/08/04 14:47 by peter