0

I am looking to make my already working code more efficient.

This is the code that I have and works:

rng.Parent.Cells(LastRow + 1, 8).Value = textBoxTask1.Value
rng.Parent.Cells(LastRow + 2, 8).Value = textBoxTask2.Value
rng.Parent.Cells(LastRow + 3, 8).Value = textBoxTask3.Value
rng.Parent.Cells(LastRow + 4, 8).Value = textBoxTask4.Value
rng.Parent.Cells(LastRow + 5, 8).Value = textBoxTask5.Value
rng.Parent.Cells(LastRow + 6, 8).Value = textBoxTask6.Value
rng.Parent.Cells(LastRow + 7, 8).Value = textBoxTask7.Value
rng.Parent.Cells(LastRow + 8, 8).Value = textBoxTask8.Value

This is what I am trying but doesn't seem to work.

For l = 1 To 8
    rng.Parent.Cells(LastRow + l, 8).Value = "textBoxTask" & l.Value
Next l

2 Answers 2

2

Try the following...

For l = 1 To 8
    Rng.Parent.Cells(LastRow + l, 8).Value = Me.Controls("textBoxTask" & l).Value
Next l
Sign up to request clarification or add additional context in comments.

Comments

1

Two ways I can think of doing it.

The first is following your For...Next loop version:

Dim l As Long
For l = 1 To 8
    Sheet1.Cells(l, 8) = UserForm1.Controls("TextBox" & l)
Next l  

The second way is to do it in one hit with an array:

With UserForm1
    ThisWorkbook.Worksheets("Sheet1").Range("H1:H8") = _
        Application.Transpose(Array(.TextBox1, .TextBox2, .TextBox3, .TextBox4, _
                                    .TextBox5, .TextBox6, .TextBox7, .TextBox8))
End With  

(you can remove the Application.Transpose if you're copying the values to a single row rather than column).

1 Comment

I like the For...Next loop the most. Thank you for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.