Updating Multiple Range

daniel22bg

New Member
Joined
Nov 21, 2008
Messages
31
Hi there,

I need to have e multiple range that is updating itself with each spinning of the loop. Something like this (skipping the unimportant parts):

Code:
Dim UnionRng, SlectRng as Range
 
Set UnionRng = Nothing
     
For j = 1 To 50
     SelectCol = j + 20
     Set SelectRng = Range(Cells(2, SelectCol), Cells(100, SelectCol))
     Set UnionRng = Union(UnionRng, SelectRng)
Next j

I have a problem with this part "Set UnionRng = Union(UnionRng, SelectRng)". I know that this is possible im MATLAB but it seems that VBA is not supporting such functionality. Is there any way that I can get around it? Thanks :)
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You can't use union on a range that doesn't already exist, so first we have to set it before attempting a union.

Code:
[COLOR=blue]Sub[/COLOR] test()
 
[COLOR=blue]Dim[/COLOR] UnionRng, SelectRng [COLOR=blue]As[/COLOR] Range
 
[COLOR=blue]Set[/COLOR] UnionRng = [COLOR=blue]Nothing[/COLOR]
 
[COLOR=blue]For[/COLOR] j = 1 [COLOR=blue]To[/COLOR] 50
     SelectCol = j + 20
     [COLOR=blue]Set[/COLOR] SelectRng = Range(Cells(2, SelectCol), Cells(100, SelectCol))
     [COLOR=blue]If[/COLOR] UnionRng [COLOR=blue]Is[/COLOR] [COLOR=blue]Nothing[/COLOR] [COLOR=blue]Then[/COLOR]
        [COLOR=blue]Set[/COLOR] UnionRng = SelectRng
    [COLOR=blue]Else[/COLOR]
        [COLOR=blue]Set[/COLOR] UnionRng = Union(UnionRng, SelectRng)
     [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
[COLOR=blue]Next[/COLOR] j
 
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,205
Members
448,874
Latest member
Lancelots

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