Prevent Saving File Based on Value in a Column

xlhelppls

New Member
Joined
Jun 17, 2016
Messages
17
Hello - I have a file where users enter in codes (col j) and they must only be a length of 10. I am using a formula (in col I) to check if they are length of 10, otherwise it will color the cell red and put in word "invalid." I want to prevent users from saving the file until they fix any codes that are not a length of 10 OR said another way, if the word "invalid" is present in any cells in Col I, then I don't want them to be able to save it until they do so.

I have searched for some posts which appear to be doing this, and tried to make relevant updates to it to fit my case, but I new to VB and keep getting errors. here is the code I am using:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Worksheets("Sheet1").Range("I20:I50") = "invalid" Then
    Cancel = True
    MsgBox "You cannot save until the code is in the right format", vbOKOnly
End If
End Sub

the error I am getting is:
run time error 13
type mismatch

and the line it highlights is:
If Worksheets("Sheet1").Range("I20:I50") = "invalid" Then

I thought I had the right updates made, but not sure what else needs to be changed?
any help is much appreciated!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
You cannot look for a value across a whole range of cells like that.
Try this instead:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Application.WorksheetFunction.CountIf(Worksheets("Sheet1").Range("I20:I50"), "invalid") > 0 Then
    Cancel = True
    MsgBox "You cannot save until the code is in the right format", vbOKOnly
End If
End Sub
 
Upvote 0
Joe4 - THANK YOU SO MUCH!
appreciate the quick reply and I tried it - works fabulously!
have a great day!!
 
Upvote 0
You are welcome!
Glad I was able to help!:)
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,824
Members
449,190
Latest member
rscraig11

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