Help please, VBA userform, MsgBox() ?

alfa_eng

New Member
Joined
Apr 17, 2015
Messages
31
Hello.

I have a userform that I use to introduce some data on Excel.


Most of the data must be filled (that are textbox that the verifications dont need the information to do the calculations), because if they are not filled the rest of the
Worksheet
cant do the verifications that I need.

When I click on the button "insert data",the textboxs that are mark with the red spot must be filled, if one of those textboxs aren't filled a msg box appears and says "Some data are missing, so the data can't be introduce on the worksheet".

So, I need to program the button insert, to only insert if the labels with the red spot are filled, and if they aren't filled to show me a msgbox.

Please, please, can anyone help me?

Thanks!

 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
One way that I use;

For each mandatory input control, use the Tag property to indicate if it is mandatory or not.

You can find the Tag property in the Properties window when you select the control.

Then use your command button click event to evaluate the Tag of each control, e.g.:

Code:
    For Each ctl in Me.Controls
        If ctl.Tag = "True" Then
            If Len(ctl.Value) = 0 Then 'this depends on the type of control actually
               blnContinue = False
               strError = "Make sure all mandatory fields are filled in!"
               Exit For
            End If
        End If
    Next ctl

This is just a point in the right direction. I don't know what your controls actually are.
 
Upvote 0
I'm sorry, I dont have much pratice at programming. So I'm a little confused.

When you say to use the Tag property to indicate if it is mandatory or not, I have to select the textbox that I want to be filled and in the property tag write "True"?

And when you say "evaluate the Tag of each control" what to you mean with control? Command button? TextBox?

Thanks for your help.
 
Upvote 0
When you say to use the Tag property to indicate if it is mandatory or not, I have to select the textbox that I want to be filled and in the property tag write "True"?
Yes

And when you say "evaluate the Tag of each control" what to you mean with control? Command button? TextBox?
I am suggesting that you use your {assume} command button that is being used for form submission. E.g. the Command click event.

So the exhibit aims to illustrate how you then loop through each control on your user form.

This is a more complete exhibit. Assuming I submit the form using CommandButton1
Code:
Private Sub CommandButton1_Click()
    Dim ctl         As MSForms.Control
    Dim blnContinue As Boolean
    Dim strError    As String
    
    For Each ctl In Me.Controls
        If ctl.Tag = "True" Then
            blnContinue = CBool(Len(ctl.Value))
            If blnContinue = False Then 'it is empty
                strError = "Please complete all mandatory fields!"
                Exit For
            End If
        End If
    Next ctl
    
    If blnContinue Then 'it passed mandatory checks
        'do what you wanna do
    Else 'it failed
        Call MsgBox(Prompt:=strError, Buttons:=vbOKOnly + vbExclamation, Title:="Validation failed")
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,208
Members
448,554
Latest member
Gleisner2

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