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!
 

RickXL

MrExcel MVP
Joined
Sep 9, 2013
Messages
4,314
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.
 

Forum statistics

Threads
1,082,257
Messages
5,364,074
Members
400,778
Latest member
Canadian Sal

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top