loop throught textboxes and copy to sheet

  • hi

    I need fixing this code

    Code
    Private Sub CommandButton1_Click()
     Dim i As Integer
    Dim lastRow As Long
    
    lastRow = Sheets("DATA").Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To 4
            Sheets("DATA").Cells(lastRow + i, "A").Value = Controls("textbox" & i).Value
    Next i
    
    End Sub

    t just copy in one column . each four textboxes (CODE,BRAND,TYPE,ORIGIN) should copy to columns A,B,C,D


    currently the code copy to column A and just textbox1,2,3 ,4 with ignore the rest of textboxes

    so I truly appreciate if anybody help.

  • Look at this example and adapt to your project :


  • thanks I know this way , but if I have much more textboxes , then the code becomes very big .so I want using loop in my case

    to make the code is short . did you see my file?

  • Try this


    Code
    Dim lastRow As Long
    
    With Sheets("DATA")
    lastRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
        .Range(.Cells(lastRow, 1), .Cells(lastRow, 4)).Value = Array(TextBox1, TextBox2, TextBox3, TextBox4)
    End With
  • royUK thanks . what are about the rest of textboxes ? when copy from the userform should be like this

    (textbox1,2,3,4)=columns(A,B,C,D)


    (textbox5,6,7,8)=columns(A,B,C,D)


    (textbox9,10,11,12)=columns(A,B,C,D)


    how can I use loop like this


    for i= 1 to 12 'numbers of textboxes


    for each four textboxes when I fill . should copy to COL A,B,C,D

  • I know .I don't find anything about this idea in the internet so far . I hope to you achieve that.

  • Try this


  • royUK magnificent ! that's what I want . just I would understand theses values ix-4,ix-8 . what means ,please ?

  • Does this help?


Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!