2012-03-12 92 views
1

我是VBA的新手。 我已经将这段代码写在名为“Outputs”的表单中的按钮单击事件中。 问题是,当我尝试复制数据形式TestInput片计算器片我得到一个运行时错误1004
我的代码如下所示从Excel中的不同工作表中复制数据

Do While currentInd < lastRows 
    With Worksheets("TestInput") 
    Worksheets("Calculator").Range(Cells(3, "C"), Cells(3, (lastColumns))).Value = .Range(.Cells(currentInd, "A"), .Cells(currentInd, lastColumns)).Value 

    End With 

如果我更换的第3行我代码(“计算器”)。Range(“C3:FC3”)。Value = .Range(.Cells(currentInd,“A”),.Cells(currentInd,lastColumns))。Value

then罚款,但“lastColumns”是一个变量,将改变,所以我不想修复我的范围“C3:FC3”

回答

2

除非“计算器”是当前的活动表,你需要充分指定单元格的位置:

With Worksheets("TestInput") 
    Worksheets("Calculator").Range(Worksheets("Calculator").Cells(3, "C"), Worksheets("Calculator").Cells(3, lastColumns)).Value = .Range(.Cells(currentInd, "A"), .Cells(currentInd, lastColumns)).Value 
End With 

还是要让它多一点可读性:

Dim shCalc as WorkSheet 
Set shCalc = Worksheets("Calculator") 
With Worksheets("TestInput") 
    shCalc.Range(shCalc.Cells(3, "C"), shCalc.Cells(3, lastColumns)).Value = .Range(.Cells(currentInd, "A"), .Cells(currentInd, lastColumns)).Value 
End With 
+0

感谢你这么多:) – 2012-03-12 12:13:12

相关问题