Locking a cell with VBA if another cell contains information

Lizabeta

New Member
Joined
Oct 24, 2008
Messages
32
Hi! The linked thread shows almost exactly what I'm looking to accomplish.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
http://www.mrexcel.com/forum/showthread.php?p=1572477 <o:p></o:p>
<o:p></o:p>
But as I know very little about VBA, I have a few questions...<o:p></o:p>
<o:p></o:p>
I have two different sites entering information onto a spreadsheet. If the first site enters information in Column B, I need column C locked so the second site doesn't write try entering useless information. If the first site does not enter information, column C stays unlocked.<o:p></o:p>
<o:p></o:p>
Column B does not actually contain numerical values. It contains references to numerical values from another workbook.
<o:p></o:p>
<o:p></o:p>
So the following is what VBA Noob suggested in the other thread:
<o:p></o:p>
<o:p></o:p>
Try an event macro > right click sheet tab > select view code > paste in the below.
If A1 formula returns 10 then the cell is locked.
<o:p></o:p>
<o:p></o:p>

Code:
Private Sub Worksheet_Calculate()
Dim Rng As Range<o:p></o:p>
Set Rng = Range("A1")<o:p></o:p>
<o:p></o:p>
Select Case Rng<o:p></o:p>
Case Is = 10<o:p></o:p>
Rng.Locked = True<o:p></o:p>
Case Else<o:p></o:p>
Rng.Locked = False<o:p></o:p>
End Select<o:p></o:p>
End Sub
<o:p></o:p>
HTH<o:p></o:p>
<o:p></o:p>
VBA Noob
<o:p></o:p>
<o:p></o:p><o:p></o:p>
So… how could I make the following changes and, is the fact that there is a reference to another workbook rather than an actual numerical value going to make a difference?
And lastly, how to I apply this to all the rows, not just C1 & B1, but to C2 & B2 all the way down?

<o:p></o:p>
Code:
[SIZE=3][FONT=Times New Roman]Private Sub Worksheet_Calculate()[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]Dim Rng As Range<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]Set Rng = Range("C1")<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]Select Case Rng<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] Case Is = [COLOR=blue]B1 is Greater than 0 or Is not blank (whichever would work better)<o:p></o:p>[/COLOR][/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]     Rng.Locked = True<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] Case Else<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]     Rng.Locked = False<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]End Select<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]End Sub[/FONT][/SIZE]
<o:p></o:p>
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,215,523
Messages
6,125,318
Members
449,218
Latest member
Excel Master

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