[英]VBA: Copying cells from one workbook to another yields blank results
I have four subroutines that each take a specified external file and copy a sheet from that file into a sheet in the workbook the sub is running from. 我有四个子例程,每个子例程都使用一个指定的外部文件,并将该文件中的工作表复制到运行该子程序的工作簿中的工作表中。 For 3/4 subs, this works great. 对于3/4潜艇,效果很好。 The fourth sub, however, is giving me blank results in the destination. 但是,第四小节给了我目的地空白的结果。 The code is the same across all four subs, just with the filenames changed. 所有四个子代码都相同,只是文件名已更改。 The biggest difference I see that could be causing this error is that the source workbook in question is protected. 我看到可能导致此错误的最大区别是所讨论的源工作簿受到保护。 I am unlocking it using my sub and then copying the files. 我正在使用我的潜水艇解锁它,然后复制文件。 Adding a watch to the source shows the entire range as "Variant/Empty" 将手表添加到来源中,整个范围显示为“ Variant / Empty”
' declare variables
Dim financialReportBook As Workbook
Set financialReportBook = Workbooks.Open(FileNavPrompt.financialFilePath)
Dim financialSheet As Worksheet
Set financialSheet = FileNavPrompt.mainBook.Sheets("Financial Status")
Dim copyRange As String
copyRange = "A1:AA5000"
' unprotect workbook
financialReportBook.Unprotect ("{password removed}") ' unprotect workbook for copying
' pull data from report
financialSheet.Cells.Clear ' start with a clean slate
financialSheet.Range(copyRange).Value = financialReportBook.Sheets(1).Range(copyRange).Value
financialSheet.Range(copyRange).NumberFormat = financialReportBook.Sheets(1).Range(copyRange).NumberFormat
' close report
financialReportBook.Close False
' unrelated code continues below...
The underlying problem ended up being that the source workbook had it's sheet at index 1 attributed as xlVeryHidden. 最终的问题是,源工作簿的索引为1的表的属性为xlVeryHidden。 This caused confusion when I would address the first visible sheet as index 1 instead of index 2 as it actually was. 当我将第一个可见工作表称为索引1而不是实际的索引2时,这引起了混乱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.