复制粘贴循环多次作为单元格值 - VBA

时间:2018-01-05 17:35:37

标签: excel vba excel-vba

我正在尝试构建一个循环,将一系列单元格从一个excel工作表复制并粘贴到另一个excel工作表中,如单元格A1中所指定的那样。

    Sub AnalogPointBuild()

                Dim i As Integer
                Dim y As Integer

                'only copy the number of times as the count in cell A1 of worksheet "inputs"
                Sheets("inputs").Activate
                y = Range("A1").Value

                ' copy the range of cells from one worksheet "Analog Template' to the "Analog Output"        

                For i = 1 To y
                Worksheets("Analog Template").Range("B3:EU3").Copy Worksheets("Analog Output").Range("B3:EU3")
                Next i

    End Sub

据我所知,在迭代过程中,粘贴不会增加单元格值并将其粘贴到下一个单元格中。

帮助!我的代码出了什么问题?

2 个答案:

答案 0 :(得分:2)

根据上述评论,试试这个。

Sub AnalogPointBuild()

Dim y As Long

'only copy the number of times as the count in cell A1 of worksheet "inputs"
y = Sheets("inputs").Range("A1").Value

' copy the range of cells from one worksheet "Analog Template' to the "Analog Output"
Worksheets("Analog Output").Range("B3:EU3").Resize(y).Value = Worksheets("Analog Template").Range("B3:EU3").Value

End Sub

答案 1 :(得分:1)

你不需要for循环,而是你可以这样试试......

Sub AnalogPointBuild()
    Dim y As Integer

    'only copy the number of times as the count in cell A1 of worksheet "inputs"
    y = Sheets("inputs").Range("A1").Value

    ' copy the range of cells from one worksheet "Analog Template' to the "Analog Output"

    Worksheets("Analog Template").Range("B3:EU3").Copy Worksheets("Analog Output").Range("B3:EU3").Resize(y)

End Sub