英文:
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:
- 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
- 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.
- 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 ' ThisWorkbook
'if not "Summary" sheet is 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
- 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 not "Summary" 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 <> 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 <> "Summary" And .Item(j).Name <> "Summary" Then
If UCase(.Item(i).Name) > UCase(.Item(j).Name) Then ' Check if the current position is wrong
.Item(j).Move Before:=.Item(i) 'bring it over
End If
End If
Next j
Next i
End With
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论