excel - 按 1、8、28 而非 1、28、8 的顺序对选项卡进行排序

标签 excel vba sorting tabs

我有多个 Excel 文件,每个文件都有不同的编号系统。
其中包括 Item (#ofsheet),例如 Item (1)、Item (8)、Item (28)。
这些内容按第 1、28 和 8 项进行组织,而实际上应该是 1、8、28。

此代码将项目按项目 (1)、项目 (11)、项目 (2)、项目 (34) 顺序排列。

Sub sortAscendinfg()

    Dim i, N, k As Double

    'Count the number of worksheets and store the number in variable "n"
    N = Application.Sheets.Count
    
    'Do the following look for each worksheet again
    For i = 1 To N
    
        'Loop through all worksheets until the second last one (later you use the .move after function)
        For k = 1 To N - 1
            'If the name is larger than the following worksheet, change the sequence of these two worksheets.
            'In order to enable a proper comparison, change all characters to lower case (UCase = Upper case works
            'the same way.
            If LCase(Sheets(k).Name) > LCase(Sheets(k + 1).Name) Then Sheets(k).Move After:=Sheets(k + 1)
        Next
    Next

End Sub

最佳答案

对递增工作表进行排序

  • SortIncrementingSheetsTEST 过程是如何使用(调用)主 SortIncrementingSheets 过程的示例。
  • SortIncrementingSheets 过程需要 GetLastInteger 过程才能工作。
  • GetLastInteger 过程返回字符串中找到的最后一个整数(最后一个连续数字)。
  • GetLastIntegerTEST 过程是如何使用(调用)GetLastInteger 过程的示例。它在立即窗口中打印 13,因为 13 是示例字符串 Sheet1(013) 中的最后一个整数。
  • 基本上,所有工作表名称及其相应的最后一个整数都会写入字典KeysItems,然后使用整理纸张时。取消注释 Debug.Print 行,以便通过在“立即”窗口中查看结果来更好地了解该过程的工作原理。
  • 该过程中的排序基于以下 Microsoft 文档文章 MVP Tom Urtis :
    Sort Worksheets Alphanumerically by Name
Option Explicit

Sub SortIncrementingSheetsTEST()
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    SortIncrementingSheets wb
End Sub

Sub SortIncrementingSheets( _
        ByVal wb As Workbook)
' Needs 'GetLastInteger'.
    
    If wb Is Nothing Then Exit Sub
    
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    dict.CompareMode = vbTextCompare
    
    Dim sh As Object
    
    For Each sh In wb.Sheets
        dict.Add sh.Name, GetLastInteger(sh.Name)
    Next sh
    'Debug.Print Join(dict.Keys, ",")
    'Debug.Print Join(dict.Items, ",")
    
    Dim shCount As Long: shCount = wb.Sheets.Count

    Application.ScreenUpdating = False
    
    Dim i As Long
    Dim j As Long
    
    For i = 1 To shCount - 1
        For j = i + 1 To shCount
            If dict(wb.Sheets(j).Name) < dict(wb.Sheets(i).Name) Then
                wb.Sheets(j).Move Before:=wb.Sheets(i)
                'Debug.Print "Moved '" & wb.Sheets(i).Name & "' from '" _
                    & j & " to " & i & "'."
            End If
        Next j
    Next i

    Application.ScreenUpdating = True

    MsgBox "Sheets sorted.", vbInformation

End Sub

Function GetLastInteger( _
    ByVal SearchString As String) _
As Long
    
    Dim nLen As Long: nLen = Len(SearchString)
    
    Dim DigitString As String
    Dim CurrentChar As String
    Dim n As Long
    Dim FoundDigit As Boolean
    
    For n = nLen To 1 Step -1
        CurrentChar = Mid(SearchString, n, 1)
        If CurrentChar Like "#" Then ' it's a digit
            DigitString = CurrentChar & DigitString
            If Not FoundDigit Then
                FoundDigit = True
            End If
        Else ' it's not a digit
            If FoundDigit Then
                Exit For
            End If
        End If
    Next n
    
    If FoundDigit Then
        GetLastInteger = CLng(DigitString)
    Else
        GetLastInteger = -1
    End If

End Function

Sub GetLastIntegerTEST()
    Debug.Print GetLastInteger("Sheet1(013)")
End Sub

关于excel - 按 1、8、28 而非 1、28、8 的顺序对选项卡进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69996493/

相关文章:

c# - 当公式更新 Excel 单元格时通知

vba - 如何更改图表轴的字体属性

Ruby:按键对哈希进行排序并按值进行平局

javascript - 根据 javascript 中的数字/对象对数组或 json 进行排序

java - Apache POI 散点图创建

python - Excel 使用 Python Pandas 合并 2 个工作表中的单元格

c# - 以编程方式检索Excel库的版本

VBA代码为控制按钮分配名称,而不是让excel生成从数字派生的按钮名称

c# - Excel 2010 64 位无法创建 .net 对象

algorithm - 随机快速排序最坏情况时间复杂度