ForumExcel
New Member
- Joined
- Jan 17, 2017
- Messages
- 21
Excel 2010:
Following code works if I pass a rectangular range but fails if I attempt to pass an entire column; I get runtime error 1004 (Application-defined or object-defined error) in the following code.
Thank you!
Following code works if I pass a rectangular range but fails if I attempt to pass an entire column; I get runtime error 1004 (Application-defined or object-defined error) in the following code.
Code:
Function GetRangeWithoutFirstNRows(inRange As Range, numRows As Integer) As Range
Dim rowCount As Long
Dim colCount As Long
Dim myRange As Range
rowCount = inRange.Rows.Count
colCount = inRange.Columns.Count
' Note that an object has to always be "Set". Even a Function name if it returns an object
Set myRange = inRange.Offset(numRows).Resize(rowCount - numRows, colCount) ' <--- Error line
Set GetRangeWithoutFirstNRows = myRange
End Function
Sub TestGetRangeWithoutFirstNRows()
Dim myRange As Range
Set myRange = GetRangeWithoutFirstNRows(ActiveSheet.Columns(1), 1) ' not working
PrintRangeAddress myRange
End Sub
Sub PrintRangeAddress(myRange As Range)
Debug.Print myRange.address
End Sub
Thank you!