VBA从工作簿拷贝并粘贴到这个工作簿

时间:2022-05-26 02:22:20

VBA从工作簿拷贝并粘贴到这个工作簿I am trying to copy a template and paste it into the current workbook. But the code is erroring out. I get a run time error 1004.

我正在尝试复制一个模板并粘贴到当前工作簿中。但是代码出了问题。我得到一个运行时错误1004。

Sub templateToBBU()

'OPEN TEMPLATE
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
Dim sPath As String, sFile As String
Dim wb As Workbook
sPath = "C:\Users\user123\Desktop\"
sFile = sPath & "BBU_CMD_TEMPLATE.xlsx"
Set wb = Workbooks.Open(sFile)

'COPY TEMPLATE PASTE IN BBU DOC
wb.Sheets("TEMPLATE").Copy 
    After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)

End Sub

Thanks all,

谢谢,

1 个解决方案

#1


1  

I tested your code Excel 2016 and It works without problem. It seems It's a known problem for Excel 2003 - 2010 and there is a workaround. Save your template file as a Excel Template (xltx) and try as follow:

我测试了您的代码Excel 2016,它工作正常。这似乎是Excel 2003 - 2010的一个众所周知的问题,有一个变通方法。将模板文件保存为Excel模板(xltx),并尝试如下:

Sub templateToBBU()

'OPEN TEMPLATE
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
Dim sPath As String, sFile As String

sPath = "C:\Users\User123\Desktop\"
sFile = sPath & "Book1.xltx"

ThisWorkbook.Sheets.Add Type:=sFile
End Sub

#1


1  

I tested your code Excel 2016 and It works without problem. It seems It's a known problem for Excel 2003 - 2010 and there is a workaround. Save your template file as a Excel Template (xltx) and try as follow:

我测试了您的代码Excel 2016,它工作正常。这似乎是Excel 2003 - 2010的一个众所周知的问题,有一个变通方法。将模板文件保存为Excel模板(xltx),并尝试如下:

Sub templateToBBU()

'OPEN TEMPLATE
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
Dim sPath As String, sFile As String

sPath = "C:\Users\User123\Desktop\"
sFile = sPath & "Book1.xltx"

ThisWorkbook.Sheets.Add Type:=sFile
End Sub