xlf | xlfSortVisibleWSheetsFromList macro part 2
0. Sort list, and sort WS from list macros
This module extends xlfSortVisibleWSheetsFromList (part 1) with the sort event moved to the Option Buttons. See figure 1 for Controls animation.
About the WS components:
- Defined name Sheetlist: is a WS dynamic range, with RefersTo:
=OFFSET(WorkArea!$C$5,0,0,COUNTA(WorkArea!$C:$C)-1)
A dynamic range anchored on cell $C$5, the first sheet name in the list - Defined name SortAsc RefersTo:
=WorkArea!$F$6
. Located behind the on-sheet controls
About the on-sheet control components (ver. 2):
- GroupBox: Caption: "Sort Order"; Name: "Group Box 3";
- OptionButton: Caption: "Ascending"; GroupBox.Name: "Group Box 3"; Name: "Option Button 4". Ascending is first in the group, so it returns the value 1
- OptionButton: Caption: "Descending"; GroupBox.Name: "Group Box 3"; Name: "Option Button 5". Descending is second in the group, so it returns the value 2
- Button: Caption: "Sort WS from list"; OnAction: "'xlf-sort-sheets.xlsm'!Sheet8.xlfSortVisibleWSheetsFromList1"; Name: "Button 7"
1. xlfSortVisibleWSheetsFromList - the VBA code
The amended section of code 1 is shown by the ▼ ▲ indicators
1.1 "Ascending" and "Descending" option buttons
Code 1: Sub
SortAsc_Click
and SortDesc_Click
replaces the Sort Sheetlist button (ver. 1)
Option Explicit Const List As String = "SheetList" ' [Sheet8 (Code)] Module (WorkArea) '============================ ' By-Pass the control Cell Link▼ START Sort List Group
Private Sub SortAsc_Click() cmdSort_Click 1 End Sub Private Sub SortDesc_Click() cmdSort_Click 2 End Sub '============================▲ END Sort List Group
'============================ ' Sort WS from list button Private Sub cmdSort_Click(Sort As Integer) Dim SortDir As Long If Sort = 1 Then SortDir = xlAscending Else SortDir = xlDescending Range(List).Sort _ Key1:=Range(List).Range("A1"), _ Order1:=SortDir, _ Header:=xlNo End Sub '
1.2 "Sort WS from List" button
Code 2: Macro
xlfSortVisibleWSheetsFromList1
for the Sort WS from List button
'============================ ' Sort WS from List Private Sub xlfSortVisibleWSheetsFromList1() ' List values must be valid worksheet names ' Worksheet must be visible (visibility property) Dim ASht As String Dim i As Integer ASht = ActiveSheet.Name Application.ScreenUpdating = False With Range(List) ' a one column range For i = .Rows.Count To 1 Step -1 Worksheets(.Cells(i, 1).Value).Move before:=Worksheets(1) Next i End With Worksheets(ASht).Activate Application.ScreenUpdating = True End Sub '
- Download: xlf-sort-sheets-part2.xlsm [30 KB ]
- Development platform: Office 365 ProPlus - Excel 2016 MSO (16.0...) 64 bit
- Published: 3rd June 2018
- Revised: Friday 24th of February 2023 - 02:38 PM, Pacific Time (PT)