Excel vba:合并具有相同名称的工作簿

时间:2019-01-22 09:02:36

标签: excel vba

我使用此代码将更多工作簿合并到一个文件中。我发现的问题是我有几个具有相同名称的工作表,并且代码将停止。知道如何解决此问题吗?例如,如果我有两个名称为“ Sheet123”的工作表,程序将停止。

result_list = [myoriginaldata]
for i in range(n):
    new_result = result_list[n] + coef
    result_list.apend(new_result)

pd.concat(result_list)

1 个答案:

答案 0 :(得分:0)

让我们假设您创建了一个控制表,您可以尝试:

Option Explicit

Sub Loop_Sheets()

    Dim ws As Worksheet
    Dim LastRow As Long
    Dim wsName As String
    Dim wsList As Range, cell As Range
    Dim Excist As Boolean

    'Loop worksheets
    For Each ws In ThisWorkbook.Worksheets

        'Get Sheet name
        wsName = ws.Name

        With ThisWorkbook.Worksheets("Control")
            LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

            If LastRow = 1 Then
                .Cells(LastRow + 1, 1).Value = wsName
            Else
                'Set the list with worksheet names
                Set wsList = .Range(Cells(2, 1), Cells(LastRow, 1))

                Excist = False

                For Each cell In wsList
                    'Loop through list
                    If wsName = cell Then
                        Excist = True
                        Exit For
                    End If

                Next

                'If sheet appears in the list
                If Excist = True Then
                'Code'
                'If sheet dont appears in the list
                Else
                    .Cells(LastRow + 1, 1).Value = wsName
                    'Code'
                End If
            End If

        End With

    Next

End Sub