要使用循环存储在Excel工作表中的文本框值

时间:2014-11-14 09:44:15

标签: excel vba excel-vba

Dim RI As Integer
Dim CI As Integer
Dim X As Integer
Dim texts() As Variant

ActiveCell.Offset(1, 0).Select
RI = ActiveCell.Row
ActiveSheet.Cells(RI, 1).Select
CI = ActiveCell.Column

texts = Array(Text5.Text, Text6.Text, Text7.Text, Text8.Text, Text9.Text,Text10.Text,   Text11.Text, Text12.Text, Text13.Text, Text14.Text, Text15.Text, Text16.Text, Text17.Text, Text18.Text, Text19.Text)
For CI = 2 To 16
For X = 0 To UBound(texts)
    ActiveSheet.Cells(RI, CI).Value = texts(X)
Next X
Next CI

亲爱的朋友们,    我必须从text5.text到1ext19.text的15个文本框中获取值,并将其存储在从B到P列的Excel工作表中。当我按照上面的代码时,它只返回行的所有单元格中的text19.text 。请建议我的朋友......

1 个答案:

答案 0 :(得分:1)

不需要循环。使用.Resize。这是一个例子

'
'~~> Rest of the code
'
texts = Array("1", "2", "3", "4", "5", "6", "7", "8", _
"9", "10", "11", "12", "13", "14", "15")

'~~> Not needed
'For CI = 2 To 16
    'For X = 0 To UBound(texts)
        'ActiveSheet.Cells(RI, CI).Value = texts(X)
    'Next X
'Next CI

ActiveSheet.Cells(RI, CI).Resize(, 15).Value = texts

修改

如果你仍然想要使用循环,那么就像这样使用它

'
'~~> Rest of the code
'
texts = Array("1", "2", "3", "4", "5", "6", "7", "8", _
"9", "10", "11", "12", "13", "14", "15")

j = 2

'~~> Not needed
For X = 0 To UBound(texts)
    ActiveSheet.Cells(RI, j).Value = texts(X)
    j = j + 1
Next X

在您的代码中,列未正确递增。