+ Reply to Thread
Results 1 to 5 of 5

adding "Yes/No" message box to delete row macro

Hybrid View

Dannypak adding "Yes/No" message box... 05-07-2009, 01:19 PM
Leith Ross Re: adding "Yes/No" message... 05-07-2009, 01:24 PM
Paul Re: adding "Yes/No" message... 05-07-2009, 01:25 PM
royUK Re: adding "Yes/No" message... 05-07-2009, 01:25 PM
Dannypak Re: adding "Yes/No" message... 05-07-2009, 02:02 PM
  1. #1
    Registered User
    Join Date
    03-11-2009
    Location
    Seattle, WA
    MS-Off Ver
    Office 2007
    Posts
    58

    adding "Yes/No" message box to delete row macro

    Hello,

    I'm using the following code to do conditional delete row, but would like to add a message box to confirm the "delete" with a Yes/No msgbox. I'd like to have the macro exit when the user clicks "no" but I'm missing something here.
    Thank you in advance for your help

    Sub DeleteRowsVendorUS()
        Const sTOFIND As String = "void"
         varanswer = MsgBox("Are you sure you want to delete these rows?", vbYesNo, "alert")
        Dim rngToCheck As Range, rngCell As Range, rngToDelete As Range
        
        Application.ScreenUpdating = False
        
        With Sheet7
            Set rngToCheck = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
        End With
        
        For Each rngCell In rngToCheck
            If rngCell.Value = sTOFIND Then
                If rngToDelete Is Nothing Then
                    Set rngToDelete = rngCell
                Else
                    Set rngToDelete = Union(rngToDelete, rngCell)
                End If
            End If
        Next rngCell
            
        If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
        
        Application.ScreenUpdating = True
    End Sub
    Last edited by Dannypak; 05-07-2009 at 02:02 PM. Reason: solved

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: adding "Yes/No" message box to delete row macro

    Hello Dannypak,

    You just need to test the result with an If ... Then statement.
    Sub DeleteRowsVendorUS()
       Const sTOFIND As String = "void"
       Dim rngToCheck As Range, rngCell As Range, rngToDelete As Range
    
         varanswer = MsgBox("Are you sure you want to delete these rows?", vbYesNo, "alert")
           If varanswer = vbNo Then Exit Sub
        
         Application.ScreenUpdating = False
        
        With Sheet7
            Set rngToCheck = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
        End With
        
        For Each rngCell In rngToCheck
            If rngCell.Value = sTOFIND Then
                If rngToDelete Is Nothing Then
                    Set rngToDelete = rngCell
                Else
                    Set rngToDelete = Union(rngToDelete, rngCell)
                End If
            End If
        Next rngCell
            
        If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
        
        Application.ScreenUpdating = True
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: adding "Yes/No" message box to delete row macro

    Hi Dannypak,

    Something along these lines should work...
    Dim varAnswer
    varAnswer = MsgBox("Are you sure you want to delete these rows?", vbYesNo, "alert")
    If varAnswer = vbYes Then
       ' Put the rest of the code you want to execute here
    Else
       Exit Sub
    End If

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: adding "Yes/No" message box to delete row macro

    Try this
    Option Explicit
    
    Sub DeleteRowsVendorUS()
        Const sTOFIND As String = "void"
        Dim rngToCheck As Range, rngCell As Range, rngToDelete As Range
        
        Application.ScreenUpdating = False
        
        With Sheet7
            Set rngToCheck = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
        End With
        
        For Each rngCell In rngToCheck
            If rngCell.Value = sTOFIND Then
                If rngToDelete Is Nothing Then
                    Set rngToDelete = rngCell
                Else
                    Set rngToDelete = Union(rngToDelete, rngCell)
                End If
            End If
        Next rngCell
       Select Case MsgBox("Do you want to permanently remove this data?", vbYesNo Or vbQuestion Or vbDefaultButton1, "Continue")
       
        Case vbYes
           If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
        Case vbNo
       End Select
        Application.ScreenUpdating = True
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    03-11-2009
    Location
    Seattle, WA
    MS-Off Ver
    Office 2007
    Posts
    58

    Re: adding "Yes/No" message box to delete row macro

    Thank you all! So simpple yet so...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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