通过特定工作表循环命名的值范围

时间:2016-05-17 14:33:42

标签: excel vba excel-vba

我有一个命名范围的邮政编码,我需要在12个不同的方案选项卡中循环输入单元格,并将这些值吐出到输出选项卡上。我现在的代码循环遍历命名的拉链范围,但只针对一个场景,只在输出选项卡上的一个单元格中粘贴,而不是在所需的24个单元格中粘贴。

Sub RunAllZips()

'Take each zip code in the named range ZipCodes and enter into cell C11
'on each of the 12 Scenario tabs and spit out value onto the Output tab.

Dim Scenarios, i


Scenarios = Array("Scenario 1", "Scenario 2", "Scenario 3", "Scenario 4", "Scenario 5", "Scenario 6", _
"Scenario 7", "Scenario 8", "Scenario 9", "Scenario 10", "Scenario 11", "Scenario 12")

For i = LBound(Scenarios) To UBound(Scenarios)
    With Worksheets(Scenarios(i))

        For Each zip In Range("ZipCodes")
            Sheets("Scenario 1").Select
            Range("C12").Value = zip
            Range("Output1").Select
            Selection.Copy
            Sheets("Output").Select
            Range("C3").Select
            Selection.PasteSpecial Paste:=xlPasteValues

       Next zip


   End With

Next i

End Sub

1 个答案:

答案 0 :(得分:0)

您使用With,但不要使用"锚定"与Range("zipcodes')

试试这个:

For i = LBound(Scenarios) To UBound(Scenarios)
    With Worksheets(Scenarios(i))
        For Each zip In .Range("ZipCodes")
            Sheets("Scenario 1").Range("C12").Value = zip
            Range("Output1").Copy
            Sheets("Output").Range("C3").PasteSpecial Paste:=xlPasteValues
        Next zip
    End With
Next i