VBA 命名范围偏移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1194107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:38:24  来源:igfitidea点击:

VBA named range offset

excelvbacopy-pastenamed-ranges

提问by Karl

I am trying to write VBA code that will select a named range, copy it and paste it for a certain number of rows. What I need to know is how to select the range of cells corresponding to the ones above.

我正在尝试编写 VBA 代码,该代码将选择一个命名范围,将其复制并粘贴到一定数量的行中。我需要知道的是如何选择与上述单元格对应的单元格范围。

E.g. I have a range "myRange" which refers to: "=$A$1:D$1$,$F$1,$K$1". I want to copy this and paste it in "=$A$2:D$2$,$F$2,$K$2" by referring to "myRange" in stead of to the string of cell references.

例如,我有一个范围“myRange”,它指的是:“=$A$1:D$1$,$F$1,$K$1”。我想通过引用“myRange”而不是单元格引用的字符串来复制它并将其粘贴到“=$A$2:D$2$,$F$2,$K$2”中。

Any help?

有什么帮助吗?

回答by Gary McGill

Something like this?

像这样的东西?

Sub Test()

    Dim oRange As Range
    Set oRange = ActiveSheet.Range("A1:D1") ' Change this to point at the range to be copied

    Dim i As Integer
    For i = 1 To 10
        oRange.Copy
        oRange.Offset(i, 0).PasteSpecial xlPasteAll
    Next i

End Sub


EDIT: OK, something like this then (to cope with disjoint ranges):

编辑:好的,就像这样(以应对不相交的范围):

Sub Test()

    Dim oRange As Range
    Set oRange = ActiveSheet.Range("A1,C1:D1") ' Change this to point at the range to be copied

    Dim i As Integer
    For i = 1 To 10
        Dim oArea As Range
        For Each oArea In oRange.Areas
            oArea.Copy
            oArea.Offset(i, 0).PasteSpecial xlPasteAll
        Next oArea
    Next i

End Sub