adding new rows to table based on the number in textboxes

fraufreda

Board Regular
Joined
Oct 14, 2010
Messages
190
I need your help please

I have 4 textboxes in a userform. I need to run a loop to go through each box and check if it contains a number greater than 0 and add rows to a table accordingly. summing up all textboxes together is not option.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Is this working for you?

Code:
Private Sub CommandButton1_Click()
Dim i, Rc As Integer
Dim TBL As ListObject
Set TBL = Sheet1.Range("Tabel1").ListObject
For i = 1 To 4
If Controls("TextBox" & i).Text <> "" Then
    If CInt(Controls("TextBox" & i).Value) > 0 Then
    Rc = Sheet1.Range("Tabel1").Rows.Count
        Do Until Sheet1.Range("Tabel1").Rows.Count = CInt(Controls("TextBox" & i).Value) + Rc
        TBL.ListRows.Add
        Loop
    End If
End If
Next i
End Sub
 
Upvote 0
here is my attempt

Code:
Dim ws As Worksheet
Dim i, Rc As Integer
Dim TBL As ListObject

Set ws = Sheets("sheet1")
On Error GoTo ErrorHandler
    
    If Record.ComboBox1.Value = "" Then
        MsgBox "please select"
        Exit Sub
    Else
        
        Set TBL = ws.Range("Tbl").ListObject
        If ws.Range("Tbl").Rows.Count = 1 And TB1.Value = 1 And H1.Value <> "" And H1.Value > 0 Then
            TBL.ListRows.Add.Range(1, 1).Offset(0, 0).Value = DTPicker1.Value
            TBL.ListRows.Add.Range(1, 1).Offset(0, 1).Value = ComboBox1.Value
            TBL.ListRows.Add.Range(1, 1).Offset(0, 2).Value = H1.Value
            If ComboBox2.Value <> "" Then TBL.ListRows.Add.Range(1, 1).Offset(0, 3).Value = CB1.Value & CB11.Value
        Else
            For i = 1 To 4
            If Controls("TB" & i).Text <> "" And Controls("H" & i).Text <> "" Then
                If CInt(Controls("TB" & i).Value) > 0 And CInt(Controls("H" & i).Value) > 0 Then
                    For Rc = 1 To CInt(Controls("TB" & i).Value)
                        TBL.ListRows.Add.Range(Rc, 1).Offset(0, 0) = DateAdd("d", Rc - 1, Controls("DTPicker" & i).Value)
                        TBL.ListRows.Add.Range(Rc, 1).Offset(0, 1) = ComboBox1.Value
                        TBL.ListRows.Add.Range(Rc, 1).Offset(0, 2) = Controls("H" & i).Value
                        If Controls("CB" & i).Value <> "" Then TBL.ListRows.Add.Range(Rc, 1).Offset(0, 3) = Controls("CB" & i).Value & Controls("CB" & i & i).Value
                    Next
                End If
            End If
            Next i
        End If
    End If
 
Upvote 0

Forum statistics

Threads
1,223,961
Messages
6,175,651
Members
452,664
Latest member
alpserbetli

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