簡體   English   中英

復制粘貼循環多次作為單元格值-VBA

[英]Copy paste loop a number of times as the cell value - VBA

我正在嘗試建立一個循環,以將一個單元格工作表中的一系列單元格復制並粘貼多次,如單元格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

我知道在迭代過程中,粘貼不會增加單元格的值,也不會粘貼到下一個單元格中。

救命! 我的代碼有什么問題?

按照上面的評論嘗試這個。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM