多列宏

时间:2014-03-06 15:30:06

标签: excel vba

我有很多列必须粘贴到另一个标签中才能执行计算。宏从列CH获取数据并将其粘贴到计算表中。在生产选项卡上抓取计算字段并粘贴到CH172下方。我想为后续列(CI,CJ等)执行此例程

Sub MonthlyCost()
    Range("CH147:CH172").Select
    Selection.Copy
    Sheets("1.2.1.1 Calculation").Select
    Range("J10").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveWindow.SmallScroll Down:=9
    Range("W63").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("1.2.1.1 Production").Select
    Range("CH174").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub

1 个答案:

答案 0 :(得分:0)

在我的结尾处进行了简单的测试和运行,没有错误。我很确定这会做你想要的。如果没有,请告诉我,我会再次尝试:

Sub MonthlyCost()

Dim columnStart As Long
Dim columnsToCopy As Long
Dim columnEnd As Long
Dim i As Long

columnsToCopy = 5

columnStart = 86 ' this is column CH
columnEnd = columnStart + columnsToCopy - 1 ' this will loop from column CH {column 1 of the columnsToCopy} to column CL {column 5 of the columnsToCopy}


For i = columnStart To columnEnd
    Range(Sheets("1.2.1.1 Production").Cells(147, i), Sheets("1.2.1.1 Production").Cells(172, i)).Copy
    Sheets("1.2.1.1 Calculation").Range("J10").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("1.2.1.1 Calculation").Range("W63").Copy
    Sheets("1.2.1.1 Production").Cells(174, i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
Next i

End Sub

这可以用一些使用语句或将工作表设置为变量来清理,但这应该可以让你开始。

祝你好运,让我知道结果如何!