gpt4 book ai didi

excel - 如何在Excel VBA宏中循环遍历行列

转载 作者:行者123 更新时间:2023-12-01 19:49:44 27 4
gpt4 key购买 nike

您好,我正在尝试创建一个宏,该宏具有一个循环,该循环为每个工作站复制第 1 列(VOL)下的函数和第 2 列(容量)下的另一个函数。这是我到目前为止所拥有的:

Sub TieOut()
Dim i As Integer
Dim j As Integer

For i = 1 To 3
For j = 1 To 3
Worksheets("TieOut").Cells(i, j).Value = "'=INDEX('ZaiNet Data'!$A$1:$H$39038,MATCH('INDEX-MATCH'!Z$7&TEXT('INDEX-MATCH'!$A9,"m/dd/yyyy"),'ZaiNet Data'!$C$1:$C$39038,0), 4)"
Next j
Next i

End Sub

我想要的图片如下:您可以看到我已手动将两个函数复制并粘贴到每列中。我只需要一个可以循环它的宏。

alt text

我想要在每个电台的 VOL 列中循环的函数是:

=INDEX('ZaiNet Data'!$A$1:$H$39038,MATCH('INDEX-MATCH'!Z$7&TEXT('INDEX-MATCH'!$A438,"M/DD/YYYY"),'ZaiNet Data'!$C$1:$C$39038,0), 4)

我想要在每个站点的 CAPACITY 列中循环的函数是:

=INDEX('ZaiNet Data'!$A$1:$H$39038,MATCH('INDEX-MATCH'!Z$7&TEXT('INDEX-MATCH'!$A438,"M/DD/YYYY"),'ZaiNet Data'!$C$1:$C$39038,0), 5)

有人可以帮忙吗?谢谢!

更新

****如何让循环自动运行,而无需在前两个单元格中手动输入公式并单击宏?
另外,如何使循环遍历所有列/行? (水平)****

我提供了两个屏幕截图来说明我的意思。下面是我当前的代码。 alt text alt text谢谢!

    Sub Loop3()
Selection.Copy
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
ActiveCell.Offset(-1, 1).Select
Selection.Copy
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
ActiveCell.Offset(0, -1).Select

Dim i As Integer
Dim j As Integer
With Worksheets("Loop")
i = 1
Do Until .Cells(10, i).Value = "blank"
For j = 1 To 10
.Cells(j, i).Formula = "=INDEX('ZAINET DATA'!$A$1:$H$39038,MATCH(Loop!E$7&TEXT(Loop!$A9,""M/D/YYYY""),'ZAINET DATA'!$C$1:$C$39038,0),4)"
.Cells(j, i + 1).Formula = "=INDEX('ZAINET DATA'!$A$1:$H$39038,MATCH(Loop!E$7&TEXT(Loop!$A9,""M/D/YYYY""),'ZAINET DATA'!$C$1:$C$39038,0),5)"
Next j
i = i + 2
Loop
End With

Selection.Copy
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
ActiveCell.Offset(-1, 1).Select
Selection.Copy
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
ActiveCell.Offset(0, -1).Select

End Sub

最佳答案

这是我的建议:

Dim i As integer, j as integer

With Worksheets("TimeOut")
i = 26
Do Until .Cells(8, i).Value = ""
For j = 9 to 100 ' I do not know how many rows you will need it.'
.Cells(j, i).Formula = "YourVolFormulaHere"
.Cells(j, i + 1).Formula = "YourCapFormulaHere"
Next j

i = i + 2
Loop
End With

关于excel - 如何在Excel VBA宏中循环遍历行列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1128841/

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