从Excel中的其他工作表复制数据
我是VBA新手我在一个名为“输出”的表单中的按钮点击事件中编写了这段代码。 问题是,当我尝试将数据表单测试输入表复制到计算器表时,我得到一个运行时错误1004。从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
我的代码如下所示
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