如何获得嵌套循环以移动到VBA中的下一组数据

问题描述 投票:0回答:1

我有一个工作表(CAT),我试图在其中将15行的8列数据复制到另一工作表中的一行中(Transpose)。我有多个嵌套循环,需要循环中的一个循环进入CAT工作表(rowIndex)中的下一组15行。相反,代码返回到前2到16行,并且每次都填充相同的15行。我尝试了添加rowIndex = rowIndex + 15的多次迭代,但这似乎无济于事。谁能帮我解决我做错的事情?

    For rowTranspose = 2 To 3 'move down one row when first row is filled in Transpose sheet
       For colTranspose = 8 To 120 Step 8 'write data from columns 8 to 15 and rows 2 to 16 (colIndex, rowIndex) in columns 8 through 120 of Transpose sheet
           For rowIndex = 2 To 16 'loop through rows 2 through 16
               For colIndex = 8 To 15 'loop through columns 8 through 15
                    Transpose.Cells(rowTranspose, colTranspose) = CAT.Cells(rowIndex, colIndex) 'copy CAT worksheet values to rowTranspose and colTranspose values
                    colTranspose = colTranspose + 1 'move the columns over so that the CAT info copies to the correct column
               Next colIndex
           Next rowIndex
       Next colTranspose
       rowIndex = rowIndex + 15
    Next rowTranspose
excel vba
1个回答
0
投票

rowIndex的值由循环设置,因此rowIndex = rowIndex + 15不执行任何操作。添加另一个名为rowOffset的变量,并将其添加到'rowIndex'。

© www.soinside.com 2019 - 2024. All rights reserved.