Daniel_Roberts

New Member
Joined
Jun 4, 2019
Messages
2
Hello all,

I'm building a worksheet with several ActiveX text boxes for data entry. I'm trying to write code that would prompt the user with a message box listing any text boxes that they have not completed before moving the entries to the spreadsheet. When I run my code, it doesn't return an error, however it also doesn't seem to work... Thanks in advance!

Code:
Sub TEST()
Dim fTextBox As OLEObject
Dim xTxtName As String
Dim xEptTxtName As String
For Each fTextBox In Sheet2.OLEObjects
    If TypeName(fTextBox) = "TextBox" Then
        If fTextBox.Text = "" Then
            xEptTxtName = xEptTxtName & fTextBox.Name & vbNewLine
        End If
    End If
Next
If xEptTxtName <> "" Or xTxtName <> "" Then
    MsgBox ("Please provide the following information:" & vbNewLine & "" & vbNewLine & xEptTxtName & vbNewLine & xTxtName)
End If
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi,
welcome to forum

try this update to your code

Code:
Sub TEST()
    Dim fTextBox As OLEObject
    Dim xTxtName As String
    Dim xEptTxtName As String

    For Each fTextBox In Sheet2.OLEObjects
        If TypeOf fTextBox.Object Is msforms.TextBox Then
            If Len(fTextBox.Object.Text) = 0 Then
                xEptTxtName = xEptTxtName & fTextBox.Name & vbNewLine
            End If
        End If
    Next

    If Len(xEptTxtName) > 0 Then
        MsgBox "Please provide the following information:" & _
        vbNewLine & "" & vbNewLine & _
        xEptTxtName & vbNewLine & _
        xTxtName, 48, "Entry Required"
    End If
End Sub

Dave
 
Last edited:
Upvote 0
you could try this..

Code:
Sub EmptyTextboxes()
    Dim sh As Shape, xEptTxtName As String
    For Each sh In Sheet2.Shapes
        If sh.Type = msoOLEControlObject Then
            If TypeName(sh.OLEFormat.Object.Object) = "TextBox" Then
                If sh.OLEFormat.Object.Object = "" Then xEptTxtName = xEptTxtName & sh.Name & vbCr
            End If
        End If
    Next sh
    If xEptTxtName <> "" Then MsgBox ("Please provide the following information:" & vbNewLine & "" & vbCr & xEptTxtName)
End Sub
 
Upvote 0
Hi,
welcome to forum

try this update to your code

Code:
Sub TEST()
    Dim fTextBox As OLEObject
    Dim xTxtName As String
    Dim xEptTxtName As String

    For Each fTextBox In Sheet2.OLEObjects
        If TypeOf fTextBox.Object Is msforms.TextBox Then
            If Len(fTextBox.Object.Text) = 0 Then
                xEptTxtName = xEptTxtName & fTextBox.Name & vbNewLine
            End If
        End If
    Next

    If Len(xEptTxtName) > 0 Then
        MsgBox "Please provide the following information:" & _
        vbNewLine & "" & vbNewLine & _
        xEptTxtName & vbNewLine & _
        xTxtName, 48, "Entry Required"
    End If
End Sub

Dave


This worked perfect! I'm hoping to tweak it a little bit so the message box list includes combo boxes too? Any ideas?

And is there a way that I could exclude a couple text boxes?

Thanks!
 
Upvote 0
This worked perfect! I'm hoping to tweak it a little bit so the message box list includes combo boxes too? Any ideas?

And is there a way that I could exclude a couple text boxes?

Thanks!

try this update

Rich (BB code):
Sub TEST()
    Dim fTextBox As OLEObject
    Dim xTxtName As String, xEptTxtName As String
    Dim TobeExcluded As Boolean
    
    For Each fTextBox In Sheet2.OLEObjects
        Select Case True
        Case TypeOf fTextBox.Object Is MSForms.TextBox, TypeOf fTextBox.Object Is MSForms.ComboBox
        TobeExcluded = Not IsError(Application.Match(fTextBox.Name, Array("TextBox1", "TextBox4"), 0))
        
        If Not TobeExcluded Then
            If Len(fTextBox.Object.Text) = 0 Then
                xEptTxtName = xEptTxtName & fTextBox.Name & vbNewLine
            End If
        End If
        
        End Select
    Next
    
    If Len(xEptTxtName) > 0 Then
        MsgBox "Please provide the following information:" & _
        vbNewLine & "" & vbNewLine & _
        xEptTxtName & vbNewLine & _
        xTxtName, 48, "Entry Required"
    End If
    
End Sub

add / update the name(s) of the controls you want to exclude from the check in the array shown in RED.

Dave
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

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