Make Loop Faster / Loop Returns a Multiple Selection

zzucc

New Member
Joined
Aug 10, 2015
Messages
1
Hi, I couldnt find in the internet a loop that returns a multiple selection.

Private X0, Y0, Lx, Ly, Atotal As Integer
'X0 = Position of the first cell in the first machine on Monday
'X0 = Position of the first cell in the first machine on Monday
'Lx = Widht, in cells, of one day
'Ly = Widht, in cells, of a machine (Anlage)
'Atotal = Total number of "Anlagen"

I am trying that, because I noticed that a macro runs faster if it is executed on a manual multiple range, like:

Sub Spalte()
Range("D:D,N:N,X:X,AH:AH,AR:AR,BB:BB,BL:BL").ColumnWidth = Range("$B$3")
Rows("4:167").EntireRow.AutoFit
End Sub

...faster than a automatic range produced by a loop:

Sub SpalteLoop()
Dim t As Integer
For t = 1 To 7
cells(Y0, X0 + Lx * (t - 1)).ColumnWidth = Range("$B$3")
Next t
Rows("4:167").EntireRow.AutoFit
With ActiveSheet
End Sub

Thus, Im trying to find a way that makes the loop returns a multiple selection, so I can execute the macro on this selection, which would be faster. I noticed that the .Select loop runs faster than the .ClearContents loop. I also using the same automation to clear contents, and it was also faster when it was manual.
-> Makes sense I try to make a loop that returns a multiple selection? How can I do that?

P.S.: I Created the autofit part because I wasnt able to make the cells that contains formulas autofit. My Excel just dont respect it for cells that contains formulas. If it was exectued in every change in the sheet, it would make the workbook slow.

Thanks for the attention!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi and welcome to the MrExcel Message Board.

I think you are looking for something like this:
Code:
Sub SpalteLoop()

    Dim t As Long
    Dim r As Range
    
    Const X0 As Long = 1
    Const Y0 As Long = 2
    Const Lx As Long = 3

    Set r = Cells(Y0, X0)
    For t = 2 To 7
        Set r = Union(Cells(Y0, X0 + Lx * (t - 1)), r)
    Next t
    
    r.ColumnWidth = Range("$B$3")
    
    Rows("4:167").EntireRow.AutoFit

End Sub
It sets range r to be equal to the first range then after that it adds the new range to the list so far by using the Union function.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

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