Consulting

Results 1 to 3 of 3

Thread: Solved: VBA - Get Range of a Range ??? How?

  1. #1

    Solved: VBA - Get Range of a Range ??? How?

    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.

    [VBA] 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
    [/VBA]

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

  2. #2
    You are very close.

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

    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).

    ___________________________________
    g-
    gwkenny@Fin-ITSolutions.com
    ___________________________________

  3. #3
    gwkenny: It worked great, I am so happy. I'll keep learning, thanks again!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •