再次与嵌套循环VBA混淆

时间:2018-05-30 11:39:44

标签: excel vba

我再次遇到了一些应该是基本的问题。我知道这个问题有一个更快的解决方案,但我似乎无法让循环正确。

基本上我想缩短这段代码,因为我有一个更大的表和数组来处理下面的问题,下面我当前的解决方案需要很多行。

源表有10列和4行..我需要从源表中读取数据并将其分配给目标表“d105WeeklyTable”的一行

非常感谢任何帮助。

With ActiveSheet.ListObjects("d105WeeklyTable")
    For i = 1 To .ListRows.Count
        If .DataBodyRange(i, 2).Value = wkComm Then

                .DataBodyRange(i, 132).Value = d105TicksheetNightsFigures(1, 2)
                .DataBodyRange(i, 133).Value = d105TicksheetNightsFigures(1, 3)
                .DataBodyRange(i, 134).Value = d105TicksheetNightsFigures(1, 4)
                .DataBodyRange(i, 135).Value = d105TicksheetNightsFigures(1, 5)
                .DataBodyRange(i, 136).Value = d105TicksheetNightsFigures(1, 6)
                .DataBodyRange(i, 137).Value = d105TicksheetNightsFigures(1, 7)
                .DataBodyRange(i, 138).Value = d105TicksheetNightsFigures(1, 8)
                .DataBodyRange(i, 139).Value = d105TicksheetNightsFigures(1, 9)
                .DataBodyRange(i, 140).Value = d105TicksheetNightsFigures(1, 10)

                .DataBodyRange(i, 141).Value = d105TicksheetNightsFigures(2, 2)
                .DataBodyRange(i, 142).Value = d105TicksheetNightsFigures(2, 3)
                .DataBodyRange(i, 143).Value = d105TicksheetNightsFigures(2, 4)
                .DataBodyRange(i, 144).Value = d105TicksheetNightsFigures(2, 5)
                .DataBodyRange(i, 145).Value = d105TicksheetNightsFigures(2, 6)
                .DataBodyRange(i, 146).Value = d105TicksheetNightsFigures(2, 7)
                .DataBodyRange(i, 147).Value = d105TicksheetNightsFigures(2, 8)
                .DataBodyRange(i, 148).Value = d105TicksheetNightsFigures(2, 9)
                .DataBodyRange(i, 149).Value = d105TicksheetNightsFigures(2, 10)

                .DataBodyRange(i, 150).Value = d105TicksheetNightsFigures(3, 2)
                .DataBodyRange(i, 151).Value = d105TicksheetNightsFigures(3, 3)
                .DataBodyRange(i, 152).Value = d105TicksheetNightsFigures(3, 4)
                .DataBodyRange(i, 153).Value = d105TicksheetNightsFigures(3, 5)
                .DataBodyRange(i, 154).Value = d105TicksheetNightsFigures(3, 6)
                .DataBodyRange(i, 155).Value = d105TicksheetNightsFigures(3, 7)
                .DataBodyRange(i, 156).Value = d105TicksheetNightsFigures(3, 8)
                .DataBodyRange(i, 157).Value = d105TicksheetNightsFigures(3, 9)
                .DataBodyRange(i, 158).Value = d105TicksheetNightsFigures(3, 10)

                .DataBodyRange(i, 159).Value = d105TicksheetNightsFigures(4, 2)
                .DataBodyRange(i, 160).Value = d105TicksheetNightsFigures(4, 3)
                .DataBodyRange(i, 161).Value = d105TicksheetNightsFigures(4, 4)
                .DataBodyRange(i, 162).Value = d105TicksheetNightsFigures(4, 5)
                .DataBodyRange(i, 163).Value = d105TicksheetNightsFigures(4, 6)
                .DataBodyRange(i, 164).Value = d105TicksheetNightsFigures(4, 7)
                .DataBodyRange(i, 165).Value = d105TicksheetNightsFigures(4, 8)
                .DataBodyRange(i, 166).Value = d105TicksheetNightsFigures(4, 9)
                .DataBodyRange(i, 167).Value = d105TicksheetNightsFigures(4, 10)

        End If

    Next i
End With

1 个答案:

答案 0 :(得分:2)

尝试此代码(从第2列开始传输数据):

Const tRows as Long = 4
Const tColumns as Long = 10    

Dim i as Long, rowCounter as Long, colCounter as Long, outStart as Long
outStart = 132

With ActiveSheet.ListObjects("d105WeeklyTable")
    For i = 1 To .ListRows.Count
        If .DataBodyRange(i, 2).Value = wkComm Then
            For rowCounter = 1 to tRows
                For colCounter = 2 to tColumns                        
                    .DataBodyRange(i, outStart).Value = d105TicksheetNightsFigures(rowCounter, colCounter)
                    outStart = outStart + 1                        
                Next colCounter
            Next rowCounter
        End If
    Next i
End With