Open file with file picker, get data and close file

wpryan

Well-known Member
Joined
May 26, 2009
Messages
534
Office Version
  1. 365
Platform
  1. Windows
Hi All, I have a situation I would like to solve... I want to run a macro where I want to open a file, get some data and copy it, and then close the workbook without saving. The workbook to select is password protected and I am getting a "run-time error 9: subscript out of range" error on the line to unlock the worksheet (selectedWorkbook.Worksheets("shInstructions").Unprotect Password:=SelWBPW)
Code is below:
VBA Code:
Sub AutoPowerMeasurement()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    Dim pw As String: pw = shRelease.Range("A14").Value

    shLaserPower.Unprotect Password:=pw
    
    Dim selectedWorkbook As Workbook
    Dim selectedFilePath As Variant
    
    ' Open the file picker dialog and get the selected file path
    selectedFilePath = Application.GetOpenFilename("Excel Files (*.xlsx;*.xlsm),*.xlsx;*.xlsm")
    
    If VarType(selectedFilePath) = vbBoolean And selectedFilePath = False Then ' Check if the user cancelled the file picker
        Exit Sub
    End If
        
    ' Open the selected workbook
    Set selectedWorkbook = Workbooks.Open(selectedFilePath)
    Dim SelWBPW As String: SelWBPW = "21.05.2021"
    selectedWorkbook.Worksheets("shInstructions").Unprotect Password:=SelWBPW
    
    ' Copy the value from shInstructions, cell C9, to shLaserPower, cell B33
    ThisWorkbook.Sheets("shLaserPower").Range("B33").Value = selectedWorkbook.Worksheets("shInstructions").Range("C9").Value 'Park Max
    ThisWorkbook.Sheets("shLaserPower").Range("C33").Value = selectedWorkbook.Worksheets("shInstructions").Range("C10").Value 'Park Min
    ThisWorkbook.Sheets("shLaserPower").Range("B34").Value = selectedWorkbook.Worksheets("shInstructions").Range("D9").Value 'OS Max
    ThisWorkbook.Sheets("shLaserPower").Range("C34").Value = selectedWorkbook.Worksheets("shInstructions").Range("D9").Value 'OS Min
    ThisWorkbook.Sheets("shLaserPower").Range("B35").Value = selectedWorkbook.Worksheets("shInstructions").Range("E9").Value 'OD Max
    ThisWorkbook.Sheets("shLaserPower").Range("C35").Value = selectedWorkbook.Worksheets("shInstructions").Range("E9").Value 'OD Min
    ThisWorkbook.Sheets("shLaserPower").Range("B36").Value = selectedWorkbook.Worksheets("shInstructions").Range("F9").Value 'OS_Other Max
    ThisWorkbook.Sheets("shLaserPower").Range("C36").Value = selectedWorkbook.Worksheets("shInstructions").Range("F9").Value 'OS_Other Min
    ThisWorkbook.Sheets("shLaserPower").Range("B37").Value = selectedWorkbook.Worksheets("shInstructions").Range("G9").Value 'OD_Other Max
    ThisWorkbook.Sheets("shLaserPower").Range("C37").Value = selectedWorkbook.Worksheets("shInstructions").Range("G9").Value 'OD_Other Min
    ' Close the selected workbook without saving changes
    selectedWorkbook.Worksheets("shInstructions").Protect Password:=SelWBPW
    selectedWorkbook.Close False
   
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
    shLaserPower.Protect Password:=pw
    
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
It seems that the worksheet you are trying to unprotect doesn't exist...
When you get the error, enter the Debug mode, then check that a workbook named exactely "shInstructions" exists in the just-opened workbook

But I seem that you only READ info from that worksheet, so you shouldn't even need to unprotect it to proceed.
As well you don't need to re-protect the sheet, since you'll close the workbook without saving it
 
Upvote 0
It seems that the worksheet you are trying to unprotect doesn't exist...
When you get the error, enter the Debug mode, then check that a workbook named exactely "shInstructions" exists in the just-opened workbook

But I seem that you only READ info from that worksheet, so you shouldn't even need to unprotect it to proceed.
As well you don't need to re-protect the sheet, since you'll close the workbook without saving it
Thanks for your response. When I step through the code, the selected workbook opens and I can see the worksheet:
1679485796363.png

...I still get the error...
 
Upvote 0
Well, "shInstructions" is the Codename, whereas the Name is "Instructions"; thus you need to use selectedWorkbook.Sheets("Instructions") or selectedWorkbook.shIntructions
 
Upvote 0
Solution
You would need to use
VBA Code:
selectedWorkbook.Worksheets("Instructions").Range("C9").Value
You can only directly refer to the codename of sheets in the workbook with the code.
 
Upvote 0

Forum statistics

Threads
1,216,030
Messages
6,128,405
Members
449,448
Latest member
Andrew Slatter

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top