Check If textboxes are empty before saving record in Sheet1

haseft

Active Member
Joined
Jun 10, 2014
Messages
321
Hi,
Have problem with this.
I want to ensur that all textboxes are filled before saving the record to the last row+1 in Sheet1.
There are meny textboxes in userform (here is shown only 3).
Help with this please,
VBA Code:
Private Sub cmbSaveRecord_Click()

   Dim i As Long
   Set sht = ThisWorkbook.Worksheets("Sheet1")
   LastRow = sht.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
    For Each ctrl In UserForm1.Controls
        If TypeName(ctrl) = "TextBox" Then
            If ctrl.Text = Empty Then
               MsgBox ctrl.Name & " is empty"
               ctrl.SetFocus
               Exit For
            End If
        End If
    Next ctrl

   '-- save the record in Sheet1 if all textboxes are filled with data
   Worksheets("Sheet1").Range("F" & LastRow).Value = TextBox1.Text
   Worksheets("Sheet1").Range("G" & LastRow).Value = TextBox2.Text
   Worksheets("Sheet1").Range("H" & LastRow).Value = TextBox3.Text

End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi,
Change Exit For to Exit Sub

Rich (BB code):
Private Sub cmbSaveRecord_Click()

   Dim i As Long
   Set sht = ThisWorkbook.Worksheets("Sheet1")
   LastRow = sht.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
    For Each ctrl In UserForm1.Controls
        If TypeName(ctrl) = "TextBox" Then
            If ctrl.Text = Empty Then
               MsgBox ctrl.Name & " is empty"
               ctrl.SetFocus
               Exit Sub
            End If
        End If
    Next ctrl

   '-- save the record in Sheet1 if all textboxes are filled with data
   Worksheets("Sheet1").Range("F" & LastRow).Value = TextBox1.Text
   Worksheets("Sheet1").Range("G" & LastRow).Value = TextBox2.Text
   Worksheets("Sheet1").Range("H" & LastRow).Value = TextBox3.Text

End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,669
Members
448,977
Latest member
moonlight6

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