VBA If number in Range

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,124
Office Version
  1. 365
Platform
  1. Windows
Good Day,

If I have a number in range "B11:H180" upon Sheet1, I'd like to have a message box state:

VBA Code:
MsgBox "Cells in range!"


Then lock those cells from being edited.


How would I do this?

Thank you!
Pinaceous
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Do you mean a particular number, or just any number?


Lock which cells? The cells with the number, or the entire range?
Hey 6StringJazzer,

Do you mean a particular number, or just any number?
Any number &/or value.

Lock which cells?
The cells with the number &/or value in them and not its entire range.
 
Upvote 0
There is not enough detail regarding how you want to lock the cells but I can tell you how to do it. All cells are locked by default, and when you protect a sheet, edits are not allowed in locked cells. So if you protect the sheet, all cells in the whole sheet are prevented from being edited. Therefore the cells you want to lock are already locked. This code is only useful if you have cells that you have already explicitly unlocked.
VBA Code:
   ' unprotect the sheet to be able to lock any unlocked cells
   Sheet1.Unprotect
   
   With Sheet1.Range("B11:H180")
      Union(.SpecialCells(xlCellTypeConstants), .SpecialCells(xlCellTypeFormulas)).Locked = True
   End With

   ' reprotect the sheet
   Sheet1.Protect
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
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