我希望使我已经工作的代码更有效率。
这是我拥有和工作的代码:
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
这是我正在尝试但似乎不起作用。
For l = 1 To 8
rng.Parent.Cells(LastRow + l, 8).Value = "textBoxTask" & l.Value
Next l
试试以下......
For l = 1 To 8
Rng.Parent.Cells(LastRow + l, 8).Value = Me.Controls("textBoxTask" & l).Value
Next l
我能想到的两种方法。
第一个是关注你的For...Next
循环版本:
Dim l As Long
For l = 1 To 8
Sheet1.Cells(l, 8) = UserForm1.Controls("TextBox" & l)
Next l
第二种方法是使用数组进行一次点击:
With UserForm1
ThisWorkbook.Worksheets("Sheet1").Range("H1:H8") = _
Application.Transpose(Array(.TextBox1, .TextBox2, .TextBox3, .TextBox4, _
.TextBox5, .TextBox6, .TextBox7, .TextBox8))
End With
(如果要将值复制到单行而不是列,则可以删除Application.Transpose
)。