按照表格数组对工作表进行排序。

huangapple go评论62阅读模式
英文:

Sort worksheets according from a table array

问题

I currently have an array that has all of the employee names listed. Each worksheet is named based on the employee name. I was wondering if it was possible to sort the worksheets in the order of the table array.

This is what I have so far:

Dim EmployeeNameRow As Long

Dim EmployeeLastRow As Long
EmployeeLastRow = ThisWorkbook.Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Row

Dim First_Name_Column As Integer
Dim Last_Name_Column As Integer

First_Name_Column = 1
Last_Name_Column = 2
Dim FirstAndLastName As String

'Create the array for which we'll need to add to once we've retrieve all the names
Dim EmployeeNameArray As Collection
Set EmployeeNameArray = New Collection

For EmployeeNameRow = 2 To EmployeeLastRow
    FirstAndLastName = ThisWorkbook.Sheets("Summary").Cells(EmployeeNameRow, First_Name_Column).Value & " " & ThisWorkbook.Sheets("Summary").Cells(EmployeeNameRow, Last_Name_Column).Value

    'Add these values to array
    EmployeeNameArray.Add FirstAndLastName
Next EmployeeNameRow

'Delete any duplicates added to the array

Dim DeletedDuplicatesEmployeeNameArray
DeletedDuplicatesEmployeeNameArray = RemoveDupes(EmployeeNameArray)

Dim r As Range
For Each r In DeletedDuplicatesEmployeeNameArray
    Sheets(r.Value).Move after:=Sheets(Sheets.Count)
Next
英文:

I currently have an array that has all of the employee names listed. Each worksheet is named based on the employee name. I was wondering if it was possible to sort the worksheets in the order of the table array.

This is what I have so far:

        Dim EmployeeNameRow As Long

        
        Dim EmployeeLastRow As Long
        EmployeeLastRow = ThisWorkbook.Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Row
        
        Dim First_Name_Column As Integer
        Dim Last_Name_Column As Integer
        
        First_Name_Column = 1
        Last_Name_Column = 2
        Dim FirstAndLastName As String
        
        'Create the array for which we'll need to add to once we've retrieve all the names
        Dim EmployeeNameArray As Collection
        Set EmployeeNameArray = New Collection

        
        For EmployeeNameRow = 2 To EmployeeLastRow
            FirstAndLastName = ThisWorkbook.Sheets("Summary").Cells(EmployeeNameRow, First_Name_Column).Value & " " & ThisWorkbook.Sheets("Summary").Cells(EmployeeNameRow, Last_Name_Column).Value
            
            'Add these values to array
            EmployeeNameArray.Add FirstAndLastName

        Next EmployeeNameRow
        
        'Delete any duplicates added to the array
        
        Dim DeletedDuplicatesEmployeeNameArray
        DeletedDuplicatesEmployeeNameArray = RemoveDupes(EmployeeNameArray)
        
        Dim r As Range
        For Each r In DeletedDuplicatesEmployeeNameArray
            Sheets(r.Value).Move after:=Sheets(Sheets.Count)
        Next

答案1

得分: 2

Sure, here are the translated parts of your code:

  1. Alphabetically sorting the existing sheets, excluding the sheet "Summary" and placing it as the last sheet:
Sub sortSheetsNames()
   Dim arrSh, sh As Worksheet, wb As Workbook, i As Long
   
   Set wb = ActiveWorkbook ' ThisWorkbook

    ' If "Summary" sheet is not the last one, place it as last:
    If Not wb.Worksheets("Summary") Is wb.Worksheets(wb.Worksheets.Count) Then
        wb.Worksheets("Summary").Move After:=wb.Worksheets(wb.Worksheets.Count)
    End If
    
   ReDim arrSh(1 To wb.Worksheets.Count - 1)
   For Each sh In wb.Worksheets
        i = i + 1: If i = wb.Worksheets.Count Then Exit For
        arrSh(i) = sh.Name
   Next
   
   BubbleSort arrSh
   Debug.Print Join(arrSh, "|"): 'just to visually see the sorted array...
   
   For i = 1 To wb.Worksheets.Count - 1
        If wb.Worksheets(i).Name <> arrSh(i) Then
            wb.Worksheets(arrSh(i)).Move Before:=wb.Worksheets(i)
        End If
   Next i
End Sub

Private Sub BubbleSort(arr) 'ascending
    Dim i As Long, j As Long, temp
    For i = LBound(arr) To UBound(arr) - 1
        For j = i + 1 To UBound(arr)
            If arr(i) > arr(j) Then
                temp = arr(i): arr(i) = arr(j)
                arr(j) = temp
            End If
        Next j
    Next i
End Sub
  1. Sorting according to the unique concatenated values between columns A and B of the sheet "Summary":
Sub SortShetsByArray()
   Dim wb As Workbook, sh As Worksheet, wS As Worksheet, i As Long, dict As Object
   Dim arr, arrSh, lastR As Long
   
   Set wb = ActiveWorkbook ' ThisWorkbook
   Set wS = wb.Worksheets("Summary")
   
   lastR = wS.Range("A" & wS.Rows.Count).End(xlUp).Row
   
   arr = wS.Range("A2:B" & lastR).Value2
   Set dict = CreateObject("Scripting.Dictionary")

   For i = 1 To UBound(arr)
        dict(arr(i, 1) & " " & arr(i, 2)) = vbNullString 'place the Unique concatenation first/last name in dictionary
   Next i
   
   arrSh = dict.Keys

    ' If "Summary" sheet is not the last one, place it as last:
    If Not wS Is wb.Worksheets(wb.Worksheets.Count) Then
        wS.Move After:=wb.Worksheets(wb.Worksheets.Count)
    End If
    
    For i = 1 To wb.Worksheets.Count - 1
        If wb.Worksheets(i).Name <> arrSh(i - 1) Then
            wb.Worksheets(arrSh(i - 1)).Move Before:=wb.Worksheets(i)
        End If
   Next i
