Excel VBA 数组结果偏移量



我根据输入工作表的结果开发了一个数组。 使用命令按钮,我能够将必要的数据填充到延迟表中。 我面临的问题是我无法让数组结果以我想要的方式在延迟表上展开。 在延迟表上,10 和 11 之间有一列偏移量,13 和 14 之间有 4 列偏移量。

arval = "" 'This will be the total strig value of the individual array values that are captured
a = 0 'counts the total number of rows of data that exist in the array
For i = 2 To lr  'Start the array
aval = wsg.Range("A" & i).Value
If aval = "Y" Then  'Set the search parameters
arval = wsg.Range("B" & i).Value & "~#pop#~"  'Start collecting data with the B column
For j = 3 To 14
arval = arval & wsg.Cells(i, j).Value & "~#pop#~"   'continue collecting information in the various columns
Next j
ReDim Preserve array1(a)
array1(a) = arval
a = a + 1
End If
Next i
wsd.Range("G2:X15").ClearContents  'Clears the inserts range
If a > 0 Then
cr = 2
For i = LBound(array1) To UBound(array1)
cc = 7
newarr = Split(array1(i), "~#pop#~")
For j = LBound(newarr) To UBound(newarr)
wsd.Cells(cr, cc).Value = newarr(j)
cc = cc + 1
Next j
cr = cr + 1
Next i
End If

我一直在考虑使用偏移函数,但无法弄清楚如何在代码中有效地使用它。 我还考虑使用一个额外的数组,比如使用"b"变量,然后将 cc 设置为 11 并继续显示信息。 我有点困惑。 我想跳过 10 到 11 之间的列。 这就是目标。 如果我能得到一个,我就可以得到其余的。

添加一些条件数学,基于假为零,真为负一的前提。

For j = LBound(newarr) To UBound(newarr)
wsd.Cells(cr, cc).Value = newarr(j)
cc = cc + 1 + (abs(cc=10)) + (abs(cc=13)*4)
Next j

我想建议将目标范围设置为不连续,例如

dim r as range
set r  = wsd.range("G2:P2,R2:T2,X2:X2")

然后像这样使用偏移量(我假设数组的 lbound 为 0(。 此外,您将能够省去cc/cr计数的东西,因为该范围应该处理...

For j = LBound(newarr) To UBound(newarr)
r.offset(i,0) = newarr(j)
Next j

无法在这里进行测试,因此您可能需要更正我的代码!

最新更新