Get data from closed workbook that is password protected

Posted on 2012-09-04
Medium Priority
Last Modified: 2012-09-04
I need to get some data from this range in my closed workbook that is password protected:


How can I retrieve this data and paste special (values) into my active workbook into range A2:C9?
Question by:Lawrence Salvucci
  • 2
  • 2
LVL 11

Expert Comment

ID: 38364757

Workbooks.Open Filename:="Full Path Name", Password:="Apassword",  _ WriteResPassword:="Apassword"

Author Comment

by:Lawrence Salvucci
ID: 38364763
I also need to copy and paste special a specific range from this closed workbook and paste the values into my active worksheet.
LVL 17

Accepted Solution

andrewssd3 earned 2000 total points
ID: 38365786
Try this - assuming you want to copy to the active sheet, and your source data is on the first worksheet of the protected workbook:
Public Sub CopyFromPwDoc()

    Dim rngCopyTo As Range
    Dim rngCopyFrom As Range
    Dim wbkProtected As Workbook
    Set rngCopyTo = ActiveSheet.Range("A2:C9")
    Set wbkProtected = Application.Workbooks.Open(FileName:="c:\users\stuart\desktop\protected.xlsx", Password:="Password1")
    Set rngCopyFrom = wbkProtected.Worksheets(1).Range("M22:O29")
    rngCopyTo.Value = rngCopyFrom.Value
    wbkProtected.Close savechanges:=xlDoNotSaveChanges

End Sub

Open in new window

If it's not on the first sheet in the protected workbook, use Worksheets("MySheetName") instead.  Obviously substitute your own file name and password
LVL 17

Expert Comment

ID: 38365801
Note that this does not use copy/paste - in VBA it's nearly always better just to assign the values from one range to another.  Copy/paste can be useful if you need to take formats as well, but generally is an unnecessary overhead.

Author Closing Comment

by:Lawrence Salvucci
ID: 38366069
Thanks Andrew! Much appreciated!

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question