Check each Textbox has a value before running code

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,738
Office Version
  1. 2007
Platform
  1. Windows
Afternoon,

I am using the code below on a userform but before that code is allowed to run i wish to make sure that each Textbox has a value in it first otherwise show Msgbox to alert user.
Textboxes are 1 - 8

Please can you advise Thanks
So if all Textboxes have a value the run the code below.
If one or more have NO value then show Msgbox untill each have a value then run the below code.

Doesnt need to be fancy just something simple.

Rich (BB code):
Sub EnterData(Optional ByVal HideTextbox As Boolean)
    Dim i             As Long
    Dim Box(1 To 2)   As MSForms.TextBox
    
    For i = 1 To 8
        Set Box(1) = Me.Controls("TextBox" & (9 - i))
        Set Box(2) = Me.Controls("TextBox" & i + 8)
        Box(1).Visible = HideTextbox: Box(2).Visible = Not HideTextbox
        If Not HideTextbox Then Box(2).Value = Box(1).Value Else _
        Box(1).Value = "": Box(2).Value = ""
        Erase Box
    Next i
    'Show/Hide Labels
    Me.Label1.Visible = HideTextbox
    Me.Label2.Visible = Not HideTextbox
    
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Trying this but line in Red shown syntax error

Rich (BB code):
    Dim i As Integer
    Dim ControlsArr As Variant, ctrl As Variant
    Dim x As Long
    For i = 1 To 8
       With Me.Controls("TextBox" & i)
            If TextBox <= 0 Then
                MsgBox "TEXTBOX IS EMPTY", 48, "TRANSFER CODE"
                .SetFocus
                Exit Sub
            End If
        End With
    Next i
 
Upvote 0
Maybe this will work?
VBA Code:
Dim i As Integer
    Dim ControlsArr As Variant, ctrl As Variant
    Dim x As Long
    For i = 1 To 8
       With Me.Controls("TextBox" & i)
            If .Value = "" Then
                MsgBox "TEXTBOX IS EMPTY", 48, "TRANSFER CODE"
                .SetFocus
                Exit Sub
            End If
        End With
    Next i
 
Upvote 0
Solution

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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