+ Reply to Thread
Results 1 to 4 of 4

Activate vbCancel in message box

Hybrid View

datafiend Activate vbCancel in message... 09-25-2012, 02:31 AM
Andy Pope Re: Activate vbCancel in... 09-25-2012, 04:05 AM
JosephP Re: Activate vbCancel in... 09-25-2012, 04:15 AM
datafiend Re: Activate vbCancel in... 09-25-2012, 04:41 AM
  1. #1
    Registered User
    Join Date
    09-10-2009
    Location
    Perth, Western Australia
    MS-Off Ver
    Excel 2013
    Posts
    47

    Activate vbCancel in message box

    Hi there
    I'm using a message box to unhide some sheets in my workbook and want to be able to cancel the command when I have unhidden the ones I want. I'm using the code below and though it seemed to work a few times, it won't do so now. Can anyone tell me how I can correct it?

    Thanks in advance for any help.

    datafiend

    Sub UnhideSomeSheets()
        Dim sSheetName As String
        Dim sMessage As String
        Dim Msgres As VbMsgBoxResult
        Dim wsSheet As Worksheet
    
        For Each wsSheet In ActiveWorkbook.Worksheets
            If wsSheet.Visible = xlSheetHidden Then
                sSheetName = wsSheet.Name
                sMessage = "Unhide the following sheet?" _
                  & vbNewLine & sSheetName
                Msgres = MsgBox(sMessage, vbYesNoCancel)
                If Msgres = vbYes Then wsSheet.Visible = xlSheetVisible
               ElseIf Msgres = vbCancel Then Exit Sub
          
            End If
     
        Next wsSheet
    End Sub
    Last edited by datafiend; 09-25-2012 at 04:46 AM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Activate vbCancel in message box

                Msgres = MsgBox(sMessage, vbYesNoCancel Or vbDefaultButton3)
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Activate vbCancel in message box

    your If statements are out of line
    Sub UnhideSomeSheets()
        Dim sSheetName As String
        Dim sMessage As String
        Dim Msgres As VbMsgBoxResult
        Dim wsSheet As Worksheet
    
        For Each wsSheet In ActiveWorkbook.Worksheets
            If wsSheet.Visible = xlSheetHidden Then
                sSheetName = wsSheet.Name
                sMessage = "Unhide the following sheet?" _
                  & vbNewLine & sSheetName
                Msgres = MsgBox(sMessage, vbYesNoCancel)
                If Msgres = vbYes Then
                    wsSheet.Visible = xlSheetVisible
                ElseIf Msgres = vbCancel Then
                    Exit Sub
                End If
          
            End If
     
        Next wsSheet
    End Sub
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  4. #4
    Registered User
    Join Date
    09-10-2009
    Location
    Perth, Western Australia
    MS-Off Ver
    Excel 2013
    Posts
    47

    Re: Activate vbCancel in message box

    Hi Joseph
    Thanks very much for that. Works perfectly. I hadn't realised one of the If statements hadn't been ended.

    datafiend

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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