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

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
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
 
Upvote 0
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!!)
 
Upvote 0
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
 
Upvote 0
Try this

Code:
Dim Arrcols As Range
Set Arrcols = Range("c6", Range("C6").End(xlDown).End(xlToRight))
 
Upvote 0
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?
 
Upvote 0
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)
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

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