Share via

Userform Controls Help

Anonymous
2019-01-10T20:06:26+00:00

I have created a UserForm that pops up when my document is opened. 

You fill out the UserForm and the values you typed in the text boxes transfer to their location within the document. 

Is there a code I can write so that if a section is not filled out, the UserForm will not be able to be "submitted"

Or

Is there a code I can write so that if a section is left blank, the corresponding location (Bookmark) within the document is highlighted?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2019-01-10T21:28:51+00:00

    If there is nothing in the range of the bookmark, there is nothing to color.  However you could use code such as:

    If Me.TextBox18.Value = "" then

        TOF.Text = "No Response"

        TOF.Font.Color = wdColorRed

    Else

        TOF.Text = Me.TextBox18.Value

    End If

    I would strongly suggest that you name the controls on the form with names that impart some meaning to them

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2019-01-10T21:09:07+00:00

    I figured out a code so that if a section is not filled out, the UserForm will not be able to be submitted. 

    However I am still wondering about the second question. 

    The code I have written is as follows - 

    Private Sub CommandButton1_Click()

        Dim ans As Long

    For Each ctrl In Me.Controls

        Select Case True

            Case TypeOf ctrl Is MSForms.CheckBox, TypeOf ctrl Is MSForms.OptionButton

                Select Case ctrl.Value

                    Case True

                        ctrl.BackColor = vbGreen

                    Case Else

                        ctrl.BackColor = vbRed: ans = ans + 1

                End Select

            Case TypeOf ctrl Is MSForms.TextBox, TypeOf ctrl Is MSForms.ComboBox

                Select Case ctrl.Value

                    Case vbNullString

                        ctrl.BackColor = vbRed: ans = ans + 1

                    Case Else

                        ctrl.BackColor = vbGreen

                End Select

        End Select

    Next ctrl

    If ans > 0 Then

    MsgBox "Please enter data in the required fields.  Controls in Red need values", vbInformation, "Enter data"

        Exit Sub

        Else

        Dim TOF As Range

        Set TOF = ActiveDocument.Bookmarks("TOF").Range

        TOF.Text = Me.TextBox18.Value

        Dim CS As Range

        Set CS = ActiveDocument.Bookmarks("CS").Range

        CS.Text = Me.TextBox20.Value

        Dim NO As Range

        Set NO = ActiveDocument.Bookmarks("NO").Range

        NO.Text = Me.TextBox19.Value

        Dim MY As Range

        Set MY = ActiveDocument.Bookmarks("MY").Range

        MY.Text = Me.TextBox21.Value

        Dim TOF2 As Range

        Set TOF2 = ActiveDocument.Bookmarks("TOF2").Range

        TOF2.Text = Me.TextBox18.Value

        Dim CS2 As Range

        Set CS2 = ActiveDocument.Bookmarks("CS2").Range

        CS2.Text = Me.TextBox20.Value

        Me.Repaint

        UserForm1.Hide

        End If

    End Sub

    Private Sub UserForm_Click()

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more