遍历一个代码块中每个命名范围的第一行

Loop through first row of each named range in one code block

所以我不得不编写两个几乎相同的代码块来遍历我的两个命名范围。但是,我知道命名范围将始终具有相同的大小和形状,甚至从同一列(不同的行)开始,它们也需要粘贴到彼此相邻的两列中,所以我觉得应该这样可能在一个代码块中,但甚至无法考虑如何开始攻击它。例如。 Cash Payments Monthly 在数组 A10:D20 中,P&L Payments Monthly 在数组 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

假设您的工作簿中有其他命名范围,您应该首先创建一个您想要搜索的命名范围的白名单数组,然后遍历该数组,在该循环中嵌入现有代码的单个副本...

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