Consulting

Results 1 to 11 of 11

Thread: Message Box using vbYesNo

  1. #1
    VBAX Regular
    Joined
    Jun 2013
    Posts
    40
    Location

    Message Box using vbYesNo

    I am having difficulty placing a message box with the option of yes or no to execute a sub. If the user selects yes the sub executes and if the user selects no the sub is aborted with a mess of activex controls being clear and hidden.

    Is it better to use input boxes and if so, how do I use the activex controls?

    Private Sub CommandButton3_Click()
    Dim nullstring
    If Me.ComboBox1.Value = "" Or nullstring Then
            MsgBox "Selection Required"
            Exit Sub
        End If
        
    If Len(Me.TextBox1.Text) < 3 Then
            MsgBox "The Minimum of Three Alpha Characters Required"
            TextBox1.Value = ""
            Exit Sub
        End If
    MsgBox "THIS CHANGE IS PERMANENT!", vbYesNo
        Dim WS              As Worksheet
        Set WS = Worksheets("combined")
        Dim Search          As String
        Dim Replacement     As String
        Dim Prompt          As String
        Dim Title           As String
        Dim MatchCase       As Boolean
      
        Search = ComboBox1.Value
        
        Replacement = TextBox1.Value
         
        For Each WS In Worksheets
            WS.Cells.Replace What:=Search, Replacement:=Replacement, _
            LookAt:=xlPart, MatchCase:=False
        Next
    
        Me.ComboBox1.Value = ""
        Me.ComboBox1.Visible = False
        TextBox1.Value = ""
        TextBox1.Visible = False
        TextBox2.Value = ""
        CommandButton3.Visible = False
        Range("h18:i18").ClearContents
        MsgBox "Change Completed"
    End Sub

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Private Sub combobox1_change()
       commandbutton3.visible=(combobox1.listindex>-1)*(len(TextBox1.Text)=3)
    End Sub
    
    Private Sub TextBox1_change()
       commandbutton3.visible=(combobox1.listindex>-1)*(len(TextBox1.Text)=3)
    End Sub

  3. #3
    VBAX Regular
    Joined
    Jun 2013
    Posts
    40
    Location
    Please forgive my ignorance but how does this address the yes/no message box issue?


    Quote Originally Posted by snb View Post
    Private Sub combobox1_change()
       commandbutton3.visible=(combobox1.listindex>-1)*(len(TextBox1.Text)=3)
    End Sub
    
    Private Sub TextBox1_change()
       commandbutton3.visible=(combobox1.listindex>-1)*(len(TextBox1.Text)=3)
    End Sub

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    It makes the message box unnecessary/redundant.
    It also simplifies your code.

    You should inform the user what is being expected beforehand, not afterwards.
    As long as commandbutton3 isn't visible the user stick in the same screen.
    If she wants to continue she has to fill the textbox and the combobox correctly first.

  5. #5
    VBAX Regular
    Joined
    Jun 2013
    Posts
    40
    Location
    Well stated; however you are sexist. Not all my users are females.

    Thank you snb!

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    My approach is gender independent. Please test it and report the results,

  7. #7
    VBAX Regular
    Joined
    Jun 2013
    Posts
    40
    Location
    Quote Originally Posted by snb View Post
    My approach is gender independent. Please test it and report the results,
    "Gender Independent", that's a first for me. I will apply and test when I have less distractions (customers).

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Did not really follow the discussion, but if you were asking about

    MsgBox "THIS CHANGE IS PERMANENT!", vbYesNo


    then I think a better way to get a response is to use the function form of Msgbox and do something like ...

    Option Explicit
    Sub demo()
        Const sModule As String = "Demo of YesNo"
        
        If MsgBox("THIS CHANGE IS PERMANENT!", vbQuestion + vbYesNo + vbDefaultButton2, sModule) = vbYes Then
            Call MsgBox("OK, if you say so", vbExclamation + vbOKOnly, sModule)
        Else
            Call MsgBox("OK, then I won't", vbInformation + vbOKOnly, sModule)
        End If
    End Sub
    Paul

  9. #9
    VBAX Regular
    Joined
    Jun 2013
    Posts
    40
    Location
    I receive an error stating the command button (obeObject) cannot be found. The change does execute!

    Quote Originally Posted by snb View Post
    Private Sub combobox1_change()
       commandbutton3.visible=(combobox1.listindex>-1)*(len(TextBox1.Text)=3)
    End Sub
    
    Private Sub TextBox1_change()
       commandbutton3.visible=(combobox1.listindex>-1)*(len(TextBox1.Text)=3)
    End Sub

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Please post a workbook that only contains 1 textbox1, 1 combobox1 and 1 commandbutton3.
    Like the attachment.
    Attached Files Attached Files

  11. #11
    VBAX Regular
    Joined
    Jun 2013
    Posts
    40
    Location
    Thank you. I will give it a go.

Posting Permissions

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