For each loop TextBox data copy to excel

beman

New Member
Joined
Jan 25, 2017
Messages
8
Hi,

I'm new to VBA so I get stuck from time to time :stickouttounge:

Since a couple of days I was struggeling with the For Each Loop in combination with getting the value from textboxes in a userform and copying these to excel...

The code picks up de data entered in the userform textbox's but instead of adding them only one time for each textbox value it's adding it 91 time, that 1820 lines added to excel and I just need the text of all 20 textboxes once :stickouttounge:

Does anybody know what the problem is with my code?

Code:
Dim i As Integer
Dim TextData As String
Dim lastRow As Long


For i = 1 To 20
    For Each txtPart In Controls
        TextData = Controls("txtPart" & i).Value
        lastRow = Sheets("DePop").Range("A" & Rows.Count).End(xlUp).Row
        Sheets("DePop").Cells(lrReg + 1, "A").Value = TextData
        Next txtPart
Next i
Thanks for the help!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Assuming you have txtPart1, txtPart2, ..., txtPart20 controls on your form:

Code:
Dim i As Integer
Dim lastRow As Long

lastRow = Sheets("DePop").Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To 20
        Sheets("DePop").Cells(lastRow + i, "A").Value = Controls("txtPart" & i).Value
Next i

*NB: Untested

WBD
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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