Add message box before committing Macro

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Greetings, I have a macro which works great in deleting contents. I thought I should have a message box asking the user are they sure before they commit to a major task. The message will read " This action will clear all PERSONNEL data from this X-Man". The clear macro I am currently using is:

VBA Code:
Sub XMan()
With ActiveSheet
 With Range("A3:AB3", Range("A" & Rows.Count).End(xlUp)).ClearContents
End With
End With
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
You could try this

VBA Code:
If answer = vbYes Then
  MsgBox "This action will clear all PERSONNEL data from this X-Man"
Else
  MsgBox "No"
End If
 
Upvote 0
You could try this

VBA Code:
If answer = vbYes Then
  MsgBox "This action will clear all PERSONNEL data from this X-Man"
Else
  MsgBox "No"
End If


I did try it. It did clear it out, but then the "NO" message came up. Perhaps I should have a Yes which will clear it and a No will just cancel the request out. Thank you ever so much!



VBA Code:
Sub Clear_XMan()
With ActiveSheet
With Range("A3:AB3", Range("A" & Rows.Count).End(xlUp)).ClearContents
If answer = vbYes Then
  MsgBox "This action will clear all PERSONNEL data from this X-Man"
Else
  MsgBox "No"
End If
End With
End With
End Sub
 
Upvote 0
Thank for getting me in the right direction. The macro with some help we ended up with is: And it seems to work.

VBA Code:
Sub XMan()
Dim Answer As VbMsgBoxResult

Answer = MsgBox("This action will clear all PERSONNEL data from this X-Man" & vbNewLine & "Are you sure you want to do it?", vbYesNo, "WARNING")

If Answer = vbNo Then

        Exit Sub
Else

        With ActiveSheet
            .Range("A3:AB3", Range("A" & Rows.Count).End(xlUp)).ClearContents
        End With
End If
End Sub
 
Upvote 0
Solution
Try...

VBA Code:
Sub XMan()

    Dim ans As VbMsgBoxResult
    ans = MsgBox("This action will clear all PERSONNEL data from" & vbCrLf & "this X-Man, continue?", vbQuestion + vbYesNo, "Continue?")

    If ans = vbNo Then Exit Sub
    
    With ActiveSheet
       .Range("A3:AB3", .Range("A" & .Rows.Count).End(xlUp)).ClearContents
    End With
    
End Sub

By the way, notice that the references within the With/End With statement are preceded with a dot ( . ) so that refer to ActiveSheet.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,030
Messages
6,122,762
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top