因此,我不得不编写两个几乎相同的代码块来遍历我的两个命名范围。然而,我知道命名范围总是彼此大小和形状相同,甚至在同一列(不同的行)中开始,它们还需要粘贴到彼此相邻的两列中,所以我觉得这应该可以在一个代码块中实现,但我甚至不知道如何开始攻击它。例如,每月现金付款在数组A10:D20中,P&L每月付款在数组A40:D50中。
有谁有什么想法,谢谢你?
For Each Row In Range(Names("Cash_Payments_Monthly")).Rows
LastRow = wsDashData.Cells(Rows.Count, 14).End(xlUp).Row
Row.Copy
wsDashData.Range("n" & LastRow + 1).PasteSpecial _
Paste:=xlPasteValues, _
Transpose:=True
Next Row
For Each Row In Range(Names("PL_Payments_Monthly")).Rows
LastRow = wsDashData.Cells(Rows.Count, 15).End(xlUp).Row
Row.Copy
wsDashData.Range("o" & LastRow + 1).PasteSpecial _
Paste:=xlPasteValues, _
Transpose:=True
Next Row
发布于 2020-09-28 23:32:08
假设您的工作簿中有其他命名区域,您应该首先创建要搜索的命名区域的白名单数组,然后迭代该数组,在该循环中嵌入现有代码的单个副本...
Dim myranges()
Dim c As Integer 'counter
myranges = Array("Cash_Payments_Monthly", "PL_Payments_Monthly")
For c = 0 To UBound(myranges)
For Each Row In Range(myranges(c)).Rows
...the rest of your code, but just one instance of it :-) ...
Next c
https://stackoverflow.com/questions/64104892
复制相似问题