简体   繁体   English

VBA复制和粘贴循环

[英]VBA COPY AND PASTE IN A LOOP

I am new to VBA and need some help with this problem. 我是VBA的新手,并且需要一些有关此问题的帮助。

I have 8 columns and each column has 9 rows. 我有8列,每列有9行。 I want to copy each column and paste it to another sheet at a specific range. 我想复制每列并将其粘贴到特定范围的另一张纸上。

How can i code the following statement? 我如何编码以下语句? "Go to sheet2 and copy the first column and 2-9 rows and paste it to sheet1 in the range("C3:C10") and then press solver do some other staff and when all these are done go to the next column and do the same again". “转到sheet2并复制第一列和2-9行,然后将其粘贴到范围(“ C3:C10”)中的sheet1中,然后按求解器执行其他一些操作,完成所有这些操作后转到下一列并执行再次一样”。

I found how can I do this for one column but I can't find how to do the loop. 我发现如何对一列进行此操作,但找不到循环。 Here is the code I've wrote for the first column. 这是我为第一列编写的代码。 I want to do the same for all 8 columns. 我想对所有8列都做同样的事情。

Sub test1()

Worksheets("sheet 1").Range("c3:c10").Value = Worksheets("Sheet 2").Range("A2:A9").Value
Worksheets("Sheet 1").Range("C17").Value = "Y"
Worksheets("Sheet 1").Range("C32").Value = "Y"
        SolverOk SetCell:="$E$96", MaxMinVal:=3, ValueOf:=Range("C104").Value, 
            ByChange:="$C$100", _
            Engine:=1, EngineDesc:="GRG Nonlinear"
        SolverSolve True

Worksheets("Sheet 3").Range("J2").Value = Worksheets("Sheet 1").Range("L24").Value

End Sub

Instead of using Range(""), I would loop between columns using .Cells(row, col), for example: 而不是使用Range(“”),我将使用.Cells(row,col)在列之间循环,例如:

Sub Iteration()

    For c = 1 To 9
        Sheets("Sheet1").Cells(1, c).EntireColumn.Copy
        Sheets("Sheet2").Cells(1, c).PasteSpecial
    Next

End Sub

This code is copying every row between columns 1 and 9 from Sheet1 to Sheet2, by iterating through "Cells" second parameter, that references a column. 该代码通过遍历引用一列的“ Cells”第二个参数,将第1列和第9列之间的每一行从Sheet1复制到Sheet2。

And the code can do this because of the "For" loop, which keeps repeating until the condition is reached. 并且由于“ For”循环,代码可以执行此操作,该循环不断重复直到达到条件为止。 On my example, I set "c" to 1 and asked to repeat the codeinside the "For - Next" statement until c reaches 9, adding 1 to the variable by default. 在我的示例中,我将“ c”设置为1,并要求在“ For-Next”语句中重复执行代码,直到c达到9,默认情况下将1加到变量中。

You could do it thus. 您可以这样做。 If other things are variable can similarly adjust using i perhaps. 如果其他事情是可变的,也许可以使用i进行类似的调整。

Sub test1()

Dim i As Long

For i = 1 To 8
    Worksheets("sheet 1").Range("c3:c10").Value = Worksheets("Sheet 2").Range("A2:A9").Offset(, i - 1).Value
    Worksheets("Sheet 1").Range("C17").Value = "Y"
    Worksheets("Sheet 1").Range("C32").Value = "Y"
    SolverOk SetCell:="$E$96", MaxMinVal:=3, ValueOf:=Range("C104").Value, _
             ByChange:="$C$100", Engine:=1, EngineDesc:="GRG Nonlinear"
    solverSolve True
    Worksheets("Sheet 3").Range("J2").Value = Worksheets("Sheet 1").Range("L24").Value
Next i

End Sub

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

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