简体   繁体   English

Excel VBA-将所有工作表从特定工作簿复制到活动工作簿中

[英]Excel VBA - Copying all Worksheets from a specific workbook into an active workbok

I'm trying to copy all worksheets from a file saved on a network drive into my current active workbook. 我正在尝试将所有工作表从网络驱动器上保存的文件复制到我当前的活动工作簿中。 After they are copied I would like to hide them. 复制它们后,我想隐藏它们。

The tricky part, which I have yet to been able to find, is every time the macro is re-run I would like those worksheets that were previously copied over to be overwritten or deleted and replaced by the new worksheets from the existing file I am copying from. 我还没有找到的棘手的部分是,每次重新运行宏时,我都希望以前复制的那些工作表被覆盖或删除,并替换为我现有文件中的新工作表。复制。

Currently, I have my code set up to just copy over a specific worksheet depending on the string of a hyperlink. 目前,我已将代码设置为仅根据超链接的字符串复制到特定的工作表上。 Below is what I've started but its not quite the direction I want to head. 以下是我已经开始的内容,但并不是我想要的方向。

Note the below is the edited script: 请注意,以下是已编辑的脚本:

Sub ImportWorksheets() 子ImportWorksheets()

Dim wb As Workbook, ws As Worksheet, wbTarget As Workbook, wsTarget As Worksheet

Application.ScreenUpdating = False

Dim pth As String
    pth = wb.Path

Dim titleDetailPth As String
    titleDetailPth = Left(pth, InStrRev(pth, "\") - 1)

Dim filePthName As String
    filePthName = titleDetailPth & "\New Release Templates\" & "Key New Release Accounts Details.xlsx"

Set wb = ActiveWorkbook 'Your workbook

Set wbTarget = Workbooks.Open(filePthName, UpdateLinks:=False, ReadOnly:=True) 'The drive workbook

For Each wsTarget In wbTarget.Worksheets 'a loop for each worksheet on the drive workbook
    For Each ws In wb.Worksheets ' a loop for each worksheet on your workbook
        If wsTarget.Name = ws.Name Then 'if the sheet you are trying to import exist, it will delete it
            Application.DisplayAlerts = False
            ws.Delete
            Application.DisplayAlerts = True
        End If
    Next ws
    wsTarget.Copy After:=wb.Sheets(wb.Sheets.Count) 'this will copy it into the last sheet
    wb.Sheets(wb.Sheets.Count).Visible = 0 'this will hide it
Next wsTarget
wbTarget.Close SaveChanges:=False
Application.ScreenUpdating = True

End Sub 结束子

Then this should do the work for you: 然后,这应该为您完成工作:

Sub ImportWorksheets()

    Dim wb As Workbook, ws As Worksheet, wbTarget As Workbook, wsTarget As Worksheet

    Application.ScreenUpdating = False


    Set wb = ThisWorkbook 'Your workbook

    Set wbTarget = Workbooks.Open("wherever your drive file is", UpdateLinks:=False, ReadOnly:=True) 'The drive workbook

    For Each wsTarget In wbTarget.Worksheets 'a loop for each worksheet on the drive workbook
        For Each ws In wb.Worksheets ' a loop for each worksheet on your workbook
            If wsTarget.Name = ws.Name Then 'if the sheet you are trying to import exist, it will delete it
                Application.DisplayAlerts = False
                ws.Delete
                Application.DisplayAlerts = True
            End If
        Next ws
        wsTarget.Copy After:=wb.Sheets(wb.Sheets.Count) 'this will copy it into the last sheet
        wb.Sheets(wb.Sheets.Count).Visible = 0 'this will hide it
    Next wsTarget
    wbTarget.Close SaveChanges:=False
    Application.ScreenUpdating = True

End Sub

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

相关问题 将数据从工作簿中的多个工作表复制到单独工作簿中的不同工作表-VBA Excel - Copying Data from Multiple Worksheets in Workbooks to Differing Worksheets in Separate Workbook - VBA Excel Excel VBA; 从不同位置的多个工作簿中复制特定的工作表 - Excel VBA; copying specific worksheets from multiple workbooks in different locations Excel VBA 将指定的一组工作表复制到新工作簿/从副本中排除工作表 - Excel VBA copying specified set of worksheets to new workbook/excluding sheet from copy 仅将可见工作表中的可见单元格复制到新工作簿中,即Excel 2007 VBA - Copying only the visible cells from visible worksheets into a new workbook, excel 2007 VBA Excel VBA复制工作表 - Excel VBA copying worksheets 隐藏工作簿/工作表Excel VBA - Hide workbook/worksheets Excel VBA EXCEL VBA-从另一个Excel实例复制到活动工作簿时出错 - EXCEL VBA - Error when copying from another instance of Excel to active workbook Excel VBA:如何在同一工作簿中合并特定的工作表? - Excel VBA: How to consolidate specific worksheets in the same workbook? 使用 VBA 在事件中使用来自表单的字符串从 Access 中搜索工作簿中的所有 Excel 工作表 - Search all Excel worksheets in workbook from Access with string from form on event with VBA Excel VBA - 使宏对工作簿中的所有工作表有效 - Excel VBA - Making a macro valid for all worksheets in workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM