+ Reply to Thread
Results 1 to 5 of 5

Conditional MsgBox

Hybrid View

  1. #1
    Registered User
    Join Date
    09-06-2012
    Location
    Alberton, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    30

    Conditional MsgBox

    I have created a UserForm with 5 ComboBoxes and 1 TextBox. For the ComboBoxes, the Enabled property is set to False and the Locked property is set to True. On initializing the UserForm, these properties on some of the ComboBoxes are changed based on the value of a cells in the worksheet. The user can now change the values in only the unlocked ComboBoxes and the TextBox.

    I want to display the new values of only the ComboBoxes that are unlocked and the TextBox, so that the user can confirm their entries.

    The message displayed should look like this:

    The new values are:
    ComboBox1 - Value1
    ComboBox2 - Value2
    TextBox - Value3

    I can write the MsgBox statement so that the message is displayed as above, but all ComboBoxes are listed.

    I would like to now if it is possible to display only the qualifying ComboBoxes in a MsgBox, and what the syntax MsgBox statement should be.

    Any help would be appreciated.

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Conditional MsgBox

    Hi,

    you can loop the comboboxes and construct a string of values first, then displaying the msgbox, e.g.:
    Private Sub CommandButton1_Click()
        
        Dim ctrl As Control
        Dim strMsg As String
        
        For Each ctrl In Me.Controls
            If TypeName(ctrl) = "ComboBox" Then
                If ctrl.Enabled Then
                    If strMsg = vbNullString Then
                        strMsg = ctrl.Name & ": " & ctrl.Value
                    Else
                        strMsg = strMsg & vbNewLine & ctrl.Name & ": " & ctrl.Value
                    End If
                End If
            End If
        Next ctrl
        Select Case MsgBox("Check the values: " & vbNewLine & strMsg & vbNewLine & "Are these fine?", vbQuestion + vbYesNo, "Confirm")
            Case vbNo
                'do sth
            Case vbYes
                'do sth else
        End Select
    End Sub
    This is looping all controls on a UF, if its a combobox and its enabled the content and name is stored in a string
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  3. #3
    Registered User
    Join Date
    09-06-2012
    Location
    Alberton, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Conditional MsgBox

    Thank you tehneXus

    This is exactly what I needed.

    However, the name of the ComboBox (e.g. ExpCat1ComboBox) is not very user friendly. Is there a way to change the Name or Caption of the ComboBox, and displaying the new Name or Caption. I have done this for the Label associated with the ComboBox, but do not know how to do it for the ComboBox.

    Thank you.

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

    Re: Conditional MsgBox

    Simplest thing would be to use the comboboxes Tag property to store desired output text
    Cheers
    Andy
    www.andypope.info

  5. #5
    Registered User
    Join Date
    09-06-2012
    Location
    Alberton, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Conditional MsgBox

    Thank you Andy and tehneXus

    I have changed tehneXus' code to use the .Tag property and not the .Name property (see the changed code below):

    strMsg = "You are about to change the Expense Category Codes and/or the Reference Number." & vbCr & vbCr _
    & "The New values are:" & vbCr
    
    For Each ctrl In Me.Controls
      
      If TypeName(ctrl) = "ComboBox" Then
        If ctrl.Enabled Then
            strMsg = strMsg & vbCr & ctrl.Tag & " :" & Space(40 - Len(ctrl.Tag)) & ctrl.Value
        End If
      End If
    Next ctrl
    
    Confirm = MsgBox(strMsg & vbCr _
    & RefNoLabel & " :" & Space(40 - Len(RefNoLabel)) & RefNoTextBox.Value & vbCr & vbCr _
    & "Is this correct?", vbYesNo, "Confirm Change")
    I am setting the Tag property when the form is Initialized, i.e.

    Me.ExpCat1ComboBox.Tag = Sheet3.Range("B10").Value
    Thank you for your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Msgbox
    By Jazzy Max in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-08-2009, 01:03 AM
  2. MsgBox-center the message on a msgbox
    By CobraLAD in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-11-2007, 06:48 AM
  3. MsgBox-at the end a msgBox popup
    By CC in forum Excel General
    Replies: 4
    Last Post: 05-05-2006, 12:50 PM
  4. MsgBox Help
    By Zach H in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-24-2006, 11:16 AM
  5. [SOLVED] Msgbox
    By Wildman in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-26-2005, 12:06 AM

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