简体   繁体   English

将多张纸(但不是全部纸)的值复制/粘贴到一张纸中

[英]Copy/paste values from multiple sheets, but not all sheets, into one sheet

I am needing to copy cells B3:W400 from multiple sheets (will have varying names each time it is run) and paste values into "CombinedPlans", appending each new selection under the last. 我需要从多个工作表中复制单元格B3:W400(每次运行时名称都不同),然后将值粘贴到“ CombinedPlans”中,并将每个新选择追加到最后一个。 I need 3 sheets excluded from the code: IBExport, MonthlyIBs, and Combined Plans. 我需要从代码中排除3张纸:IBExport,MondayIB和组合计划。

A lot of googling with trial and error has given me the following code, which I got to work in my "practice" workbook. 大量的反复试验为我提供了以下代码,这些代码必须在“练习”工作簿中使用。 Now that I have put it into my production workbook, it is no longer copying any sheets. 现在,将其放入生产工作簿中,它不再复制任何工作表。 It just skips straight to the message box. 它只是直接跳到消息框。 What am I doing wrong? 我究竟做错了什么?

Sub consolidatetest()

Sheets("CombinedPlans").Select
Range("B3:W1048576").Select
Selection.ClearContents

Dim J As Integer
Dim sh As Worksheet
Const excludeSheets As String = "QBExport,MonthlyIBs,CombinedPlans"

On Error Resume Next
For Each sh In ActiveWorkbook.Worksheets
If IsError(Application.Match(sh.Name, Split(excludeSheets, ","))) Then
        Application.GoTo Sheets(sh.Name).[b3]
        Range("B3:W400").Select
        Selection.Copy
       Worksheets("CombinedPlans").Activate
    Range("B1048576").End(xlUp).Offset(rowOffset:=1, columnOffset:=0).PasteSpecial xlPasteValues
    End If
Next
Application.CutCopyMode = False
MsgBox "Complete!"

End Sub

This should work. 这应该工作。 If you have still problems, make sure that the Sheet CombinedPlans is indeed so named. 如果仍然有问题,请确保工作表CombinedPlans确实如此命名。

Sub consolidatetest()

Dim wb As Workbook
Dim sh_CombPlans As Worksheet

Set wb = ThisWorkbook
Set sh_CombPlans = wb.Sheets("CombinedPlans")
sh_CombPlans.Range("B3:W1048576").ClearContents

Dim sh As Worksheet

For Each sh In ActiveWorkbook.Worksheets
    Select Case sh.Name
        Case "QBExport", "MonthlyIBs", "CombinedPlans":
            'Do Nothing
        Case Else
            sh.Range("B3:W400").Copy
            sh_CombPlans.Range("B1048576").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
    End Select
Next

Application.CutCopyMode = False
MsgBox "Complete!"

End Sub

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

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