A better way for If and Or condition in VBA

danjuma

Active Member
Joined
Sep 21, 2008
Messages
251
Hello. I have the VBA code below. What it does is check if the data entered in a cell in column R is valid and then lock the cell and cells on the row from columns A to Q. What I am after is a better/more efficient for the condition "If Target = "123456" Or Target = "3245671" Or Target = "4227716" Then" as there are up to 20 different set of numbers I want to validate else I will have a very long If Or statement. Thanks


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("R:R")) Is Nothing Then Exit Sub
 ActiveSheet.Unprotect Password:="mypassword"
 If Target = "123456" Or Target = "3245671" Or Target = "4227716" Then
 Range("A" & Target.Row & ":R" & Target.Row).Locked = True
 End If
 ActiveSheet.Protect Password:="1855nom"
End Sub
 
Cheers, hope it works for you. :)
Hello Peter_SSs, the code works fine, thank you. Just one little issue, for some reason when typing data into the cells in a worksheet (that the code is on), the text does not show (or to be more specific, it shows as white font, so you can't see what you are actually typing on a white sheet) until you finish typing and press enter/click out of the cell, then the text shows (black text). Not sure why this is? Thanks
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
That is unrelated to the code I suggested. I think you must have some special formatting in that column, or Conditional Formatting, or other vba code that if affecting it.
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,094
Latest member
bsb1122

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