If textbox is empty, do not copy the value to worksheet

elbertzeeroone

New Member
Joined
Sep 22, 2023
Messages
1
Office Version
  1. 2013
Platform
  1. Windows
Can anyone help me with my code, I'm new in VB and I want to copy only the textbox with the value to my worksheet. I Hope anyone can help me

VBA Code:
'Copy input values to sheet.
    Set ws = Worksheets("Particulars")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 1).Value = Me.TextBox1.Value
        .Cells(lRow, 2).Value = Me.TextBox2.Value
        .Cells(lRow, 3).Value = Me.TextBox5.Value
        .Cells(lRow, 4).Value = Me.TextBox6.Value
       
    End With
   
    'Copy input values to sheet.
    Set ws = Worksheets("Particulars")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 1).Value = Me.TextBox1.Value
        .Cells(lRow, 2).Value = Me.TextBox2.Value
        .Cells(lRow, 3).Value = Me.TextBox7.Value
        .Cells(lRow, 4).Value = Me.TextBox8.Value
       
    End With
   
    'Copy input values to sheet.
    Set ws = Worksheets("Particulars")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 1).Value = Me.TextBox1.Value
        .Cells(lRow, 2).Value = Me.TextBox2.Value
        .Cells(lRow, 3).Value = Me.TextBox9.Value
        .Cells(lRow, 4).Value = Me.TextBox10.Value
       
    End With
   
    'Copy input values to sheet.
    Set ws = Worksheets("Particulars")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 1).Value = Me.TextBox1.Value
        .Cells(lRow, 2).Value = Me.TextBox2.Value
        .Cells(lRow, 3).Value = Me.TextBox11.Value
        .Cells(lRow, 4).Value = Me.TextBox12.Value
       
    End With
   
    'Copy input values to sheet.
    Set ws = Worksheets("Particulars")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 1).Value = Me.TextBox1.Value
        .Cells(lRow, 2).Value = Me.TextBox2.Value
        .Cells(lRow, 3).Value = Me.TextBox13.Value
        .Cells(lRow, 4).Value = Me.TextBox14.Value
       
    End With
   
    'Copy input values to sheet.
    Set ws = Worksheets("Particulars")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 1).Value = Me.TextBox1.Value
        .Cells(lRow, 2).Value = Me.TextBox2.Value
        .Cells(lRow, 3).Value = Me.TextBox15.Value
        .Cells(lRow, 4).Value = Me.TextBox16.Value
       
    End With
   
      'Copy input values to sheet.
    Set ws = Worksheets("Particulars")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 1).Value = Me.TextBox1.Value
        .Cells(lRow, 2).Value = Me.TextBox2.Value
        .Cells(lRow, 3).Value = Me.TextBox17.Value
        .Cells(lRow, 4).Value = Me.TextBox18.Value
       
    End With
   
      'Copy input values to sheet.
    Set ws = Worksheets("Particulars")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 1).Value = Me.TextBox1.Value
        .Cells(lRow, 2).Value = Me.TextBox2.Value
        .Cells(lRow, 3).Value = Me.TextBox19.Value
        .Cells(lRow, 4).Value = Me.TextBox20.Value
       
    End With
   
      'Copy input values to sheet.
    Set ws = Worksheets("Particulars")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 1).Value = Me.TextBox1.Value
        .Cells(lRow, 2).Value = Me.TextBox2.Value
        .Cells(lRow, 3).Value = Me.TextBox21.Value
        .Cells(lRow, 4).Value = Me.TextBox22.Value
       
    End With
   
      'Copy input values to sheet.
    Set ws = Worksheets("Particulars")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 1).Value = Me.TextBox1.Value
        .Cells(lRow, 2).Value = Me.TextBox2.Value
        .Cells(lRow, 3).Value = Me.TextBox23.Value
        .Cells(lRow, 4).Value = Me.TextBox24.Value
       
    End With
 

Attachments

  • form.JPG
    form.JPG
    122.7 KB · Views: 10
Last edited by a moderator:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I'm guessing maybe something along the lines of this
VBA Code:
Private Sub AddBtn_Click()
    
    Dim ws As Worksheet
    Dim x As Long
    Dim lRow As Long
    
    Set ws = Sheets("Particulars")
    
    For x = 5 To 23 Step 2
        If Controls("TextBox" & x).Value <> "" Then
            With ws
                lRow = ws.Range("A" & .Rows.Count).End(xlUp).Row + 1
                .Cells(lRow, 1).Value = Me.TextBox1.Value
                .Cells(lRow, 2).Value = Me.TextBox2.Value
                .Cells(lRow, 3).Value = Controls("TextBox" & x).Value
                .Cells(lRow, 4).Value = Controls("TextBox" & x + 1).Value
            End With
        End If
    Next x

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,128
Messages
6,123,204
Members
449,090
Latest member
bes000

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