Help needed in VBA, defining a variable range from excel

centauri

New Member
Joined
Jul 18, 2010
Messages
6
Was wondering if someone could help me with defining a range from data in excel.

I have a lot of data in excel that varies both by rows and columns. I need to define this array in VBA, but want it to change if more columns or rows are added e.g array may be from C6:k100 but then change to c6:z250, but don't know where to start. If I know the data will always start in c6 how can I write some code to pick up the correct size of the array?

Any help appreciated
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Perhaps

Code:
Sub test()
Dim LR As Long, LC As Long, MyRange As Range
With ActiveSheet
    LR = .UsedRange.Find(what:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    LC = .UsedRange.Find(what:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
    Set MyRange = .Range(.Cells(6, 3), .Cells(LR, LC))
End With
End Sub
 

centauri

New Member
Joined
Jul 18, 2010
Messages
6
I've used your suggestion but get an error "object variable or with variable not set".

here is the code I've used

Dim Arrcols As Range
Arrcols = Range("c6", Range("C6").End(xlDown).End(xlToRight)).Select

have I done something wrong (new to vba!!)
 

jim may

Well-known Member
Joined
Jul 4, 2004
Messages
7,484

ADVERTISEMENT

If the cells to the right and downward from C6 are always form a contiguous conected block of cells, perhaps then

Set Rng = Range("C6").CurrentRegion
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try this

Code:
Dim Arrcols As Range
Set Arrcols = Range("c6", Range("C6").End(xlDown).End(xlToRight))
 

centauri

New Member
Joined
Jul 18, 2010
Messages
6

ADVERTISEMENT

cheers guys,

just one quick question, how do I find out what the array size is now? e.g. I tried "msgbox arrcols " and was hoping to get something along the lines of arrcols(C6,m100). Can I get something like this to make sure it is working?
 

centauri

New Member
Joined
Jul 18, 2010
Messages
6
Fantastic! I've been trying to do this for the last 4 hours, Mr excel is the bee's knees! Thanks a lot for all your help. (will add Mr excel as a fav)
 

Watch MrExcel Video

Forum statistics

Threads
1,129,352
Messages
5,635,780
Members
416,880
Latest member
kyleswanson

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
Top