将数组写入工作表并重复n次

时间:2018-11-18 16:57:57

标签: excel excel-vba

我正在处理要在其中写2次数组(在“输入表”中分配)到“输出”表的代码,即在循环中具体写2次。我想使用数组,因为id的范围及其名称可以更改(可以更多)。 从一个简单的示例(数据量少)开始,将为这些数组分配acc。到“输入”表中的数据: enter image description here

这2个数组应该被写到“输出”表n次,即;应该在循环中一次又一次地写入它们,即2次。我想在循环中做到这一点,以使其在将来编写时具有灵活性,例如3 4 3在这个例子中,我做了两次。在每个书面数组之前,应写一个标题“ Title”,在书面数组的末尾应写文本“ Total”,因此这是我想要的结果: enter image description here

我的代码仅第一次写入2个数组,但第二次不写入这2个数组。相反,我得到了其他错误的信息: enter image description here

这是我的代码:

Sub Write1()
Dim r As Long
Dim c As Long
Dim Start_Row As Long
Dim End_Row As Long
Dim main As Integer
Dim lngRowCount As Long
Dim w_Output As Worksheet
Dim w1 As Worksheet

Dim intLastRow As Integer
Const RowStart As Integer = 3
Const ColumnID As Integer = 1
Const Column_Desc As Integer = 3

Dim arrID() As Variant
Dim arrDesc() As Variant

With ThisWorkbook
    Set w1 = .Sheets("Input")
    Set w_Output = .Sheets("Output")
End With

'***********************************
'arrays
With w1
    intLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
    'IntLastCol = .Cells(4, Columns.Count).End(xlToLeft).Column
    arrID = .Range(.Cells(RowStart, ColumnID), .Cells(intLastRow, ColumnID))
    arrDesc = .Range(.Cells(RowStart, Column_Desc), .Cells(intLastRow, Column_Desc))

'******************************************

main = 1
End_Row = 2  'this is the 2nd iteration to write arrays

For Start_Row = 1 To End_Row
    w_Output.Cells(main, 3) = "Title"

    main = main + 1

    For r = 1 To UBound(arrID, 1)

        If Len(arrID(r, 1)) > 0 Then

            'Write
                w_Output.Cells(r + 1, 3) = arrID(r, 1)
                w_Output.Cells(r + 1, 4) = arrDesc(r, 1)

        End If


    main = main + 1

    w_Output.Cells(main, 3) = "Total "

    Next r

    main = main + 4
Next Start_Row

End With

MsgBox "Done", vbInformation
End Sub

有人知道我在循环中做错了什么吗?

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了,原来我只是应该使用'main'作为写表的行,而不是用于数组的'r'-这是数组中代码的一部分被写入工作表。

Sub Write1()
Dim r As Long
Dim c As Long
Dim d As Long
Dim Start_Row As Long
Dim End_Row As Long
Dim main As Integer
Dim lngRowCount As Long
Dim w_Output As Worksheet
Dim w1 As Worksheet

Dim intLastRow As Integer
Dim IntLastCol As Integer
Const RowStart As Integer = 3
Const ColumnID As Integer = 1
Const Column_Desc As Integer = 3

Dim arrID() As Variant
Dim arrDesc() As Variant

With ThisWorkbook
    Set w1 = .Sheets("Input")
    Set w_Output = .Sheets("Output")
End With

'***********************************
'arrays
With w1
    intLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
    arrID = .Range(.Cells(RowStart, ColumnID), .Cells(intLastRow, ColumnID))
    arrDesc = .Range(.Cells(RowStart, Column_Desc), .Cells(intLastRow, Column_Desc))

'******************************************

main = 1
End_Row = 2

For Start_Row = 1 To End_Row
    w_Output.Cells(main, 3) = "Title"

    main = main + 1

    For r = 1 To UBound(arrID, 1)

        If Len(arrID(r, 1)) > 0 Then

            'Write
                w_Output.Cells(main, 3) = arrID(r, 1)
                w_Output.Cells(main, 4) = arrDesc(r, 1)

        End If


    main = main + 1

    Next r

    w_Output.Cells(main, 3) = "Total "
    main = main + 4

Next Start_Row

End With

MsgBox "Done", vbInformation
End Sub

它运行完美。