【问题标题】:Copying Values from one Workbook to a newly created workbook将值从一个工作簿复制到新创建的工作簿
【发布时间】:2020-06-22 22:58:55
【问题描述】:

有人可以查看我下面的代码并告诉我缺少什么吗?我认为这会起作用,但一直在复制功能上中断。非常感谢您的帮助。

代码复制一个 excel 工作簿模板并在新目录中重命名它。然后需要将当前工作簿中的数据粘贴到新工作簿中。从此工作簿工作表“站点页面”复制,并粘贴到 NewFileName 工作表“摘要”中。

Public NewIntegration As String
    
Sub Copy_One_File()
    
'Variables
Dim TemplateWorkbook As String
Dim NewWorkbook As String
Dim GivenLocation As String
Dim OldFileName As String
Dim NewFileName As String

'Define Strings
TemplateWorkbook = "\\r.sharepoint.com@SSL\DavWWWRoot\teams\NOE-RadioEng\Radio\New Build\_2019 NSB Tracker\Integration Reports\Template\TEMPLATE.xlsx"
NewWorkbook = "\\r.sharepoint.com@SSL\DavWWWRoot\teams\NOE-RadioEng\Radio\New Build\_2019 NSB Tracker\Integration Reports\TEMPLATENew.xlsx"
GivenLocation = "\\r.sharepoint.com@SSL\DavWWWRoot\teams\NOE-RadioEng\Radio\New Build\_2019 NSB Tracker\Integration Reports\"
ReportCreator = "\\r.sharepoint.com@SSL\DavWWWRoot\teams\NOE-RadioEng\Radio\New Build\_2019 NSB Tracker\Site_Integration_Report_Creator.xlsm"
OldFileName = "TEMPLATENew.xlsx"
NewFileName = Range("D9").Text 'grab new file name report creator
NewIntegration = GivenLocation & NewFileName 'New location and file name

'Functions
FileCopy TemplateWorkbook, NewWorkbook 'copy file to new location
Name GivenLocation & OldFileName As GivenLocation & NewFileName 'Rename based on H2 cell name

'MsgBox NewIntegration
'Workbooks.Open (NewIntegration)
'MsgBox NewIntegration
'Workbook.Close (NewIntegration)
'MsgBox Worksheets("Summary").Range("A1")

'copy values to new sheet
Worksheets("Site Page").Range("J2").Copy_ Workbooks(NewIntegration).Worksheets("Summary").Range("G1")
End Sub

【问题讨论】:

  • “不断中断复制功能”不是很具有描述性 - 运行代码时会发生什么?”
  • @TimWilliams 运行时错误“9”:下标超出范围
  • 目标工作簿未打开...
  • 此外,当您使用多个工作簿时,您应该使用工作簿对象限定每个工作表/范围引用。
  • 我如何获得工作簿对象的资格?我也会尝试打开它。

标签: excel vba


【解决方案1】:

你可以试试这样的:

Dim wbNew As Workbook
'...
'...

Set wbNew = Workbooks.Open(NewIntegration, ReadOnly:=False)
'what is the source of the copy operation???
Worksheets("Site Page").Range("J2").Copy wbNew.Worksheets("Summary").Range("G1")
wbNew.Close True 'save and close

【讨论】:

  • 来源是运行当前宏的工作表
  • 仍然出现同样的错误。在新创建和打开的工作簿的顶部显示“刷新建议服务器上有一个新版本的文件可用”尝试了上面的代码,还尝试了 .refreshAll 命令,还做了一个关闭然后延迟并重新打开。但仍然没有工作。有什么建议吗?
  • 抱歉,我对此没有任何建议 - 我从未遇到过该错误
  • 也许在这里试试答案:stackoverflow.com/questions/53225677/…
猜你喜欢
  • 1970-01-01
  • 2014-12-09
  • 2021-10-18
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-08
相关资源
最近更新 更多