I have 6 textboxes on a userform but require only 1 entry.

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have 6 textboxes numbered 2-7 on a userform.

I only want 1 with a value in when I press the command button to transfer the value to the worksheet.

If there is a value in 2 or more I'd like the textboxes cleared and a message saying "only one textbox value please"

any help would be appreciated.

Kind Regards

Dan
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Here's one way
Code:
Dim i As Long
Dim FirstFull As Long

For i = 2 To 7
    With Me.Controls("TextBox" & i)
        If .Text <> vbNullString Then
            If FirstFull <> 0 Then
                .Text = vbNullString
                FirstFull = -1 * Abs(FirstFull)
            Else
                FirstFull = i
            End If
        End If
    End With
Next i
If FirstFull < 0 Then
    Me.Controls("TextBox" & Abs(FirstFull)).Text = vbNullString
    MsgBox "only one textbox please"
End If

Another approach would be to disable the other textboxes whenever a textbox gets an entry

Code:
Private Sub TextBox2_Change()
    TextBoxEnabelment (TextBox2.Text = vbNullString)
End Sub
Private Sub TextBox3_Change()
    TextBoxEnabelment (TextBox3.Text = vbNullString)
End Sub
Private Sub TextBox4_Change()
    TextBoxEnabelment (TextBox4.Text = vbNullString)
End Sub
'...
Private Sub TextBox7_Change()
    TextBoxEnabelment (TextBox7.Text = vbNullString)
End Sub

Sub TextBoxEnabelment(flag As Boolean)
    Dim i As Long
    For i = 2 To 7
        With Me.Controls("TextBox" & i)
           .Enabled = (.Text = vbNullString) Imp flag
        End With
    Next i
End Sub
 
Last edited:
Upvote 0
Something along these lines:

Code:
Private Sub CommandButton1_Click()

Dim filledBoxes As Long
Dim thisBox As Long

filledBoxes = 0
For thisBox = 2 To 7
    If Controls("TextBox" & CStr(thisBox)).Text <> "" Then
        filledBoxes = filledBoxes + 1
    End If
Next thisBox

If filledBoxes > 1 Then
    MsgBox "only one textbox value please"
    For thisBox = 2 To 7
        Controls("TextBox" & CStr(thisBox)).Text = ""
    Next thisBox
End If

End Sub

WBD
 
Upvote 0
Thank you mikerickson & WBD for your responses.

mikerickson I have used your first suggestion and I was wondering if you would modify it slightly for me so a message box pops up if none of the textboxes have been edited?

Thanks again

Dan
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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