简体   繁体   English

VBA循环复制列

[英]VBA Loop to Copy Columns

I want to copy a defined number (lets say 10) of rows from one sheet ("Data") and paste it in another sheet ("Input). This will cause a bunch of stuff to calculate. Then I want to copy said calculated data (6 rows) from ("Input") to ("Data") and paste in a results table. THen I would repeat this a defined number of times for a certain number of columns (lets say 10). 我想从一张工作表(“数据”)中复制定义的行数(比如说10行),然后将其粘贴到另一张工作表(“输入”中),这将导致大量计算工作,然后我要复制计算出的行数从(“输入”)到(“数据”)的数据(6行),然后粘贴到结果表中。然后,我将对特定数量的列(例如10)重复定义的次数。

I tried writing the code but it has literally been years since I have written code. 我尝试编写代码,但是从编写代码到现在已经有好几年了。

I used the Record Marco thing and got this: 我使用了Record Marco的东西并得到了这个:

Sub Macro2()
'
' Macro2 Macro
'

'
    Range("C5:C14").Select
    Selection.Copy
    Sheets("Input").Select
    Range("C5").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("P12:P19").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Data").Select
    Range("C22").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("D5:D14").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Input").Select
    Range("C5").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("P12:P19").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Data").Select
    Range("D22").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("G16").Select
End Sub

I hope this makes sense 我希望这是有道理的

Sub Macro2()

    Const NUM_TIMES As Long = 10
    Dim shtInput As Worksheet, shtData As Worksheet
    Dim rngCopy As Range, i As Long

    Set shtInput = Sheets("Input")
    Set shtData = Sheets("Data")

    Set rngCopy = shtData.Range("C5:C15")

    For i = 1 To NUM_TIMES

        With shtInput
            .Range("C5").Resize(rngCopy.Rows.Count, 1).Value = rngCopy.Value
            .Calculate
            rngCopy(1).Offset(17, 0).Resize(8, 1).Value = .Range("P12:P19").Value
        End With

        Set rngCopy = rngCopy.Offset(0, 1)
    Next i

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM