microsoft_excel:macro_sort
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
microsoft_excel:macro_sort [2020/11/15 21:10] – peter | microsoft_excel:macro_sort [2021/08/04 14:26] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Microsoft Excel - Macro 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/macro_sort.1605474617.txt.gz · Last modified: 2020/11/15 21:10 by peter