Consulting

Results 1 to 6 of 6

Thread: Cant delete contents of a cell ... VBA issue

  1. #1

    Cant delete contents of a cell ... VBA issue

    This should be simple.....

    I have a sheet that ..when a entry is made in column B, the date is entered in column H.. all that works well until I found that if I remove the data in column B-G, the date in H stays.. and everytime I delete it the macro below runs and puts the date back... and I don't really want it to work that way.

    Here is the code I am currently using

    Private Sub Worksheet_Change(ByVal Target As Range)
    ActiveSheet.Unprotect
        If Target(1, 1).Row > 1 Then
           With Cells(Target(1, 1).Row, "h")
               .Value = Date
            End With
            End If
       ActiveSheet.Protect
       End Sub
    Any suggestions ?

    Cheers

    Andrew

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim r As Range
        Dim c As Range
        
        Set r = Intersect(Target, Columns("B"))
        If r Is Nothing Then Exit Sub
         
        ActiveSheet.Unprotect
        Application.EnableEvents = False
        For Each c In r
            If c.Row > 1 Then
                With c.EntireRow
                    If WorksheetFunction.CountA(.Range("B1:G1")) > 0 Then
                        .Range("H1").Value = Date
                    Else
                        .Range("H1").ClearContents
                    End If
                End With
            End If
        Next
        Application.EnableEvents = True
        ActiveSheet.Protect
    '
    End Sub

  3. #3
    Hi Mana and thank you for the quick reply. I have checked it on the sheet .. and also in a new book but it doesn't seems to either add a date or remove when I empty the cell in column B.

    I will keep looking ... but thank you again

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Private Sub Worksheet_Change(ByVal Target As Range) 
      If Target.Row > 1 and target.column=2 Then
        ActiveSheet.Unprotect         
        for each cl  in target
          cl.offset(,6)= iif(cl.value="","",date) 
        next
        ActiveSheet.Protect
      End If 
    End Sub

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If you protect the sheet, as you do in your code, you can't make a change to any cells in that sheet, so the code never fires. You need to unlock cells that you can modify.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    SNB... That works .. many thanks

Posting Permissions

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