vba - 将所有工作表重命名为 Sheet1 ColA 中每个单元格的值

标签 vba excel

我很惊讶我无法在网上找到解决方案。已经提出了几个类似的问题,但涉及更复杂的部分。
这真的是为了准备工作簿。 Sheet1 ColA 有一个部分编号列表。我需要它将工作表重命名为每个部分编号。如果需要,他们将需要保持秩序并创建更多工作表。每个部分编号只留下一张纸。

这是我发现但不完全理解的一些代码。看起来很接近,我只需要修改它以使用 ColA 而不是标题为“Last_Name”的列。

Sub MakeSectionSheets()

Dim rLNColumn As Range
Dim rCell As Range
Dim sh As Worksheet 
Dim shDest As Worksheet 
Dim rNext As Range 

    Const sNUMB As String = "Last_Name"

    Set sh = ThisWorkbook.Sheets("Sheet1")
    Set rLNColumn = sh.UsedRange.Find(sNUMB, , xlValues, xlWhole)

    'Make sure you found something 
    If Not rLNColumn Is Nothing Then
        'Go through each cell in the column 
        For Each rCell In Intersect(rLNColumn.EntireColumn, sh.UsedRange).Cells 
            'skip the header and empty cells 
            If Not IsEmpty(rCell.Value) And rCell.Address <> rLNColumn.Address Then
                'see if a sheet already exists 
                On Error Resume Next
                    Set shDest = sh.Parent.Sheets(rCell.Value)
                On Error GoTo 0

                'if it doesn't exist, make it
                If shDest Is Nothing Then
                    Set shDest = sh.Parent.Worksheets.Add
                    shDest.Name = rCell.Value
                End If

                'Find the next available row
                Set rNext = shDest.Cells(shDest.Rows.count, 1).End(xlUp).Offset(1, 0)

                'Copy and paste
                Intersect(rCell.EntireRow, sh.UsedRange).Copy rNext

                'reset the destination sheet
                Set shDest = Nothing
            End If
        Next rCell
    End If

End Sub

最佳答案

这是重命名工作表的方法

Dim oWorkSheet As Worksheet

    For Each oWorkSheet In Sheets
        If Len(oWorkSheet.Cells(1, 1).Value) > 0 Then
            oWorkSheet.Name = oWorkSheet.Cells(1, 1)
        End If
    Next

这是移动工作表的方法。
    Sheets(1).Move Before:=Sheets(2)

使用 here 中的快速排序算法你得到
Public Sub QuickSortSheets()
    QuickSort 1, Sheets.Count
End Sub

Private Sub QuickSort(ByVal LB As Long, ByVal UB As Long)
    Dim P1 As Long, P2 As Long, Ref As String, TEMP As String

    P1 = LB
    P2 = UB
    Ref = Sheets((P1 + P2) / 2).Name

    Do
        Do While (Sheets(P1).Name < Ref)
            P1 = P1 + 1
        Loop

        Do While (Sheets(P2).Name > Ref)
            P2 = P2 - 1
        Loop

        If P1 <= P2 Then
            TEMP = Sheets(P1).Name
            Sheets(P2).Move Before:=Sheets(TEMP)
            Sheets(TEMP).Move After:=Sheets(P2 - 1)

            P1 = P1 + 1
            P2 = P2 - 1
        End If
    Loop Until (P1 > P2)

    If LB < P2 Then Call QuickSort(LB, P2)
    If P1 < UB Then Call QuickSort(P1, UB)
End Sub

关于vba - 将所有工作表重命名为 Sheet1 ColA 中每个单元格的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9166654/

相关文章:

c++ - Excel 调用 C++ 程序的函数

php - 如何从 mySQL 数据库创建格式化的 Excel 列表?

html - VBA 函数不返回 HTMLElementCollection 对象

vba - 使用 VBA 或 Excel 将英尺和英寸(例如 "5 ft 1 in")转换为十进制英尺

excel - 显示 1/8 英寸的 VBA 用户定义函数显示空白

python - 在openpyxl中查看行值

c# - 使用 Microsoft.Office.Interop.Excel 读取所有行和列

python - 如果某个单元格包含带有python的 "0",有没有办法在excel中删除一行?

ms-access - 如何使用VBA循环删除多个表中的所有记录? Access 2010

excel - 以编程方式将按钮添加到 WorkbookOpen 上的工作表