我是VBA的新手。 我已将此代码写入名为“输出”的工作表中的按钮点击事件。
问题是当我尝试将数据从 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
如果我用工作表(“计算器”)替换我的第三行代码。范围(“C3:FC3”)。值= .Range(.Cells(currentInd,“A”),. Cell(currentInd,lastColumns) )。价值
然后它的工作正常,但“lastColumns”是一个会改变的变量,所以我不想把我的范围修改为“C3:FC3”
答案 0 :(得分:2)
除非&#34;计算器&#34;是当前活动工作表,您需要完全指定单元格的位置:
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