gpt4 book ai didi

excel - 再次对嵌套循环 VBA 感到困惑

转载 作者:行者123 更新时间:2023-12-02 19:06:32 25 4
gpt4 key购买 nike

我再一次在一些本应是相当基本的事情上遇到了麻烦。我知道这个问题有一个更快的解决方案,但是我似乎无法正确地解决循环。

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

源表有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

最佳答案

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

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

关于excel - 再次对嵌套循环 VBA 感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50603733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com