I'm trying to define a dynamic range in VBA for some operations I have to do with a matrix, as follows:
Dim Range1 As Range, Range2 As Range
'...
'define some indexes
'...
Set Range1 = Sheets("Sheet1").Range(Cells(rowFirst, colFirst), Cells(rowEnd,colEnd))
But I'm getting the runtime error #1004, and I don't know why and how to fix it.
I'm new to VBA so I was expecting you could help me.
Thanks to all!
Note: The rowFirst,colFirst,rowEnd,colEnd variables are constantly changing since I need to be moving in the worksheet from matrix to matrix.
Edit
Thank you all for your responses, but I'm still getting the same error on the same line. Let me explain a little bit deeper what's what I'm trying to do. I need to take a 13x91(in cells) matrix and transpose it, then I need to join all in one single column, and put the resultant column in the Sheet 2. This done 20 times advancing in columns (and not in rows). Here I leave the code:
Sub ConvertRangeToColumn()
Dim it As Integer
Dim rowInit As Integer, rowEnd As Integer, colInit As Integer, colEnd As Integer
rowInit = 1
rowEnd = 91
colInit = 1
colInit = 13
For it = 1 To 20
Dim Range1 As Range, Range2 As Range, Rng As Range
Dim rowIndex As Integer
xTitleId = "Automatización"
Set Range1 = Hoja1.Range(Hoja1.Cells(rowInit, colInit), Hoja1.Cells(rowEnd, colEnd))
Set Range2 = Sheets("Hoja2").Range(Cells(1, it))
colInit = colInit + 13
colEnd = colEnd + 13
rowIndex = 0
Application.ScreenUpdating = False
For Each Rng In Range1.Rows
Rng.Copy
Range2.Offset(rowIndex, 0).PasteSpecial Paste:=xlPasteAll, Transpose:=True
rowIndex = rowIndex + Rng.Columns.Count
Next
Application.CutCopyMode = False
Application.ScreenUpdating = True
Next it
End Sub
Note 2: Since I work in Excel in Spanish, the Hoja1 object means Sheet1
Sheets("Sheet1").Range(Sheets("Sheet1").Cells(rowFirst, colFirst), Sheets("Sheet1").Cells(rowEnd,colEnd))Assign Parentage to ALL range objects.Hoja1should beWorksheets("Hoja1")