Range with variable columns

GrayMPA

Board Regular
Joined
Dec 10, 2003
Messages
95
I can use ranges for variable rows, but cannot figure out how to use variable columns.

For variable rows, I use:

lastrow = Range("b65536").End(xlUp).Row
Range("B11:H" & lastrow).Select

I am trying to set the data for a graph using a variable number of columns
using this form:

lastcol = Cells(2, 256).End(xlToLeft).Column

but this errors out:
mydataRange = Range(Cells(2, 22), Cells(3, lastcol))

I have tried Set command, no luck.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hello,

You definitely want to use Set, the following works for me:

Code:
Sub foo()
Dim myDataRange As Range
Dim lastCol As Long
lastCol = Cells(2, 256).End(xlToLeft).Column
Set myDataRange = Range(Cells(2, 22), Cells(3, lastCol))
MsgBox myDataRange.Address(0, 0)
End Sub
Yes-no? :)
 
Upvote 0
Thanks vry much

I had the right idea, just had a "." instead of a ",". Couldn't find it until I put yours text up next to mine.
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,212
Members
449,074
Latest member
cancansova

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