How to resize a whole column?

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.

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!
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
You can't offset by any number of rows an entire column so you can't do that first. Also both parameters for Resize are not required. Since you aren't changing the column count, I would just leave that part out completely. There is also no need for all those variables that you only use the value once. Try this for your function:
Code:
Function GetRangeWithoutFirstNRows(inRange As Range, numRows As Integer) As Range
  ' Note that an object has to always be "Set". Even a Function name if it returns an object
  Set GetRangeWithoutFirstNRows = inRange.Resize(inRange.Rows.Count - numRows).Offset(numRows) ' <--- Error line
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top