Consulting

Results 1 to 5 of 5

Thread: Solved: Alert before delete a record

  1. #1

    Solved: Alert before delete a record

    I have a macro which deletes the selected record But it doesnot alert any message before delete I want that the when the code will be run, a alert msg should pop up asking "Do you really want to delete the record?" if the user clicks "Yes" then the record should be deleted and if the user clicks "No" , no deletion should take place and the msg box should go off the screen.
    Sub DeleteRecord()
        Dim intActiveRow As Integer
     
        Application.ScreenUpdating = False
     
        intActiveRow = ActiveCell.Row
     
        ActiveSheet.Unprotect password:="abcd"
        With Range("A" & intActiveRow)
            .EntireRow.Delete
        End With
        ActiveSheet.Protect password:="abcd"
     
        Application.ScreenUpdating = True
    End Sub

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    See if this works for you:
    [VBA]Option Explicit
    Public Sub CustomMessageBoxes()
    Dim iResponse As Integer
    Dim intActiveRow As Integer
    iResponse = MsgBox(Prompt:="Click Yes or No.", _
    Buttons:=vbYesNo + vbCritical)
    Select Case iResponse
    Case vbYes
    Application.ScreenUpdating = False
    intActiveRow = ActiveCell.Row
    ActiveSheet.Unprotect Password:="abcd"
    With Range("A" & intActiveRow)
    .EntireRow.Delete
    End With
    ActiveSheet.Protect Password:="abcd"
    Application.ScreenUpdating = True
    Case vbNo
    ' MsgBox Prompt:="You clicked No."
    Exit Sub
    End Select
    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

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    or you can call it like this and call the macro you already have:
    [VBA]Public Sub CustomMessageBoxes()
    Dim iResponse As Integer
    Dim intActiveRow As Integer
    iResponse = MsgBox(Prompt:="Click Yes or No.", _
    Buttons:=vbYesNo + vbCritical)
    Select Case iResponse
    Case vbYes
    DeleteRecord
    Case vbNo
    ' MsgBox Prompt:="You clicked No."
    Exit Sub
    End Select
    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

  4. #4
    VBAX Mentor anandbohra's Avatar
    Joined
    May 2007
    Location
    Mumbai
    Posts
    313
    Location
    try this simplest way

    [VBA]Sub DeleteRecord()
    Dim a
    Dim intActiveRow As Integer
    Application.ScreenUpdating = False
    intActiveRow = ActiveCell.Row
    ActiveSheet.Unprotect Password:="abcd"
    a = MsgBox("do you really want to delete record", vbYesNo + vbCritical, "Your Name")
    If a = vbYes Then
    With Range("A" & intActiveRow)
    .EntireRow.Delete
    End With
    End If

    ActiveSheet.Protect Password:="abcd"

    Application.ScreenUpdating = True
    End Sub[/VBA]


  5. #5
    Thank you lucas, thanks anand.
    Codes are perfect.

Posting Permissions

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