Verify if 2 textbox are empty when a user select a combobox (VBA)

DECOVIOTI

New Member
Joined
Dec 11, 2020
Messages
22
Office Version
  1. 2019
Platform
  1. Windows
Capture.PNG

I am creating a vba form which I have 2 textbox and 1 combobox as attached. I would like to create the following condition. When the user try to change the combobox value without having put the amount in the 2 textbox first I would like to display a message e.g. "The textboxes need to have values", not allowing the user to continue. Could you please help me?
VBA Code:
Sub TextBoxnull()
    
        If Len(TextBoxnull.txtTOPDtotalrem.Value) = 0 And Len(TextBoxnull.txtTOPDmedical.Value) > 0 Then
            MsgBox "Number must be entered to continue!"
         End If
        TextBoxnull.cboTOPDsuperannuationperc.Value = Format(TextBoxnull.cboTOPDsuperannuationperc.Value, "0%")
        Sheet1.Range("B10").Value = Format(TextBoxnull.cboTOPDsuperannuationperc.Value, "0%")
        With TextBoxnull
          .txtTOPtaxableremexclsuperannuation.Value = Format(Sheet1.Range("B13").Value, "$#,##0.00;-$#,##0.00")
          .txtTOPsuperannuationcontribution.Value = Format(Sheet1.Range("B14").Value, "$#,##0.00;-$#,##0.00")
          .txtTOPDtotalremnet.Value = Format(Sheet1.Range("B15").Value, "$#,##0.00;-$#,##0.00")
        End With
        
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
NB test on a copy of your work, first.
VBA Code:
Private Sub ComboBox1_DropButtonClick()
    With Me
        If .TextBox1.Value = "" Then MsgBox "Both TextBox1 AND 2 must contain values, before proceeding!": .TextBox1.SetFocus: Exit Sub
        If .TextBox2.Value = "" Then MsgBox "Both TextBox1 AND 2 must contain values, before proceeding!": .TextBox2.SetFocus: Exit Sub
    End With
End Sub
...change control names, to suite...
 
Upvote 0
Hi,
another way maybe

VBA Code:
Private Sub ComboBox1_Enter()
    Dim i As Integer
    For i = 1 To 2
    With Me.Controls("TextBox" & i)
            If Len(.Value) = 0 Then
                MsgBox .Name & " must contain a value before you can continue.", 48, "Entry Required"
                .SetFocus: Exit Sub
            End If
        End With
    Next i
End Sub

Dave
 
Upvote 0
Solution
Hi,
another way maybe

VBA Code:
Private Sub ComboBox1_Enter()
    Dim i As Integer
    For i = 1 To 2
    With Me.Controls("TextBox" & i)
            If Len(.Value) = 0 Then
                MsgBox .Name & " must contain a value before you can continue.", 48, "Entry Required"
                .SetFocus: Exit Sub
            End If
        End With
    Next i
End Sub

Dave
Thanks @Dave..your suggestions worked for me...Cheers
 
Upvote 0
Glad our suggestions were able to help you - we appreciate the feedback


Dave
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,581
Members
449,089
Latest member
Motoracer88

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