+ Reply to Thread
Results 1 to 12 of 12

Lock a row with an entry in Col B on saving or closing workbook

  1. #1
    Registered User
    Join Date
    02-12-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    32

    Lock a row with an entry in Col B on saving or closing workbook

    Hi,

    My first posting so apologies in advance if I offend the rules.

    I have a spreadsheet for recording and summarising particular transactions. Column B records the date and Col C details the transaction type (from a data valdation list). Using conditional formatting the cell (in the same row) to whch the transaction value is to be entered is highlighted.

    Other cells in the row contain formulas and are locked and the sheet is password protected.

    On saving or closing the workbook I want to lock all the cells (used or unused) in any row in which there is a date in column B. How can I do this?

    I'm new to vba so detail of what code goes where will be appreciated.

    Thanks.
    Last edited by glenin; 02-17-2009 at 04:13 AM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Lock a row with an entry in Col B on saving or closing workbook

    Press Alt+F11 to open the Visual Basic Editor, double-click ThisWorkbook in the Project window, and paste this code:

    Please Login or Register  to view this content.
    Change the sheet name and password appropriately.

    Save as type xlsm.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    02-12-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Lock a row with an entry in Col B on saving or closing workbook

    Thanks for the speedy response.

    After saving and on trying to close the worrkbook I'm getting the following message.

    Run-time error '1004'
    Unable to set the Locked property of the Range class.

    When I hit the debug button on the message panel the 4th last line of the code is highlighted
    Please Login or Register  to view this content.
    When I hover the mouse cursor over the IsNumeric(cell.Value) section of that highlighed code line a floating message box box identifies the cell value as the first of the header rows in column B (all the header rows are text).

    Maybe the IsNumeric bit is the issue? If so, is there some sort of "is anything" statement that can be used instead - locking the (text) header rows is not a problem.
    Last edited by VBA Noob; 02-16-2009 at 06:22 PM.

  4. #4
    Registered User
    Join Date
    02-12-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Lock a row with an entry in Col B on saving or closing workbook

    I've progressed this a bit further.

    I've got around what appears to be the text problem by simply inserting a hidden column and assigning a numeric value if the cell in column B is either text or numeric. I've changed the column reference in the code to the hidden column and now rows with a value in column B are locking as required.

    Two remaining problems.

    1. After saving and trying to close the workbook I'm getting the "Do you want to the changes" message box.

    2. On re-opening the workbook the formulas in protected cells in rows that do not have a date in column B are now unlocked.

    Any asistance with 1 & 2 will be appeciated.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Lock a row with an entry in Col B on saving or closing workbook

    Post a workbook.

  6. #6
    Registered User
    Join Date
    02-12-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Lock a row with an entry in Col B on saving or closing workbook

    Workbook attached with your code included but no other changes, Password set to pw.
    Attached Files Attached Files

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Lock a row with an entry in Col B on saving or closing workbook

    Try this. Merged cells in Excel are a PITA.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    02-12-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Lock a row with an entry in Col B on saving or closing workbook

    Many thanks for your patience and help.

    Rows are now locking but two problems remain.

    I've copied the code to the Workbook panel of ThisWorkbook. After saving & on hitting either the workbook or excel close button (top right) the rows with a date in col B lock ok but neither the workbook or excel will actually close.

    However if I change the second last line of the code to Cancel = False the workbook and excel can be closed in the normal way. Then on reopening the workbook the rows with a date in col B are locked ok, but the cells in columns H & I in rows without a date in col B are now unlocked. Because columns H & I contain formulas I need them to be locked at all times.

    Any suggestions on how to keep cols H & I locked? What does the Cancel = True or Cancel = False line of the code do? (sorry if this is a dumb question but I'm struggling with this vba stuff)

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Lock a row with an entry in Col B on saving or closing workbook

    The Cancel line was a holdover from testing, sorry. Try this:
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    02-12-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Lock a row with an entry in Col B on saving or closing workbook

    That's the trick - works perfectly.

    Thanks again for your effort.

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Lock a row with an entry in Col B on saving or closing workbook

    You’re welcome. Would you please mark the thread as Solved?

    Click the Edit button on your first post in the thread

    Click Go Advanced, select [SOLVED] from the Prefix dropdown, then click Save Changes

  12. #12
    Registered User
    Join Date
    02-12-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Lock a row with an entry in Col B on saving or closing workbook

    Done - apologies for oversight but I'm getting the hang of this.

+ 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