Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

loop TextBox data copy to excel

0

hi   guys

I  have  simple  userform   and  I    have  about  six  textboxes   .should  copy  each  three textboxes  to  sheet in COL A,B,C   so    I  try  using  loop and  array  to  become  the  code  is  fast   but  not works  so  far 

this  is  what   I  have 

Private Sub CommandButton1_Click()
   Dim i As Long, j As Long
   Dim Ary(6) As Variant
   For i = 1 To 6
      If Me.Controls("TextBox" & i) <> "" Then
         Ary(j) = Me.Controls("TextBox" & i).Value
         j = j + 1
      End If
   Next i
   ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row = Ary(j)

any  suggetion  to  achieve  that .

Answer
Discuss

Answers

0
Selected Answer

Tubrak

Your code correctly populates the array but does not transfer it to your sheet.

One problem is that in your UserForm1, the text box numbers ran along 1, 2, 4 then 3, 5, 6  (so I swapped position of 3 and 4 plus retitled CommandButton1).

If you want to use an array, then better to make it a 2-D artray (with rows and columns linke your UserForm/ worksheet) and this modified code will work:

Private Sub CommandButton1_Click()

Dim i As Long, j As Long
Dim Ary(1 To 2, 1 To 3) As Variant 'make array like text box rows/columns

For i = 1 To 2 'loop array rows
    For j = 1 To 3 'loop array columns
'If Me.Controls("TextBox" & i) <> "" Then
   Ary(i, j) = Me.Controls("TextBox" & j + (3 * (i - 1))).Value
'End If
    Next j
Next i

'find next free row
j = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row + 1
'copy array to range
ActiveSheet.Range("A" & j & ":C" & j + 1).Value = Ary

Unload Me

End Sub
where the calculation  j + (3 * (i - 1)) determines the text box number based on the row and column counters (e.g. is 1=1 and j=1, it evaluates to 4)

In fact you don't need an array and con use that sum to copy cells directly to the sheet (since the speed of an array isn't needed here). This (commented) code is in the revised sheet attached:

Private Sub CommandButton1_Click()

Dim i As Long, j As Long
Dim LstRw As Long, CurrCon As String

'find last used row
LstRw = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To 2 'loop userform rows

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

Unload Me

End Sub
where a new variable CurrCon uses the sum as part of the string. You will see I've added a button to sheet 1 to launch the form (and added the Unload statement to the sub to close the UserForm1 after entry)

Notice that I have commented out (or not used) your test for a blank value- that's because your version would have confused the population of the array (it could skip aray cells).

What I think you need is a test to see if a row is complete but that can be done when you write the subs to calculate TextBox 3 (and 6) values from the Unit and Price text boxes (while making TextBox 3 and 6 calculated labels). In that case you will need both a price and a value anyway.

Hope this helps.

Discuss

Discussion

Hi John,
your file is not the same file I attached and try to implement your codes in my file , but  nothing happens . there is no error.
tubrak (rep: 24) Oct 4, '21 at 6:28 am
Oops! I attached a file from another Answer yesterday. See correct file, in Answer now.
John_Ru (rep: 6142) Oct 4, '21 at 6:35 am
Click "Launch Form" green button on sheet1, add values to textboxes  then click Enter on UserForm.
John_Ru (rep: 6142) Oct 4, '21 at 6:37 am
I just checked again and both code revisions work.
John_Ru (rep: 6142) Oct 4, '21 at 6:39 am
I  no  know  why  it  doesn't  work  for  my  file  .  the  problem  is  the  file  . I  create  a new  file   and  the  codes work  perfectly  .
thanks  very  much  for  your assistance .
tubrak (rep: 24) Oct 4, '21 at 6:47 am
Tubrak. Good that the solution works (and thanks for selecting my Answer).

Not sure why the file does not work- it could be an Excel language thing- I see VB Explorer refers to æÑÞÉ1 in your original file (where the English version has "sheet1" instead) but I don't recognise your language, sorry.
John_Ru (rep: 6142) Oct 4, '21 at 6:55 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login