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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,215,529
Messages
6,125,345
Members
449,220
Latest member
Edwin_SVRZ

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