+ Reply to Thread
Results 1 to 13 of 13

Delete Cell Contents when macro runs

Hybrid View

  1. #1
    Registered User
    Join Date
    06-17-2013
    Location
    Salt Lake City, UT
    MS-Off Ver
    Excel 2011
    Posts
    26

    Delete Cell Contents when macro runs

    Below is my current code that is working fine. The Macro runs when I type a value into cell G2, it then adds the value of G2 to cell G4, G5, G6, G7, or G8 depending on the date. What code can I add to this so that once the macro adds G2 to the other cell, it would then delete the contents of cell G2?

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim nRw As Long, rCell As Range
    Set rCell = Range("A1")
    
            'My CODE FOR G ________________________________________________
    If Target.Address = "$G$2" Then
                           
                If rCell.Value = "" Then
                    MsgBox "No Value Found", vbInformation, "Input Required"
                    Exit Sub
                End If
                
                On Error GoTo NoMatchFound:
                nRw = Range("A:A").Find(rCell.Value).Row
                
                With Cells(nRw, "G")
                    .Value = .Value + Range("G2").Value
                End With
            Exit Sub
    
                    'My CODE FOR H ________________________________________________
    ElseIf Target.Address = "$H$2" Then
                                           
                        If rCell.Value = "" Then
                            MsgBox "No Value Found", vbInformation, "Input Required"
                            Exit Sub
                        End If
                        
                        On Error GoTo NoMatchFound:
                        nRw = Range("A:A").Find(rCell.Value).Row
                        
                        With Cells(nRw, "H")
                            .Value = .Value + Range("H2").Value
                        End With
                        
                        Exit Sub
                        
                          'My CODE FOR I ________________________________________________
    ElseIf Target.Address = "$I$2" Then
                                           
                        If rCell.Value = "" Then
                            MsgBox "No Value Found", vbInformation, "Input Required"
                            Exit Sub
                        End If
                        
                        On Error GoTo NoMatchFound:
                        nRw = Range("A:A").Find(rCell.Value).Row
                        
                        With Cells(nRw, "I")
                            .Value = .Value + Range("I2").Value
                        End With
                        
                        Exit Sub
    NoMatchFound:
                            MsgBox "No Match Found", vbCritical, "Task Un-Successfull"
    End If
    
    
    End Sub
    Last edited by brown3218; 09-09-2013 at 11:23 PM.

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Delete Cell Contents when macro runs

    Maybe
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    
    Dim nRw As Long, rCell As Range
    Set rCell = Range("A1")
    
    Application.EnableEvents = False
    
            'My CODE FOR G ________________________________________________
    If Target.Address = "$G$2" Then
    
                Application.EnableEvents = False
                           
                If rCell.Value = "" Then
                    MsgBox "No Value Found", vbInformation, "Input Required"
                    Exit Sub
                End If
                
                On Error GoTo NoMatchFound:
                nRw = Range("A:A").Find(rCell.Value).Row
                
                With Cells(nRw, "G")
                    .Value = .Value + Range("G2").Value
                End With
                Range("G2").ClearContents
                Application.EnableEvents = True
            Exit Sub
    
                    'My CODE FOR H ________________________________________________
    ElseIf Target.Address = "$H$2" Then
                                           
                        If rCell.Value = "" Then
                            MsgBox "No Value Found", vbInformation, "Input Required"
                            Exit Sub
                        End If
                        
                        On Error GoTo NoMatchFound:
                        nRw = Range("A:A").Find(rCell.Value).Row
                        
                        With Cells(nRw, "H")
                            .Value = .Value + Range("H2").Value
                        End With
                        
                        Exit Sub
                        
                          'My CODE FOR I ________________________________________________
    ElseIf Target.Address = "$I$2" Then
                                           
                        If rCell.Value = "" Then
                            MsgBox "No Value Found", vbInformation, "Input Required"
                            Exit Sub
                        End If
                        
                        On Error GoTo NoMatchFound:
                        nRw = Range("A:A").Find(rCell.Value).Row
                        
                        With Cells(nRw, "I")
                            .Value = .Value + Range("I2").Value
                        End With
                        
                        Exit Sub
    NoMatchFound:
                            MsgBox "No Match Found", vbCritical, "Task Un-Successfull"
    End If
    
    
    End Sub
    Last edited by Solus Rankin; 09-05-2013 at 07:05 PM.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Delete Cell Contents when macro runs

    perhaps

    Application.EnableEvents = False
    
    With Cells(nRw, "G")
       .Value = .Value + Range("G2").Value
       Target.Delete
    End With
    
    Application.EnableEvents = True
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    06-17-2013
    Location
    Salt Lake City, UT
    MS-Off Ver
    Excel 2011
    Posts
    26

    Re: Delete Cell Contents when macro runs

    Not quite sure how to insert that into my code.

    Quote Originally Posted by Richard Buttrey View Post
    perhaps

    Application.EnableEvents = False
    
    With Cells(nRw, "G")
       .Value = .Value + Range("G2").Value
       Target.Delete
    End With
    
    Application.EnableEvents = True

  5. #5
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Delete Cell Contents when macro runs

    Brown,

    If you don't hear from Richard try the code in post #2. I amended and included your entire code.

  6. #6
    Registered User
    Join Date
    06-17-2013
    Location
    Salt Lake City, UT
    MS-Off Ver
    Excel 2011
    Posts
    26

    Re: Delete Cell Contents when macro runs

    Thanks Solus,

    I tried your code and it worked once and then it the whole macro stopped working. I didn't get any errors, it just stopped adding the cell value to the other.

  7. #7
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Delete Cell Contents when macro runs

    I may have disabled events too soon because of the exit sub. Try
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    
    Dim nRw As Long, rCell As Range
    Set rCell = Range("A1")
    
    Application.EnableEvents = False
    
            'My CODE FOR G ________________________________________________
    If Target.Address = "$G$2" 
                           
                If rCell.Value = "" Then
                    MsgBox "No Value Found", vbInformation, "Input Required"
                    Exit Sub
                End If
                
                On Error GoTo NoMatchFound:
                nRw = Range("A:A").Find(rCell.Value).Row
                
                With Cells(nRw, "G")
                application.enableevents = false
                    .Value = .Value + Range("G2").Value
                End With
                Range("G2").ClearContents
                Application.EnableEvents = True
            Exit Sub
    
                    'My CODE FOR H ________________________________________________
    ElseIf Target.Address = "$H$2" Then
                                           
                        If rCell.Value = "" Then
                            MsgBox "No Value Found", vbInformation, "Input Required"
                            Exit Sub
                        End If

  8. #8
    Registered User
    Join Date
    06-17-2013
    Location
    Salt Lake City, UT
    MS-Off Ver
    Excel 2011
    Posts
    26

    Re: Delete Cell Contents when macro runs

    Hmm, when I run this it gives me a syntax error and highlights If Target.Address = "$G$2"

    Quote Originally Posted by Solus Rankin View Post
    I may have disabled events too soon because of the exit sub. Try
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    
    Dim nRw As Long, rCell As Range
    Set rCell = Range("A1")
    
    Application.EnableEvents = False
    
            'My CODE FOR G ________________________________________________
    If Target.Address = "$G$2" 
                           
                If rCell.Value = "" Then
                    MsgBox "No Value Found", vbInformation, "Input Required"
                    Exit Sub
                End If
                
                On Error GoTo NoMatchFound:
                nRw = Range("A:A").Find(rCell.Value).Row
                
                With Cells(nRw, "G")
                application.enableevents = false
                    .Value = .Value + Range("G2").Value
                End With
                Range("G2").ClearContents
                Application.EnableEvents = True
            Exit Sub
    
                    'My CODE FOR H ________________________________________________
    ElseIf Target.Address = "$H$2" Then
                                           
                        If rCell.Value = "" Then
                            MsgBox "No Value Found", vbInformation, "Input Required"
                            Exit Sub
                        End If

  9. #9
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Delete Cell Contents when macro runs

    This changes things up a bit, but I believe its what you're trying to achieve
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rCell As Range
    Dim bCheck As Boolean
    
            'My CODE FOR G ________________________________________________
    If Not Intersect(Target, Range("G2")) Is Nothing Then
        bCheck = False
        If Not Range("A1").Value = "" Then
            For Each rCell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
                If rCell.Value = Range("A1").Value Then
                    With Range("G" & rCell.Row)
                        .Value = .Value + Range("G2").Value
                        bCheck = True
                        Application.EnableEvents = False
                        Range("G2").ClearContents
                        Application.EnableEvents = True
                    End With
                End If
            Next rCell
        ElseIf Range("A1").Value = "" Then
            MsgBox "No Value Found", vbInformation, "Input Required"
        End If
    End If
    
    
                    'My CODE FOR H ________________________________________________
    ElseIf Not Intersect(Target, Range("H2")) Is Nothing Then
        bCheck = False
        If Not Range("A1").Value = "" Then
            For Each rCell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
                If rCell.Value = Range("A1").Value Then
                    With Range("H" & rCell.Row)
                        .Value = .Value + Range("H2").Value
                        bCheck = True
                        Application.EnableEvents = False
                        Range("H2").ClearContents
                        Application.EnableEvents = True
                    End With
                End If
            Next rCell
        ElseIf Range("A1").Value = "" Then
            MsgBox "No Value Found", vbInformation, "Input Required"
        End If
                        
                          'My CODE FOR I ________________________________________________
    ElseIf Not Intersect(Target, Range("I2")) Is Nothing Then
        bCheck = False
        If Not Range("A1").Value = "" Then
            For Each rCell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
                If rCell.Value = Range("A1").Value Then
                    With Range("I" & rCell.Row)
                        .Value = .Value + Range("I2").Value
                        bCheck = True
                        Application.EnableEvents = False
                        Range("I2").ClearContents
                        Application.EnableEvents = True
                    End With
                End If
            Next rCell
        ElseIf Range("A1").Value = "" Then
            MsgBox "No Value Found", vbInformation, "Input Required"
        End If
    End If
    If bCheck = False Then
        MsgBox "No Match Found", vbCritical, "Task Un-Successfull"
    End If
    
    End Sub

  10. #10
    Registered User
    Join Date
    06-17-2013
    Location
    Salt Lake City, UT
    MS-Off Ver
    Excel 2011
    Posts
    26

    Re: Delete Cell Contents when macro runs

    Sorry for all of the back and forth. I now get an error, "Compile Error: Else Without If". It Highlights this line: ElseIf Not Intersect(Target, Range("H2")) Is Nothing Then

    Quote Originally Posted by Solus Rankin View Post
    This changes things up a bit, but I believe its what you're trying to achieve
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rCell As Range
    Dim bCheck As Boolean
    
            'My CODE FOR G ________________________________________________
    If Not Intersect(Target, Range("G2")) Is Nothing Then
        bCheck = False
        If Not Range("A1").Value = "" Then
            For Each rCell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
                If rCell.Value = Range("A1").Value Then
                    With Range("G" & rCell.Row)
                        .Value = .Value + Range("G2").Value
                        bCheck = True
                        Application.EnableEvents = False
                        Range("G2").ClearContents
                        Application.EnableEvents = True
                    End With
                End If
            Next rCell
        ElseIf Range("A1").Value = "" Then
            MsgBox "No Value Found", vbInformation, "Input Required"
        End If
    End If
    
    
                    'My CODE FOR H ________________________________________________
    ElseIf Not Intersect(Target, Range("H2")) Is Nothing Then
        bCheck = False
        If Not Range("A1").Value = "" Then
            For Each rCell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
                If rCell.Value = Range("A1").Value Then
                    With Range("H" & rCell.Row)
                        .Value = .Value + Range("H2").Value
                        bCheck = True
                        Application.EnableEvents = False
                        Range("H2").ClearContents
                        Application.EnableEvents = True
                    End With
                End If
            Next rCell
        ElseIf Range("A1").Value = "" Then
            MsgBox "No Value Found", vbInformation, "Input Required"
        End If
                        
                          'My CODE FOR I ________________________________________________
    ElseIf Not Intersect(Target, Range("I2")) Is Nothing Then
        bCheck = False
        If Not Range("A1").Value = "" Then
            For Each rCell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
                If rCell.Value = Range("A1").Value Then
                    With Range("I" & rCell.Row)
                        .Value = .Value + Range("I2").Value
                        bCheck = True
                        Application.EnableEvents = False
                        Range("I2").ClearContents
                        Application.EnableEvents = True
                    End With
                End If
            Next rCell
        ElseIf Range("A1").Value = "" Then
            MsgBox "No Value Found", vbInformation, "Input Required"
        End If
    End If
    If bCheck = False Then
        MsgBox "No Match Found", vbCritical, "Task Un-Successfull"
    End If
    
    End Sub

  11. #11
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Delete Cell Contents when macro runs

    I missed an 'end if'
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rCell As Range
    Dim bCheck As Boolean
    
            'My CODE FOR G ________________________________________________
    If Not Intersect(Target, Range("G2")) Is Nothing Then
        bCheck = False
        If Not Range("A1").Value = "" Then
            For Each rCell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
                If rCell.Value = Range("A1").Value Then
                    With Range("G" & rCell.Row)
                        .Value = .Value + Range("G2").Value
                        bCheck = True
                        Application.EnableEvents = False
                        Range("G2").ClearContents
                        Application.EnableEvents = True
                    End With
                End If
            Next rCell
        ElseIf Range("A1").Value = "" Then
            MsgBox "No Value Found", vbInformation, "Input Required"
        End If
    End If
    
    
                    'My CODE FOR H ________________________________________________
    ElseIf Not Intersect(Target, Range("H2")) Is Nothing Then
        bCheck = False
        If Not Range("A1").Value = "" Then
            For Each rCell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
                If rCell.Value = Range("A1").Value Then
                    With Range("H" & rCell.Row)
                        .Value = .Value + Range("H2").Value
                        bCheck = True
                        Application.EnableEvents = False
                        Range("H2").ClearContents
                        Application.EnableEvents = True
                    End With
                End If
            Next rCell
        ElseIf Range("A1").Value = "" Then
            MsgBox "No Value Found", vbInformation, "Input Required"
        End If
    End If
                        
                          'My CODE FOR I ________________________________________________
    ElseIf Not Intersect(Target, Range("I2")) Is Nothing Then
        bCheck = False
        If Not Range("A1").Value = "" Then
            For Each rCell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
                If rCell.Value = Range("A1").Value Then
                    With Range("I" & rCell.Row)
                        .Value = .Value + Range("I2").Value
                        bCheck = True
                        Application.EnableEvents = False
                        Range("I2").ClearContents
                        Application.EnableEvents = True
                    End With
                End If
            Next rCell
        ElseIf Range("A1").Value = "" Then
            MsgBox "No Value Found", vbInformation, "Input Required"
        End If
    End If
    If bCheck = False Then
        MsgBox "No Match Found", vbCritical, "Task Un-Successfull"
    End If
    
    End Sub

  12. #12
    Registered User
    Join Date
    06-17-2013
    Location
    Salt Lake City, UT
    MS-Off Ver
    Excel 2011
    Posts
    26

    Re: Delete Cell Contents when macro runs

    Thank you thank you!

  13. #13
    Registered User
    Join Date
    06-17-2013
    Location
    Salt Lake City, UT
    MS-Off Ver
    Excel 2011
    Posts
    26

    Re: Delete Cell Contents when macro runs

    Ended up using this:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rCell As Range
    Dim bCheck As Boolean
    
            'My CODE FOR G ________________________________________________
    If Not Intersect(Target, Range("G2")) Is Nothing Then
        bCheck = False
        If Not Range("A1").Value = "" Then
            For Each rCell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
                If rCell.Value = Range("A1").Value Then
                    With Range("G" & rCell.Row)
                        .Value = .Value + Range("G2").Value
                        bCheck = True
                        Application.EnableEvents = False
                        Range("G2").ClearContents
                        Application.EnableEvents = True
                    End With
                End If
            Next rCell
        ElseIf Range("A1").Value = "" Then
            MsgBox "No Value Found", vbInformation, "Input Required"
        End If
    End If
    
    
                    'My CODE FOR G ________________________________________________
    If Not Intersect(Target, Range("H2")) Is Nothing Then
        bCheck = False
        If Not Range("A1").Value = "" Then
            For Each rCell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
                If rCell.Value = Range("A1").Value Then
                    With Range("H" & rCell.Row)
                        .Value = .Value + Range("H2").Value
                        bCheck = True
                        Application.EnableEvents = False
                        Range("H2").ClearContents
                        Application.EnableEvents = True
                    End With
                End If
            Next rCell
        ElseIf Range("A1").Value = "" Then
            MsgBox "No Value Found", vbInformation, "Input Required"
        End If
    End If
    
                        'My CODE FOR I ________________________________________________
    If Not Intersect(Target, Range("I2")) Is Nothing Then
        bCheck = False
        If Not Range("A1").Value = "" Then
            For Each rCell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
                If rCell.Value = Range("A1").Value Then
                    With Range("I" & rCell.Row)
                        .Value = .Value + Range("I2").Value
                        bCheck = True
                        Application.EnableEvents = False
                        Range("I2").ClearContents
                        Application.EnableEvents = True
                    End With
                End If
            Next rCell
        ElseIf Range("A1").Value = "" Then
            MsgBox "No Value Found", vbInformation, "Input Required"
        End If
    End If
    
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Delete Cell Value after Macro Runs
    By brown3218 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-20-2013, 11:42 AM
  2. [SOLVED] Macro to delete certain contents of a cell
    By SpyderPB6 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-11-2012, 07:16 PM
  3. Macro to delete entire row based on cell contents
    By ATX in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 10-06-2011, 03:52 AM
  4. Macro to delete cell contents in every other column
    By rhudgins in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-01-2010, 12:29 PM
  5. [SOLVED] VBA Excel Macro to delete contents in named cell
    By reaa in forum Excel General
    Replies: 1
    Last Post: 01-03-2006, 04:20 PM

Tags for this Thread

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