Union not hardcoded

Hawkin

New Member
Joined
Jul 2, 2014
Messages
10
Hello,

I want to fill multiple Cells at once that are 5 columns off from each other, so I use

Code:
Union(Cells(2, i), Cells(2, i + 5), Cells(2, i + 10), ...) = "Text"

But the amount of cells is variable. Everytime we need a new set of columns, I need to change the code to implement them.

What I would like is to "fill" or enhance the union with all needed Cells with a For..Next loop, so that I never need to hardcode the cells anymore. Like saving the union range into a variable.

This approach shows the direction I want this to go

Code:
For i = 1 to lastcolumn Step 5[INDENT]UnionVariable = UnionVariable + Union(Cells(2, i))[/INDENT]
Next
'Now I have automatically gotten all cells and fill them at once
UnionVariable = "Text"

I have tried different methods with filling cells one by one, but unfortunately this is a performance killer as it took my code several seconds. With filling all cells at once using union it takes less than 1 second which is what I need.

Thanks in advance
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi.

I have put two ideas in here. Firstly, I have reformatted your original idea using a Range variable and concatenating the ranges in the way you suggested.

Secondly I have added : Application.ScreenUpdating = False/True.
This will stop Excel updating the fields as you go round your loop that updates the cells separately without using UNION. That may be all you need. Basically, it is a different way of updating all the cells at once. Whether it works will depend on whether the time was being consumed by Excel repeatedly updating the screen or by running round your loop.

Code:
Sub xxxx()
    Application.ScreenUpdating = False
    Dim UnionVariable  As Range
    With Worksheets("Sheet1")
        lastcolumn = 16
        Set UnionVariable = .Cells(2, 1)
        For i = 6 To lastcolumn Step 5
            Set UnionVariable = Union(UnionVariable, .Cells(2, i))
        Next
        'Now I have automatically gotten all cells and fill them at once
        UnionVariable = "Text"
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Oh I didn't know that it's really possible to set such a UnionVariable. Perfect Solution, thank you!

Note: Set'ing the UnionVariable before looping is obligatory for it to work.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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