End Sub

Please note that this code is for sorting sheets in Excel based on your description. If you have any specific questions or need further assistance, please let me know.

英文:

You did not answer my clarification question, so I will show you two ways of sorting sheets.

  1. Alphabetically sorting the existing sheets. But the sheet "Summary" must be excluded, from sorting and the code places it as last:
Sub sortSheetsNames()
   Dim arrSh, sh As Worksheet, wb As Workbook, i As Long
   
   Set wb = ActiveWorkbook &#39; ThisWorkbook
   
    &#39;if not &quot;Summary&quot; sheet is the last one, place it as last:
    If Not wb.Worksheets(&quot;Summary&quot;) Is wb.Worksheets(wb.Worksheets.count) Then
        wb.Worksheets(&quot;Summary&quot;).Move After:=wb.Worksheets(wb.Worksheets.count)
    End If
    
   ReDim arrSh(1 To wb.Worksheets.count - 1)
   For Each sh In wb.Worksheets
        i = i + 1: If i = wb.Worksheets.count Then Exit For
        arrSh(i) = sh.name
   Next
   
   BubbleSort arrSh
   Debug.Print Join(arrSh, &quot;|&quot;): &#39;just to visually see the sorted array...
   
   For i = 1 To wb.Worksheets.count - 1
        If wb.Worksheets(i).name &lt;&gt; arrSh(i) Then
            wb.Worksheets(arrSh(i)).Move Before:=wb.Worksheets(i)
        End If
   Next i
End Sub

Private Sub BubbleSort(arr) &#39;ascending
    Dim i As Long, j As Long, temp
    For i = LBound(arr) To UBound(arr) - 1
        For j = i + 1 To UBound(arr)
            If arr(i) &gt; arr(j) Then
                temp = arr(i): arr(i) = arr(j)
                arr(j) = temp
            End If
        Next j
    Next i
End Sub
  1. Sorting according to the unique concatenated values between columns A and B of the sheet "Summary":
Sub SortShetsByArray()
   Dim wb As Workbook, sh As Worksheet, wS As Worksheet, i As Long, dict As Object
   Dim arr, arrSh, lastR As Long
   
   Set wb = ActiveWorkbook &#39; ThisWorkbook
   Set wS = wb.Worksheets(&quot;Summary&quot;)
   
   lastR = wS.Range(&quot;A&quot; &amp; wS.rows.count).End(xlUp).row
   
   arr = wS.Range(&quot;A2:B&quot; &amp; lastR).Value2
   Set dict = CreateObject(&quot;scripting.Dictionary&quot;)

   For i = 1 To UBound(arr)
        dict(arr(i, 1) &amp; &quot; &quot; &amp; arr(i, 2)) = vbNullString &#39;place the Unique concatenation first/last name in dictionary
   Next i
   
   arrSh = dict.keys
   
    &#39;if not &quot;Summary&quot; sheet is the last one, place it as last:
    If Not wS Is wb.Worksheets(wb.Worksheets.count) Then
        wS.Move After:=wb.Worksheets(wb.Worksheets.count)
    End If
    
    For i = 1 To wb.Worksheets.count - 1
        If wb.Worksheets(i).name &lt;&gt; arrSh(i - 1) Then
            wb.Worksheets(arrSh(i - 1)).Move Before:=wb.Worksheets(i)
        End If
   Next i
End Sub

No error handling in case of not a match between the existing sheets name and the one from the array, but this should be easy to insert, I think...

The above codes works on ActiveWorkbook, which can be easily replaced with ThisWorkbook after testing.

答案2

得分: 0

不需要将名称读入数组中等操作,只需对工作表集合进行排序(它已经是一个集合):


Dim i As Integer, j As Integer
With ThisWorkbook.Worksheets
For i = 1 To .Count - 1
    For j = i + 1 To .Count
        If .Item(i).Name <> "Summary" And .Item(j).Name <> "Summary" Then
            If UCase(.Item(i).Name) > UCase(.Item(j).Name) Then ' 检查当前位置是否错误
               .Item(j).Move Before:=.Item(i) ' 将其移到前面
            End If
        End If
    Next j
Next i
End With
英文:

No need to read the names into an array, etc., just sort the worksheets collection (it is already a collection:


    Dim i As Integer, j As Integer
    With ThisWorkbook.Worksheets
    For i = 1 To .Count - 1
        For j = i + 1 To .Count
            If .Item(i).Name &lt;&gt; &quot;Summary&quot; And .Item(j).Name &lt;&gt; &quot;Summary&quot; Then
                If UCase(.Item(i).Name) &gt; UCase(.Item(j).Name) Then &#39; Check if the current position is wrong
                   .Item(j).Move Before:=.Item(i) &#39;bring it over
                End If
            End If
        Next j
    Next i
    End With

huangapple
  • 本文由 发表于 2023年5月15日 04:09:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定