PDA

View Full Version : Solved: VBA - Get Range of a Range ??? How?



DigitalDan
05-27-2008, 06:47 PM
I am pretty new at VBA, and am familiar with the Indirect function of excel, but confused as to how to do it with VBA as it doesn't appear to work.

When the workbook opens, I want to always automatically Lock cells A1:A10, and then unlock only the Range of the cell #'s of which is contained literally in Cells B1 & B2.

For instance if B1="A1", and B2="A5", then I want have Cells A1:A5 unlock themselves. I tried the following below, but it doesn't work for some reason. I have spent hours trying to figure out the simple answer. :)

Private Sub Workbook_Open()
ActiveSheet.Protect Contents:=True, userInterfaceOnly:=True
Range("A1:A10").Select
Range("A1:A10").Locked = True
Range(Range("B1"), Range("B2")).Select
Range(Range("B1"), Range("B2")).Locked = False

End Sub


I would be so happy as this will finish my project once I figure this thing out... Thanks in advance!

gwkenny
05-27-2008, 09:16 PM
You are very close.

Range("A1:A10").Locked = True
Range(Range("B1").Text, Range("B2").Text).Locked = False

You do not need the lines with "select" :)

Just perform the actions on the ranges directly and your good to go. FYI the reason your code didn't work is because it thought the beginning and end of the range you wanted to unlock was literally cells B1 and B2. It did not know you wanted the contents (which is what .Text gives you).

:)

DigitalDan
05-27-2008, 09:50 PM
gwkenny: It worked great, I am so happy. I'll keep learning, thanks again!! :thumb