简体   繁体   中英

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. 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"

' 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. This caused confusion when I would address the first visible sheet as index 1 instead of index 2 as it actually was.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM