简体   繁体   English

VBA:将单元格从一个工作簿复制到另一个工作簿将产生空白结果

[英]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.

相关问题 将单元格范围从一个工作簿复制到另一个工作簿会产生400错误 - Copying range of cells from one workbook to another yields 400 error VBA从一个工作簿复制到另一工作簿 - VBA Copying from one workbook to another workbook 在一个工作簿中查找并复制单元格 - Finding and Copying Cells from One Workbook to Another VBA 问题 - 从一个工作簿复制到另一个? - Issue with VBA - Copying from one workbook to another? VBA将数据从一个工作簿复制到另一个工作簿 - VBA copying data from one workbook to another VBA 根据条件将数据从一个工作簿复制到另一个工作簿中的特定单元格 - VBA copying data from one workbook to specific cells in another, based on criteria 使用vba将单元格从一个工作簿复制到excel中的另一个工作簿 - Copying the cell from One workbook to another workbook in excel using vba 在Excel中将一个工作簿复制到另一个工作簿并将单元格数据复制到另一个工作簿 - Copying in Excel from one workbook to another and copying cells data to another workbook 将数据从一个工作簿复制到另一个工作簿会粘贴在随机单元格中 - Copying data from one workbook to another gets pasted in random cells VBA-将工作表从一个工作簿复制到另一个工作簿(值,而不是公式) - VBA - Copying worksheet from one workbook to another (values, not formulas)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM