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

时间:2022-12-03 02:22:28

I am trying to build a loop to copy and paste a range of cells from one excel worksheet to another a number of times, as specified in a cell A1.

我正在尝试构建一个循环,将一系列单元格从一个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

I understand that during the iteration, the paste does not increment the cell value and paste it in the next one down.

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

Help! What's wrong with my code?

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

2 个解决方案

#1


2  

Try this, as per comments above.

根据上面的评论,试试这个。

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

#2


1  

You don't need a for loop, rather you can try it like this...

你不需要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

#1


2  

Try this, as per comments above.

根据上面的评论,试试这个。

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

#2


1  

You don't need a for loop, rather you can try it like this...

你不需要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