Exception to the obligation to fill in all fields of a UserForm

Aretradeser

Board Regular
Joined
Jan 16, 2013
Messages
176
Office Version
  1. 2013
Platform
  1. Windows
Exception to the obligation to fill in all fields of a UserForm
Through a UserForm I have a form with which I register a series of data. In principle, this code requires all the fields to be filled in, if they are not filled in then the empty ComboBox and Textbox are coloured in red and you get a warning screen, which warns you: "DATA NOT FILLED IN", preventing you from registering the data, as all the fields are not filled in.
The problem is that I don't know how to modify this code to make an exception with one of the ComboBoxes, specifically with the ComboBox2. I want that, although this ComboBox2 is not filled in, the rest of the data can be registered.

CODE
Rich (BB code):
Private Sub cmdbRegistrar_Click()
    Dim Salir       As Boolean
    Dim fe1         As Date
    Dim fe2         As Date
    Dim NotComplete As Boolean
    Dim i           As Integer, a As Integer
    For i = 1 To 7
        For a = 1 To 2
            With Me.Controls(Choose(a, "ComboBox", "TextBox") & i)
                If .Enabled Then .BackColor = IIf(Len(.Text) > 0, vbWindowBackground, vbRed)
                If .BackColor = vbRed Then NotComplete = True
            End With
        Next a
    Next i
    If NotComplete Then
        MsgBox "DATA NOT FILLED IN !!!", vbExclamation, "Entry Required"
        Exit Sub
    End If
    If IsDate(TextBox1) Then fe1 = CDate(TextBox1)
    fe1 = CDate(TextBox1)
    TextBox1 = Format(fe1, "mm/dd/yyyy")
    If Not IsDate(TextBox2) Then
        MsgBox "ENTER A VALID DATE", vbExclamation
        TextBox2.SetFocus
        Exit Sub
    End If
    fe2 = CDate(TextBox2)
    TextBox2 = Format(fe2, "mm/dd/yyyy")
    Sheets("BDATOS").Select
    Sheets("BDATOS").Unprotect ("123")
    With Worksheets("BDATOS")
        t = Cells(Rows.Count, 2).End(xlUp).Row
        If Me.ComboBox1 = "ESPAÑA" Or Me.ComboBox1 = "PORTUGAL" Then
            Cells(t + 1, 2) = TextBox6 & " " & Trim(TextBox7) & ", " & TextBox5
        Else
            Cells(t + 1, 2) = Trim(TextBox6) & ", " & TextBox5
        End If
        Cells(t + 1, 3) = ComboBox5
        Cells(t + 1, 4) = ComboBox6
        Cells(t + 1, 5) = ComboBox1
        Cells(t + 1, 6) = ComboBox2
        Cells(t + 1, 7) = ComboBox3
        Cells(t + 1, 8) = ComboBox4
        Cells(t + 1, 9) = TextBox1.Value
        Cells(t + 1, 10) = TextBox2.Value
        Cells(t + 1, 11) = 0 + TextBox3
        Cells(t + 1, 12) = ComboBox7
        Cells(t + 1, 13) = TextBox4.Value
        Cells(t + 1, 14) = Right(TextBox1.Value, 4)
    End With
    Cells(t, 13).Select
    Selection.AutoFill Destination:=Range("M" & t & ":M" & t + 1), Type:=xlFillDefault
    Sheets("BDATOS").Protect ("123")
    For n = 1 To 12
        On Error Resume Next
        Controls("textbox" & n) = ""
        Controls("combobox" & n) = ""
    Next
    Sheets("DATOS").Select
    Cells.Select
    Cells.EntireColumn.AutoFit
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi,
try the update to code below

Rich (BB code):
For i = 1 To 7
        For a = 1 To 2
            With Me.Controls(Choose(a, "ComboBox", "TextBox") & i)
                If .Enabled Then .BackColor = IIf(Len(.Text) > 0 Or .Name = "ComboBox2", vbWindowBackground, vbRed)
                If .BackColor = vbRed Then NotComplete = True
            End With
        Next a
    Next i

Dave
 
Upvote 0
Solution
Hi,
try the update to code below

Rich (BB code):
For i = 1 To 7
        For a = 1 To 2
            With Me.Controls(Choose(a, "ComboBox", "TextBox") & i)
                If .Enabled Then .BackColor = IIf(Len(.Text) > 0 Or .Name = "ComboBox2", vbWindowBackground, vbRed)
                If .BackColor = vbRed Then NotComplete = True
            End With
        Next a
    Next i

Dave
Again, thank you very much, Dave. With your code provided, it works perfectly.
Of course I am grateful for each and every one of the contributions made by the magnificent members of this wonderful forum, to which I am very honoured and grateful to belong.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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