简体   繁体   English

VBA将工作表从一个工作簿复制到另一个文件夹中的所有工作簿

[英]VBA to copy worksheet from one workbook to all workbooks in another folder

Add worksheet to workbook using VBA 使用VBA将工作表添加到工作簿

I am looking to copy an existing (already created worksheet) into about 500 workbooks (*.xlsx) that all reside in the same folder. 我希望将现有(已创建的工作表)复制到全部位于同一文件夹中的约500个工作簿(* .xlsx)中。 I was able to cobble together the below code from various other topics on here but I am not able to get it to work. 我可以将此处其他各个主题的以下代码拼凑在一起,但是无法正常工作。

Private Sub Command0_Click()

   Dim file As String
   Dim myPath As String
   Dim wb As Workbook
   Dim rng As Range

   Dim wbMaster As Workbook
   'if master workbook already opened
   'Set wbMaster = Workbooks("ProjectBabelfish.xlsx")
   'if master workbook is not opened
   Set wbMaster = Workbooks.Open(CurrentProject.Path & "\ProjectBabelfish.xlsx")

   Set rng = wbMaster.Sheets("Babelfish").Range("A1:CC200")

   myPath = CurrentProject.Path & "\PLOGs\" ' note there is a back slash in the end"
   file = Dir(myPath & "*.xlsx*")
   While (file <> "")

        Set wb = Workbooks.Open(myPath & file)
        rng.Copy
        With wb.Worksheets("Babelfish").Range("A1")
            .PasteSpecial xlPasteColumnWidths
            .PasteSpecial xlPasteAll
        End With

        wb.Close SaveChanges:=True
        Set wb = Nothing

        file = Dir
    Wend

    Application.CutCopyMode = False

End Sub

Other than simply copying the worksheet from workbook to another, the formulas need to reference cells in the new workbook. 除了简单地将工作表从工作簿复制到另一个之外,这些公式还需要引用新工作簿中的单元格。 Also, I am trying to account for some of the workbooks being locked. 另外,我正在尝试说明某些工作簿已被锁定。

Something like this should work for you: 这样的事情应该为您工作:

Sub Command0_Click()

    Dim wbMaster As Workbook
    Set wbMaster = ThisWorkbook

    Dim wsCopy As Worksheet
    Set wsCopy = wbMaster.Worksheets("Babelfish")

    Dim sFolderPath As String
    sFolderPath = wbMaster.Path & "\PLOGs\"
    If Right(sFolderPath, 1) <> "\" Then sFolderPath = sFolderPath & "\"

    Dim sFileName As String
    sFileName = Dir(sFolderPath & "*.xlsx")

    'Disable screenupdating (to prevent "screen flickering" so macro runs smoother)
    'Disable alerts (to suppress "Are you sure?" prompts during worksheet deletion)
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With

    'Begin loop through files in the folder
    Do While Len(sFileName) > 0

        Dim sWBOpenPassword As String
        Dim sWBProtectPassword As String
        Select Case sFileName
            'Specify workbook names that require passwords here
            Case "Book2.xlsx", "Another Protected File.xlsx", "Third generic password file.xlsx"
                sWBOpenPassword = "password"
                sWBProtectPassword = "secondpassword"

            'If different books require different passwords, can specify additional names with their unique passwords
            Case "Book3.xlsx"
                sWBOpenPassword = "book3openpassword"
                sWBProtectPassword = "book3protectionpassword"

            'Keep specifying excel file names and their passwords until completed
            Case "Book10.xlsx", "Book257.xlsx"
                sWBOpenPassword = "GenericOpenPW2"
                sWBProtectPassword = "GenericProtectPW2"

            'etc...


            'Case Else will handle the remaining workbooks that don't require passwords
            Case Else
                sWBOpenPassword = ""
                sWBProtectPassword = ""

        End Select

        'Open file using password (if any)
        With Workbooks.Open(sFolderPath & sFileName, , , , Password:=sWBOpenPassword)

            Dim bProtectedWB As Boolean
            bProtectedWB = False    'Reset protected wb check to false

            'Check if workbook is protected and if so unprotect it using the specified protection password
            If .ProtectStructure = True Then bProtectedWB = True
            If bProtectedWB = True Then .Unprotect sWBProtectPassword

            On Error Resume Next    'Suppress error if copied worksheet does not yet exist
            .Worksheets(wsCopy.Name).Delete 'Delete existing sheet if it exists
            On Error GoTo 0         'Remove "On Error Resume Next" condition


            wsCopy.Copy After:=.Worksheets(.Worksheets.Count)   'Copy template into the workbook
            .Worksheets(wsCopy.Name).Cells.Replace wbMaster.Name, .Name 'Change references from master workbook to current workbook

            'If workbook was protected, reprotect it with same protection password
            If bProtectedWB = True Then .Protect sWBProtectPassword

            'Close file and save the changes
            .Close True
        End With

        sFileName = Dir 'Advance to next file in the folder
    Loop

    'Re-enable screenupdating and alerts
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA:将文件夹中所有工作簿的范围复制到另一个工作簿中的工作表,其中包含每个 wb 的工作簿名称 - VBA: Copy a range from all workbooks in a folder to a worksheet in another workbook with workbook name from each wb included VBA将数据从一个工作簿复制到另一工作簿/工作表不起作用 - VBA to Copy data from one workbook to another workbook/worksheet not working VBA 将多个工作簿中的数据复制到另一个工作簿“同一工作表”中,然后更新格式颜色和字体 - VBA to copy data from multiple workbooks into another workbook 'same worksheet' then update format color and font Excel VBA。 从多个工作簿复制数据并粘贴到一个工作簿的同一工作表中 - Excel VBA. Copy data from multiple workbooks and paste in one workbook same worksheet 将工作表从一个工作簿复制到另一工作簿 - Copy worksheet from one Workbook to another Workbook 如何创建 VBA 宏,它将数据从文件夹中的多个源工作簿复制到另一个工作簿,然后另存为新工作簿 - How to create a VBA macro that will copy data from multiple source workbooks within a folder to another workbook thereafter saving as a new workbook 宏将数据从一个工作簿复制到特定文件夹中的所有其他工作簿 - Macro to copy data from one workbook to all other workbooks in a specific folder VBA:将特定范围从多个工作簿复制到一个工作表中 - VBA: Copy specific range from multiple workbooks into one worksheet 试图将一个工作表中的一个工作表复制到另一个预先存在的工作表中? - Trying to copy one worksheet from one workbook into another preexisting worksheet? 从一个工作簿复制并粘贴到文件夹中的多个CSV工作簿中 - Copy and Paste from one workbook into multiple CSV workbooks in a folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM