Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Excel中的其他工作表复制数据_Excel_Vba - Fatal编程技术网

从Excel中的其他工作表复制数据

从Excel中的其他工作表复制数据,excel,vba,Excel,Vba,我是VBA新手我在一个名为“输出”的表单中的按钮点击事件中编写了这段代码。 问题是,当我尝试将数据表单测试输入表复制到计算器表时,我得到一个运行时错误1004。 我的代码如下所示 Do While currentInd < lastRows With Worksheets("TestInput") Worksheets("Calculator").Range(Cells(3, "C"), Cells(3, (lastColumns))).Value = .Range(.Ce

我是VBA新手我在一个名为“输出”的表单中的按钮点击事件中编写了这段代码。 问题是,当我尝试将数据表单测试输入表复制到计算器表时,我得到一个运行时错误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
Do而currentInd
如果我用工作表(“计算器”).Range(“C3:FC3”).Value=.Range(.Cells(currentInd,“A”),.Cells(currentInd,lastColumns)).Value替换代码的第三行

然后它工作正常,但“lastColumns”是一个会发生变化的变量,因此我不想将范围固定到“C3:FC3”

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

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