Consulting

Results 1 to 9 of 9

Thread: Protect a column ins workbook with macros

  1. #1
    VBAX Mentor
    Joined
    Sep 2007
    Posts
    405
    Location

    Protect a column ins workbook with macros

    Hi,

    Any assistance on my requirement pls…

    Have a spreadsheet to with date time stamp macro to record the timings.
    But need is lock the column once this is done.

    For example I have a column (column 2) with the validation MET / MISS. Upon selecting a value in column 2 date time stamp will be captured in next column (column 3).

    Now I need to lock the column 3 (should not allow the date time stamp to edit).

    Since am using below macro for date time stamp am unable to protect the sheet.


    [VBA]
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    With Target
    If .Count > 1 Then Exit Sub
    If Not Intersect(Range("B2:A65000"), .Cells) Is Nothing Then
    Application.EnableEvents = False
    If IsEmpty(.Value) Then
    .Offset(0, 1).ClearContents
    Else
    With .Offset(0, 1)
    .NumberFormat = "dd mmm yy hh:mm:ss"
    .Value = Now
    End With
    End If
    Application.EnableEvents = True
    End If
    End With

    With Target
    If .Count > 1 Then Exit Sub
    If Not Intersect(Range("D2:J65000"), .Cells) Is Nothing Then
    Application.EnableEvents = False
    If IsEmpty(.Value) Then
    .Offset(0, 1).ClearContents
    Else
    With .Offset(0, 1)
    .NumberFormat = "dd mmm yy hh:mm:ss"
    .Value = Now
    End With
    End If
    Application.EnableEvents = True
    End If
    End With

    With Target
    If .Count > 1 Then Exit Sub
    If Not Intersect(Range("F2:J65000"), .Cells) Is Nothing Then
    Application.EnableEvents = False
    If IsEmpty(.Value) Then
    .Offset(0, 1).ClearContents
    Else
    With .Offset(0, 1)
    .NumberFormat = "dd mmm yy hh:mm:ss"
    .Value = Now
    End With
    End If
    Application.EnableEvents = True
    End If
    End With

    End Sub
    [/VBA]

    Help me out pls..

    -Sindhuja

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Lock the cells and protect the sheet?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Mentor
    Joined
    Sep 2007
    Posts
    405
    Location
    Hi Lucas,

    Thanks for the assistance..

    I have tried the option you have mentioned. If I protect the sheet the macro is not working..

    That is the problem and also I have to lock whole column…

    - Sindhuja

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    When your macro runs, have it unprotect the sheet.....run the code...reprotect the sheet.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Mentor
    Joined
    Sep 2007
    Posts
    405
    Location
    Used macros for the date time stamp.. So on the worksheet change event i used the coding…

    am not sure how to unprotect and reprotect the sheet using the coding…
    Used the commands

    Activesheet.unprotect at the start and
    Activesheet.protect at the end…

    Even then its not working out for me...

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    This seems to work. Open it and type something in the highlighted cell and then try to change the date in the c column.

    [VBA]Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Me.Unprotect
    With Target
    If .Count > 1 Then Exit Sub
    If Not Intersect(Range("B2:A65000"), .Cells) Is Nothing Then
    Application.EnableEvents = False
    If IsEmpty(.Value) Then
    .Offset(0, 1).ClearContents
    Else
    With .Offset(0, 1)
    .NumberFormat = "dd mmm yy hh:mm:ss"
    .Value = Now
    End With
    End If
    Application.EnableEvents = True
    End If
    End With

    With Target
    If .Count > 1 Then Exit Sub
    If Not Intersect(Range("D2:J65000"), .Cells) Is Nothing Then
    Application.EnableEvents = False
    If IsEmpty(.Value) Then
    .Offset(0, 1).ClearContents
    Else
    With .Offset(0, 1)
    .NumberFormat = "dd mmm yy hh:mm:ss"
    .Value = Now
    End With
    End If
    Application.EnableEvents = True
    End If
    End With

    With Target
    If .Count > 1 Then Exit Sub
    If Not Intersect(Range("F2:J65000"), .Cells) Is Nothing Then
    Application.EnableEvents = False
    If IsEmpty(.Value) Then
    .Offset(0, 1).ClearContents
    Else
    With .Offset(0, 1)
    .NumberFormat = "dd mmm yy hh:mm:ss"
    .Value = Now
    End With
    End If
    Application.EnableEvents = True
    End If
    End With
    Me.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
    , AllowFormattingCells:=True
    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    VBAX Mentor
    Joined
    Sep 2007
    Posts
    405
    Location
    Sorry if am bothering you again and again.. its allowing to enter only once in a sheet... its showing error message if imake another try...

    i have attached my original spreadsheet for the reference...

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    That's ok, I hope this helps you understand how things work. It only worked once on your sheet because you had the rest of column B locked when protected.

    I selected the entire sheet and unlocked all cells(right click-format cells-clear the check under the protection tab under lock or unlock.

    I then selected just column C and did the same except I locked them. then protect the sheet and run your items in column B, etc.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    VBAX Mentor
    Joined
    Sep 2007
    Posts
    405
    Location
    Hi Lucas...

    Now i understood and it worked out for me...

    thanks a lot....

    -Sindhuja

Posting Permissions

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