Results 1 to 10 of 10

Understanding this VBA code

Threaded View

  1. #1
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2002
    Posts
    5

    Understanding this VBA code

    This is my first post, so hello to all!

    I registered because there was a specific problem that I wanted solving. The problem was locking a cell after a specific date has been reached or passed.

    There was an old thread here where a couple of users had written some VBA code.

    However, because the spreadsheet and situation is different for me, the code can't be used exactly as it is.

    Having investigated, I've managed to understand some parts of the code, but was wondering if anyone would be willing to enlighten me on the rest.

    Option Explicit
    
    Private Sub Workbook_Open()
        Const PW   As String = "secret"
        Dim rBookings As Range
        Dim cl     As Range
        Dim mArea  As Range
        With Sheets("Availability")
            .Unprotect PW
            Set rBookings = .Range(.Cells(7, 4), .Cells(7675, 10)).SpecialCells(xlCellTypeConstants)
            For Each cl In rBookings
                Set mArea = .Cells(cl.Row, 1).MergeArea
                If mArea.Cells(1, 1).Value <= Date Then cl.Locked = True
            Next cl
            .Protect PW
        End With
    End Sub

    First off, what does the following mean:

    Option Explicit
    
    Private Sub Workbook_Open()
        Const PW   As String = "secret"

    And this?

    Unprotect PW

    Now, this is where it gets a bit hairy for me.

    For Each cl In rBookings
                Set mArea = .Cells(cl.Row, 1).MergeArea
                If mArea.Cells(1, 1).Value <= Date Then cl.Locked = True
    Next cl

    Other than an earlier statement saying that it's a variable, I can't see any other definition of 'cl'. So how does the 'rBooking' variable interact with the 'cl' variable?

    I assume the 'Set' statement is defining something for 'mArea'. But what exactly is it defining?

    I can see the purpose of the 'If' statement, but not how it fulfills that purpose.

    And a question not related to the actual code. The values in the cells I want to apply this code to are imported from other spreadsheets where they are the result of various formulae. If the date trigger is reached and the cell is locked, if the data in the other spreadsheets are changed, will the value remain the same in the locked cell?

    My apologies for a a mammoth opening post, and the rather involved question, but I may be repeating this exercise over the next few months for work, and rather than repost the same question, I thought I'd try and understand how it works.
    Last edited by davesexcel; 10-28-2010 at 05:32 AM. Reason: Help is not good for a title

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1