+ Reply to Thread
Results 1 to 12 of 12

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

Hybrid View

  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
    2010, 2019
    Posts
    40,689

    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:

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Dim rB      As Range
        Dim cell    As Range
        
        With Worksheets("Sheet1") ' change as appropriate
            .Protect Password:="secret", UserInterfaceOnly:=True
            Set rB = Intersect(.Columns("B"), .UsedRange)
            If rB Is Nothing Then Exit Sub
            For Each cell In rB
                cell.EntireRow.Locked = IsNumeric(cell.Value) And Not IsEmpty(cell.Value)
            Next cell
        End With
    End Sub
    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
    cell.EntireRow.Locked = IsNumeric(cell.Value) And Not IsEmpty(cell.Value)
    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
    2010, 2019
    Posts
    40,689

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