I'm trying to define a Range using VBA in Excel,
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
The first range origem is correctly defined without errors, but the second destino is throwing the error:
Object with block variable not set
On line:
destino = Sheets(separador).Range(Cells(10, 4), Cells(11, 5))
Can someone please explain me why, and how to fix this?
Thanks in advance!
setinfront? Alsoorigemis declared as variable type variant fyi.Dim linhaInicial, linhaFinal, colunaInicial, colunaFinal, numAnos As Integerdoes not declare all those variables asInteger. OnlynumAnosis declared as aninteger. You have to put theas Integerafter each variable name. Without it, they'll bevarianttypes.Range(cells())reference, you need to be explicit and make sure you tell each part of that (both theRange()andCells()) what worksheet you're working with. I.e.destino = Sheets(separador).Range(Sheets(separador).Cells(10, 4), Sheets(separador).Cells(11, 5)).Addressproperty (string). You can try this and confirm:set rng = Range(Sheet1.Cells(1,1).Address, Sheet2.Cells(1,3).Address)will not throw an error, because the.Addressproperty resolves to a valid string irrespective of the sheet. You can then use the address string to define the range on any sheet.