+ Reply to Thread
Results 1 to 10 of 10

Understanding this VBA code

  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.

    Please Login or Register  to view this content.

    First off, what does the following mean:

    Please Login or Register  to view this content.

    And this?

    Please Login or Register  to view this content.

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

    Please Login or Register  to view this content.

    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

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: Help with VBA code

    PW is the worksheet Protection password

    cl is a range in the range
    basically for each cell in the range rBookings

    this code searches the range and if it is <= today's date the it will lock the cells


    What are you trying to do?

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Understanding this VBA code

    The code with the Loop deals with Merged cells, something to avoid. Never use Merged Cells.

    The password code uses a Constant to store the password, this is then used in the code to unprotect/protect the sheet allowing the macro to lock the cells.

    I think this would be better for you
    Please Login or Register  to view this content.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

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

    Re: Understanding this VBA code

    Quote Originally Posted by davesexcel View Post
    PW is the worksheet Protection password

    cl is a range in the range
    basically for each cell in the range rBookings

    this code searches the range and if it is <= today's date the it will lock the cells


    What are you trying to do?
    I work for a large organisation and we do data processing. I'm building a future capacity plan for my company.

    I have 11 teams who each need to input forecasts. Each forecast (cell) is for a weekly period. The forecasts are proportionate to staffing on each team during each week. Staffing fluctuates according to employees taking holidays, which then in turn lowers or raises forecasts.

    I've built spreadsheets that deal with leave which then inform the capacity plan. But, if a staff member calls in sick, that is entered on the leave chart, which then affects the forecast, which I want to avoid.

    So I need a module that will lock the forecast in place a week before it is due, so that if staff do call in sick, the forecast itself isn't affected.

    Yours and royUK's responses have gone a long way toward me sorting it out. I'm going to have a stab at it now and see if it works. In short, a very big thank you.

    Quote Originally Posted by royUK View Post
    The code with the Loop deals with Merged cells, something to avoid. Never use Merged Cells.

    The password code uses a Constant to store the password, this is then used in the code to unprotect/protect the sheet allowing the macro to lock the cells.

    I think this would be better for you
    Please Login or Register  to view this content.
    Again, many thanks for the info. I'll try it out and see how it goes.

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

    Re: Understanding this VBA code

    OK, new question:

    Is there a way of locking a cell whose value is the result of a calculation, in such a way that, once a certain trigger occurs, the value cannot be altered even if the cells used for the calculation are?

    E.G.

    A3 = sum(A1+A2)

    Then once the trigger occurred, no matter how much one changes the other cells (A1 or A2), A3 remains the same.

    More specifically, I'm asking in terms of time-dependancy. So that after a specific date, A3 cannot be altered no matter how much A1 or A2 is.

  6. #6
    Registered User
    Join Date
    11-05-2007
    Posts
    54

    Re: Understanding this VBA code

    paste special values and protect?!

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

    Re: Understanding this VBA code

    Quote Originally Posted by karl1985 View Post
    paste special values and protect?!
    Sadly not. The two cells making up the formula must remain editable, but the outcome must stay locked after a certain date.

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Understanding this VBA code

    Do you mean with a formula?

    This should be a new question as the other one appears to be solved

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

    Re: Understanding this VBA code

    Quote Originally Posted by royUK View Post
    Do you mean with a formula?

    This should be a new question as the other one appears to be solved
    Far from it!

    I'm just easing you in!

    Because I've got an office to run, it's difficult for me to check up on the forum regularly, as I'm constantly being called into meetings.

    When I get home though, it'll be a lot easier to provide details and even put up sample sheets. That way, I can illustrate exactly what I'm facing.

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Understanding this VBA code

    I don't know what you mean, you appear to have two questions in this Thread

+ Reply to Thread

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