+ Reply to Thread
Results 1 to 7 of 7

Modify Auto Clear - VB Code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-22-2005
    MS-Off Ver
    Office 365
    Posts
    342

    Modify Auto Clear - VB Code

    Hello,

    I have the following code which I am attempting to recycle into another workbook

    Private Sub Worksheet_Change(ByVal Target As Range)
    ' If target is not within D3:D350 then quit
    If Intersect(Target, Range("D3:D350")) Is Nothing Then Exit Sub
    ' Disable event handling while THIS macro is changing the sheet
    Application.EnableEvents = False
    ' if the target cell has just been cleared then clear the entire row
    ' otherwise put the date in column "H"
    Select Case Target.Value
    Case Is = Empty
    Range(Cells(Target.Row, "H"), Cells(Target.Row, "G")).ClearContents
    Case Is <> Empty
    Target.Offset(0, 4).Value = Date
    End Select
    ' re-enable event handling and quit
    Application.EnableEvents = True
    I need to modify this a bit and I am coming up short ...

    First .. when a number is entered into D3:D350 the current date will prefill on H3:H350
    And as per above if the number in D3:D350 is deleted it then auto clears H & G ... However I would like to clear the entire row.

    So if I Delete the number in D3:D350 I would like all data in A, C, E, F, G & H to autoclear.
    Last edited by SVTF; 07-01-2012 at 11:32 AM.

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Modify Auto Clear - VB Code

    Hi,

    Rows(Target.Row).ClearContents
    instead of

    Range(Cells(Target.Row, "H"), Cells(Target.Row, "G")).ClearContents
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Forum Contributor
    Join Date
    08-22-2005
    MS-Off Ver
    Office 365
    Posts
    342

    Re: Modify Auto Clear - VB Code

    Thank You worked like a charm

  4. #4
    Forum Contributor
    Join Date
    08-22-2005
    MS-Off Ver
    Office 365
    Posts
    342

    Re: Modify Auto Clear - VB Code

    Slight modification needed in this autoclear code ...

    I have a vlookup in column B ...

    Can we tweak the following code to exclude clearing Column B ?

    Because B will clear once there is no data in C

    I don't want the below to earse by vlook up formula in Column B

    Quote Originally Posted by abousetta View Post
    Hi,

    Rows(Target.Row).ClearContents
    Last edited by SVTF; 07-01-2012 at 11:33 AM.

  5. #5
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Modify Auto Clear - VB Code

    Maybe try:

    Cells(Target.Row,"A").ClearContents
    Range(Cells(Target.Row, "C"), Cells(Target.Row, "XFD")).ClearContents

  6. #6
    Forum Contributor
    Join Date
    08-22-2005
    MS-Off Ver
    Office 365
    Posts
    342

    Re: Modify Auto Clear - VB Code

    Quote Originally Posted by abousetta View Post
    Maybe try:

    Cells(Target.Row,"A").ClearContents
    Range(Cells(Target.Row, "C"), Cells(Target.Row, "XFD")).ClearContents
    The above did something funky ... so I just did this messy but it works:

    Private Sub Worksheet_Change(ByVal Target As Range)
    ' If target is not within D3:D350 then quit
      If Intersect(Target, Range("D3:D350")) Is Nothing Then Exit Sub
      ' Disable event handling while THIS macro is changing the sheet
      Application.EnableEvents = False
      ' if the target cell has just been cleared then clear the entire row
      ' otherwise put the date in column "H"
      Select Case Target.Value
      Case Is = Empty
      Cells(Target.Row, "A").ClearContents
      Cells(Target.Row, "C").ClearContents
      Cells(Target.Row, "E").ClearContents
      Cells(Target.Row, "F").ClearContents
      Cells(Target.Row, "G").ClearContents
      Cells(Target.Row, "H").ClearContents
      Case Is <> Empty
           Target.Offset(0, 4).Value = Date
           Select Case Month(Target.Offset(0, 4))
           Case 11, 12, 1
           Cells(Target.Row, 1).Value = "Q1"
           Case 2, 3, 4
           Cells(Target.Row, 1).Value = "Q2"
           Case 5, 6, 7
           Cells(Target.Row, 1).Value = "Q3"
           Case 8, 9, 10
           Cells(Target.Row, 1).Value = "Q4"
     End Select
     End Select
     ' re-enable event handling and quit
     Application.EnableEvents = True
    End Sub

    *** Part of the code above clears the data in cells when the data is deleted from D
    *** Can a pop up be added to asK

    "Are you sure you want to clear the data in this row"
    With an option:
    Yes - exceute code above
    No - Does not exectute code to clear
    Last edited by SVTF; 07-01-2012 at 01:54 PM.

  7. #7
    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: Modify Auto Clear - VB Code

    Another way:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim r           As Range
        Dim cell        As Range
        Dim sQtr        As String
    
        Set r = Intersect(Target, Range("D3:D350"))
    
        If Not r Is Nothing Then
            sQtr = "Q" & WorksheetFunction.Lookup(Month(Date), Array(1, 2, 5, 8, 11), Array(1, 2, 3, 4, 1))
            On Error GoTo Oops
            Application.EnableEvents = False
    
            For Each cell In r
                If IsEmpty(cell.Value) Then
                    Rows(cell.Row).Range("A1,C1,E1:H1").ClearContents
                Else
                    Cells(cell.Row, "H").Value = Date
                    Cells(cell.Row, "A") = sQtr
                End If
            Next cell
    
    Oops:
            Application.EnableEvents = True
        End If
    End Sub
    Entia non sunt multiplicanda sine necessitate

+ 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