Doesn't copy data correctly in right location into lastrow when loop multiple textboxes on userform

abdo meghari

Active Member
Joined
Aug 3, 2021
Messages
465
Office Version
  1. 2019
Hi guys
as in the picture I have multiple textboxes on userform when I specific rows on userform should copy start from row2 and column A: G
so based on the pic 1
BR.JPG



and when copy to sheet should be

MM with corrected CBs v03 a.xlsm
ABCDEFG
1ITEMCODEBRANDTYPEORIGINCOSTSELL
21BSJG580120020BS 1200R20G580JAP200250.00
32BSTG580120020BS 1200R20G580THI120.00230.00
43BSJVSJ140020BS 1400R20VSJJAP300.00350.00
5
BRANDS


but what I got this
MM with corrected CBs v03 a.xlsm
ABCDEFGHI
1ITEMCODEBRANDTYPEORIGINCOSTSELL
2
3
4
5
6
7
8BSJG580120020BS 1200R20G580JAP200250.002.00BSTG580120020
91202303BSJVSJ140020BS 1400R20VSJJAP300
10
BRANDS

and this is the code
VBA Code:
Private Sub CommandButton1_Click()
 
Dim i As Long, j As Long
Dim LstRw As Long, CurrCon As String, ws As Worksheet
Set ws = sheet2
'find last used row
LstRw = ws.Range("B" & Rows.Count).End(xlUp).Row

For i = 1 To 11 'loop userform rows

    For j = 1 To 7 'loop userform columns
        'recreate control name/number
        
        CurrCon = "TextBox" & j + (11 * (i - 1))
        'transfer value to sheet
        ws.Cells(LstRw + i, j) = Me.Controls(CurrCon).Value
    Next j
Next i
MsgBox "  ok"
 
End Sub

columns count on userform are 7,rows are 11
every row on userform start textboxes number consecutively :
66,67,68,69,70,71,72
73,74,75,76,77,78,79
........
how fix this chaos when copy to sheet, and how copy to sheet based on fill multiple rows on userform please?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
@abdo meghari
Try this, if it doesn't work then please share a sample workbook to test.
VBA Code:
Private Sub CommandButton1_Click()
 
Dim i As Long, j As Long
Dim LstRw As Long, CurrCon As String, ws As Worksheet
Dim va, a As Long, b As Long, h As Long

Set ws = sheet2
'find last used row
LstRw = ws.Range("B" & Rows.Count).End(xlUp).Row

    h = 11 * 7    'columns count on userform are 7,rows are 11
    a = 1
    ReDim va(1 To 11, 1 To 7)

    For i = 1 To h 'loop userform textbox
          b = b + 1
            va(a, b) = Me.Controls(i + 65).Value   'textboxes number start at 66
              If b = 7 Then a = a + 1: b = 0
    Next i

Range("A" & LstRw + 1).Resize(UBound(va, 1), UBound(va, 2)) = va

MsgBox "  ok"
 
End Sub
 
Upvote 0
Solution
thanks
error invalid argument in this line
VBA Code:
 va(a, b) = Me.Controls(i + 65).Value   'textboxes number start at 66
FILE.xlsm
BTW : based on original code should start copying from A not to B as I did it.
 
Upvote 0
Sorry, the textbox name are Textbox66, Textbox67, ...etc, right?
So, it should be:
VBA Code:
va(a, b) = Me.Controls("Textbox" & i + 65).Value   'textboxes number start at 66
 
Upvote 0
it's perfect !
can you add line to clear textboxes after copy data from userform to sheet in the end of the code please?
 
Upvote 0

Forum statistics

Threads
1,215,201
Messages
6,123,621
Members
449,109
Latest member
Sebas8956

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