Excel VBA:无法定义范围,获取块variables未设置的错误对象
我想在Excel中使用VBA定义一个Range,
Sub convertePerc() Dim separador As String Dim linhaInicial, linhaFinal, colunaInicial, colunaFinal, numAnos As Integer Dim origem, destino As Range separador = "DRES(G)" colunaFinal = Sheets(separador).Cells(6, 5).End(xlToRight).Column linhaFinal = 40 numAnos = 10 origem = Sheets(separador).Range(Cells(10, 4), Cells(linhaFinal, colunaFinal)) colunaInicial = CInt(4 + numAnos + 1) colunaFinal = CInt(numAnos + colunaFinal + 1) destino = Sheets(separador).Range(Cells(10, 4), Cells(11, 5)) End Sub
第一个范围origem
正确定义没有错误,但第二个destino
正在抛出错误:
Object with block variable not set
在线:
destino = Sheets(separador).Range(Cells(10, 4), Cells(11, 5))
有人可以解释我为什么,以及如何解决这个问题?
提前致谢!
范围是对象variables,对象variables需要Set。 此外,范围或单元格的非限定引用是活动工作表。 所以
With Sheets(separador) Set destino = Range(.Cells(10, 4), .Cells(11, 5)) End With