Run-time error '9' Subscript out of range

optionsactuelles

New Member
Joined
Sep 22, 2017
Messages
2
My coworker has built the code and I'm only trying to save it to a specific site, this is the code:

Code:
Public Sub SaveAs_TL_ETI_Y()
Dim NewBook As Workbook
Dim varResult As Variant
Dim E_F, E_SN, P_PN, P_SN, D_F As Variant
Dim FName As Variant
Dim LockBook As Workbook

'Saves TL_ETI_Y as new workbook
E_F = ThisWorkbook.Sheets("TL_ETI_Y").Range("F16").Value
E_SN = ThisWorkbook.Sheets("TL_ETI_Y").Range("C13").Value
P_PN = ThisWorkbook.Sheets("TL_ETI_Y").Range("B14").Value
P_SN = ThisWorkbook.Sheets("TL_ETI_Y").Range("F14").Value
D_F = ThisWorkbook.Sheets("TL_ETI_Y").Range("B21").Value

'Sets new workbook name to pre-specified name
FName = E_F & " - " & E_SN & " - " & P_PN & " - " & P_SN & " - " & D_F
varResult = Application.GetSaveAsFilename(InitialFileName:=FName, FileFilter:="Excel Workbook (*.xlsx), *.xlsx")

'Creates new workbook with the above mentioned name
Set NewBook = Workbooks.Add
ThisWorkbook.Sheets("TL_ETI_Y").Copy Before:=NewBook.Sheets(1)
NewBook.SaveAs FileName:=varResult


With Application.FileDialog(msoFileDialogSaveAs)
  .AllowMultiSelect = False
  .InitialFileName = "(mydirectory)"
  If .Show = -1 Then .Execute
End With


'Locks all "question" cells to prevent editing
Set LockBook = Workbooks(FName & ".xlsx")
LockBook.Sheets("TL_ETI_Y").Cells.Locked = False
LockBook.Sheets("TL_ETI_Y").Range("A1:J1").Locked = True
LockBook.Sheets("TL_ETI_Y").Range("A2:B10").Locked = True
LockBook.Sheets("TL_ETI_Y").Range("C2:J3").Locked = True
LockBook.Sheets("TL_ETI_Y").Range("C4:D6").Locked = True

After the Set LockBook there it continues. What I added was the With Application.FileDialog part.
Everything works, it saves on the computer itself and it saves onto the link I want it to save. However, after it saves, I get the error '9' and when I go on "debug" it highlights the line

Code:
Set LockBook = Workbooks(FName & ".xlsx")

How do I fix this??
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
The error means there is no workbook open with that name.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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