Excel使用公式将一个工作表复制到另一个工作簿

yc0p9oo0  于 2023-03-20  发布在  其他
关注(0)|答案(2)|浏览(592)

我在想有没有办法复印一张(称为aaa)从工作簿(称为AAA)在工作表中(称为bbb)的工作簿(称为BBB)-问题是,我不想手动复制和粘贴,因为我希望自动执行此过程,因为每个月我都想更改要复制的文件(目标文件保持不变,只是更新了).有没有一个公式,例如,我可以在输入中给出我想复制的文件的路径和工作表的名称(aaa),这样它就可以直接复制并粘贴到我的新文件的bbb工作表中?
我尝试了一个VBA代码,但它说“Set sheettopaste = ActiveWorkbook.Sheets(“bbb”)”的下标超出范围。

Sub CopyPaste()
    Dim sheettocopy As Worksheet
    Dim sheettopaste As Worksheet
    Dim endrow As Long
    
    Set workbooktocopy = Workbooks.Open("Z:\AAA.xlsx")
    Set sheettocopy = workbooktocopy.Sheets("aaa")
    Set sheettopaste = ActiveWorkbook.Sheets("bbb")
    
    Application.ScreenUpdating = False
    
             endrow = sheettopaste.Range("A" & sheettopaste.Rows.Count).End(xlUp).Row
             sheettocopy.Range("A1:P250000").Copy
             sheettopaste.Activate
             sheettopaste.Range("A" & endrow + 1).PasteSpecial Paste:=xlPasteValues
             Application.CutCopyMode = False
             sheettocopy.Activate
             Range("A1:P250000").ClearContents
End Sub

是否还有其他方法可以在不使用VBA的情况下继续?
先谢谢你,这对我真的很有帮助!

wf82jlnq

wf82jlnq1#

Option Explicit

Sub CopyPaste()
    Const WBCOPY = "Z:\AAA.xlsx"
    Dim ws As Worksheet, rng As Range
    
    Set ws = ActiveWorkbook.Sheets("bbb")
    With Workbooks.Open(WBCOPY)
        Set rng = .Sheets("aaa").UsedRange
        ws.Range("A1").Resize(rng.Rows.Count, rng.Columns.Count) = rng.Value2
        rng.ClearContents
    End With
End Sub
bfnvny8b

bfnvny8b2#

调用Sub可以选择许多文件。对于每个选择的文件,提示输入所需的工作表名称。如果设置为空,则跳过该文件并继续下一个工作表。如果写入的工作表不存在,则会再次提示。

Option Explicit

'Copy this Sub in destination sheet "bbb"
Private Sub CopyPaste()
   Dim workbooktocopy As Workbook, sheettocopy As Worksheet, sheettopaste As Worksheet, endrow As Long
   Dim fn As Variant, fd As FileDialog, s As String, tmpWs As Worksheet, nextBook As Boolean, tr As Range
   Dim strinput As String
   
   Set fd = Application.FileDialog(msoFileDialogFilePicker)
   With fd
      .AllowMultiSelect = True
      .Title = "Select files from disk"
      .Filters.Add "XLSX", "*.xlsx"
      .Filters.Add "XLSXM", "*.xlsxm"
      .Filters.Add "All", "*.*"
      If .Show = True Then
         If Me.FilterMode Then
            Me.ShowAllData
         End If
         
         Set sheettopaste = ThisWorkbook.Worksheets("bbb")
         s = ""
         For Each fn In .SelectedItems
            Set workbooktocopy = Workbooks.Open(UCase(fn))
            Application.ScreenUpdating = False
            For Each tmpWs In workbooktocopy.Worksheets
               s = s & "@" & tmpWs.Name & "@" & vbCrLf
            Next
            
            nextBook = False
            Do
               strinput = InputBox("Sheets(" & workbooktocopy.Worksheets.Count & "): " & vbCrLf & s & vbCrLf & vbCrLf & "Write the desired sheet name without @" & vbCrLf, UCase(fn), "write the sheet name here")
               If strinput = "" Then
                  nextBook = True
                  Exit Do
               Else
                  If InStr(1, UCase(s), "@" & UCase(strinput) & "@") > 0 Then
                     Set sheettocopy = workbooktocopy.Sheets(strinput)
                     Exit Do
                  Else
                     MsgBox ("The sheet " & strinput & "does not exist!")
                  End If
               End If
            Loop
            
            If nextBook = False Then
               endrow = sheettopaste.Range("A" & sheettopaste.Rows.Count).End(xlUp).Row
               Set tr = sheettocopy.UsedRange    '.Range("A1:P250000").Copy
               tr.Copy
               sheettopaste.Activate
               sheettopaste.Range("A" & endrow + 1).PasteSpecial Paste:=xlPasteValues
               Application.CutCopyMode = False
               sheettocopy.Activate
               tr.Value = ""    ' or use: tr.ClearContents
               Call workbooktocopy.Close(True)
            End If
            
         Next fn
      End If
   End With
   Set fd = Nothing
End Sub

相关问题