Error Message when running code, Runtime Error 9, Subscript Out Of Range

Moseth

New Member
Joined
Sep 5, 2018
Messages
12
Hi, Can someone tell me why im getting this issue
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
 
If (Sheets("Supplier Checklist").Range("D24").Value) = "No" And Sheets("Supplier Checklist").Range("E21") = "" Then
    SaveAsUI = False
    MsgBox "Please define the non compliance/PCA where a No is entered", vbExclamation
    Sheets("Sheet1").Range("D13").Select
End If
End Sub
 
Last edited by a moderator:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
One of your sheet names must be wrong.
 
Upvote 0
Thankyou,

If I want to extend the range from D21 to D24 -"No" and Range E21, E23, E24= ""


If (Sheets("Supplier Checklist").Range("D21").Value) = "No" And Sheets("Supplier Checklist").Range("E21") = "" Then
SaveAsUI = False
MsgBox "Please define the non compliance/PCA where a No is entered", vbExclamation
Sheets("Supplier Checklist").Range("E21").Select
End If

How do I do this?
 
Upvote 0
Meaning what - all of D21:D24 are No, or any of them? Same for column E.
 
Upvote 0
so if D21 is no and any one of E21, G21,H21,I21 is Blank then cancel save
so if D22 is no and any one of E22, G22,H22,I22 is Blank then cancel save
going all the way to D129 in this format, missing a few rows in the process
 
Upvote 0
Try this:

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim rw as long
with Sheets("Supplier Checklist")
for rw = 21 to 129
If .Cells(rw, "D").Value = "No" And (Len(.cells(rw, "E").Value) = 0 Or Len(.cells(rw, "G").Value) = 0 Or Len(.cells(rw, "H").Value) = 0 Or Len(.cells(rw, "I").Value) = 0) Then
    SaveAsUI = False
    MsgBox "Please define the non compliance/PCA where a No is entered", vbExclamation
    .Cells(rw, "D").Select
    Exit For
End If
Next
End With
End Sub
 
Upvote 0
Thankyou!,
Finally within 21 to 129, I dont want the code being applied to rows 25,32,36,39,46,52,60,69,74,79,86,92,94,96,99,103,110,115 , Appreciate your help
 
Upvote 0
Oh, it's one of those expanding questions... ;)

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim rw as long
with Sheets("Supplier Checklist")
for rw = 21 to 129
Select case rw
case 25,32,36,39,46,52,60,69,74,79,86,92,94,96,99,103,110,115
' do nothing
case else
If .Cells(rw, "D").Value = "No" And (Len(.cells(rw, "E").Value) = 0 Or Len(.cells(rw, "G").Value) = 0 Or Len(.cells(rw, "H").Value) = 0 Or Len(.cells(rw, "I").Value) = 0) Then
    SaveAsUI = False
    MsgBox "Please define the non compliance/PCA where a No is entered", vbExclamation
    .Cells(rw, "D").Select
    Exit For
End If
end select
Next
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,311
Members
449,080
Latest member
jmsotelo

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