简体   繁体   English

当某些工作簿有一张工作表,一些工作簿有很多,有些工作簿具有隐藏的工作表时,将工作表从多个工作簿复制到当前工作簿中

[英]Copying worksheets from multiple workbooks into current workbook when some workbooks have one sheet, some have many, some have hidden worksheets

As the title says, I am attempting to copy all visible worksheets from a set of workbooks into a single workbook. 就像标题所说,我试图将所有可见的工作表从一组工作簿复制到一个工作簿中。

All of the workbooks are always in the same directory, but they will vary in file name. 所有工作簿始终位于同一目录中,但是文件名会有所不同。 I had tried originally using the code below, but I'm running into issues where the 'Next Sheet' line attempts to go to the next sheet in the workbook its copying from, even if there are no more worksheets. 我本来尝试使用下面的代码,但是遇到了一个问题,即使没有更多的工作表,“下一张工作表”行也尝试转到复制它的工作簿中的下一张工作表。

More specifically, my underlying workbooks which I'm trying to combine have a varying number of worksheets; 更具体地说,我要合并的基础工作簿具有不同数量的工作表。 some have one, some have many, and some have many with hidden worksheets too. 有些有一个,有些有很多,有些也有很多隐藏工作表。 I am only trying to copy sheets that are visible, and need to be able to handle the situation where a workbook could have one or many sheets. 我仅尝试复制可见的工作表,并且需要能够处理工作簿可能包含一个或多个工作表的情况。

I had tried a variant of the code below where I would count sheets and go to a separate code if there was one or more than one sheet, but that wasn't working either. 我在下面尝试了一种代码变体,如果有一张或多张纸,我将计算工作表数并转到单独的代码,但这都不起作用。 Any help is much appreciated, and thank you all for your time. 非常感谢您的帮助,感谢您的宝贵时间。

Sub ConslidateWorkbooks()

Dim FolderPath As String
Dim Filename As String
Dim Sheet As Worksheet
Application.ScreenUpdating = False
FolderPath = "MyPath"
Filename = Dir(FolderPath & "*.xls*")
Do While Filename <> ""
 Workbooks.Open Filename:=FolderPath & Filename
 For Each Sheet In ActiveWorkbook.Sheets
 Sheet.Copy after:=ThisWorkbook.Sheets(1)
 Next Sheet
 Workbooks(Filename).Close
 Filename = Dir()
Loop
Application.ScreenUpdating = True
End Sub

You should assign an object reference to the workbooks you open, rather than relying on ActiveWorkbook : 您应该为打开的工作簿分配对象引用,而不是依赖ActiveWorkbook

Dim wb As Workbook
Do While Filename <> ""
    Set wb = Workbooks.Open(Filename:=FolderPath & Filename)
    For Each Sheet In wb.Sheets
        If Sheet.Visible = xlSheetVisible Then 'only copy visible sheets
            Sheet.Copy After:=ThisWorkbook.Sheets(1)
        End If
    Next Sheet
    wb.Close
    Filename = Dir()
Loop

By avoiding the use of ActiveWorkbook , you will get around issues raised by users doing things that your code is not expecting. 通过避免使用ActiveWorkbook ,您可以避免由于用户执行代码所不期望的操作而引起的问题。

Try something along these lines: 尝试以下方法:

Sub ConslidateWorkbooks()
'Code to pull sheets from multiple Excel files in one file directory
'into master "Consolidation" sheet.

Dim FolderPath As String
Dim Filename As String
Dim Sheet As Worksheet

With ActiveSheet
    Range("A1").Activate
End With

Application.ScreenUpdating = False
FolderPath = ActiveWorkbook.Path & "\"
Filename = Dir(FolderPath & "*.xls*")

Do While Filename <> ""
   Workbooks.Open Filename:=FolderPath & Filename, ReadOnly:=True
   For Each Sheet In ActiveWorkbook.Sheets
   If Sheet.Visible = TRUE Then
       copyOrRefreshSheet ThisWorkbook, Sheet
   End If
   Next Sheet
   Workbooks(Filename).Close
   Filename = Dir()
Loop

Application.ScreenUpdating = True

End Sub



Sub copyOrRefreshSheet(destWb As Workbook, sourceWs As Worksheet)
  Dim ws As Worksheet
  On Error Resume Next
  Set ws = destWb.Worksheets(sourceWs.Name)
  On Error GoTo 0
  If ws Is Nothing Then
    sourceWs.Copy After:=destWb.Worksheets(destWb.Worksheets.Count)
  Else
    ws.Cells.ClearContents
    ws.Range(sourceWs.UsedRange.Address).Value = sourceWs.UsedRange.Value2
  End If
End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM