excel vba msgbox

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Excel VBA - MsgBox Cancel

Excel VBA - MsgBox Cancel

resolvedResolved · High Priority · Version 2003

Ziggy has attended:
Excel VBA Intro Intermediate course
Excel VBA Advanced course

Excel VBA - MsgBox Cancel


Hello

I am trying to set up a message box warning based on a selection stating that the worksheet already exists, and that continuing this action will over-write the existing sheet.

On Cancel I want to exit the sub and the sub which called it.

Below is the code at present, any help would be great.

Kind regards
Ziggy


Sub CreateSheet()

strName = "Data"

Call DeleteSheet

Sheets.Add after:=Sheets(Sheets.Count)

ActiveSheet.Name = strName

End Sub

Sub DeleteSheet()

Dim shtSheet As Worksheet

Application.DisplayAlerts = False

For Each shtSheet In ActiveWorkbook.Worksheets

If shtSheet.Name = strName Then

MsgBox strName & " already exists" & vbCrLf & vbCrLf & "This action will overwrite the existing tab", vbOKCancel

If vbOK Then shtSheet.Delete

If vbCancel Then Exit Sub

Exit For

End If

Next shtSheet

End Sub

RE: Excel VBA - MsgBox Cancel

Hi Ziggy, thanks for your query. Your problem is that you are attempt to do the worksheet management in the DeleteSheet routine. Once you forcibly exit out of that procedure you're sent straight back to CreateSheet and Sheets.Add after:=Sheets(Sheets.Count), ActiveSheet.Name = strName. I've tweaked the code and got it to do what I think you need, but let me know if you need any further help.

Here's the code:
_____________

Dim strname As String

Sub CreateSheet()

strname = "Data"

Call DeleteSheet

End Sub

Sub DeleteSheet()

Dim shtSheet As Worksheet
Dim iReply As Integer
Application.DisplayAlerts = False

For Each shtSheet In ActiveWorkbook.Worksheets

If shtSheet.Name = strname Then

iReply = MsgBox(Prompt:=strname & " already exists" & vbCrLf & vbCrLf & "This action will overwrite the existing tab", _
Buttons:=vbOKCancel, Title:="Choose")

If iReply = vbOK Then

MsgBox "OK pressed. Deleting Sheet"
shtSheet.Delete
Sheets.Add after:=Sheets(Sheets.Count)
ActiveSheet.Name = strname
Exit Sub

Else

MsgBox ("Exiting")
Exit Sub

End If

End If


Next shtSheet

End Sub
________________________


Hope this helps,

Anthony

RE: Excel VBA - MsgBox Cancel


Hello Anthony

Sorry for the delay in getting back to you, and thank you that works great.

Similar to something I had found online, but didn't know one could place a message box on the right hand side of an equation - very tidy!

Just a quick one, and it is more with regards convention is the "i" in "iReply" same as using "int"? If so are they interchangable, and which is prefered?

Many thanks

Ziggy

RE: Excel VBA - MsgBox Cancel

Hi Ziggy, yes the "i" entirely interchangeable with "int" as a prefix to variables. As for preference, that depends a lot on the development time and/or who you're going to hand over the code to. Actually, I'd go for "int" - it just indicates the type of data held by the variable a bit more clearly!

Hope this helps,

Anthony

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Highlighting only Text cells

To select onlt text value cells in a spreadsheet, click on Edit-Go to(F5)


In the Go To dialog box, click Special.
In the Go To Special dialog box, select Constants.
Click OK.

View all Excel hints and tips


Server loaded in 0.07 secs.