在Excel中复制另一个工作表中的数据

我是VBA新手。 我已经把这个代码写在名为“Outputs”的表单中的button点击事件中。 问题是当我尝试复制数据表单TestInput表单计算器表我得到一个运行时错误1004。
我的代码如下所示

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 

如果我用工作表(“计算器”)replace我的代码的第三行。范围(“C3:FC3”)。值= .Range(.Cells(currentInd,“A”),.Cells(currentInd,lastColumns))。

那么它的工作正常,但“lastColumns”是一个variables,这将改变,所以我不想修复我的范围“C3:FC3”

除非“计算器”是当前活动工作表,否则您需要完全指定单元格的位置:

 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