+ Reply to Thread
Results 1 to 6 of 6

Running Loop in an Array

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    Running Loop in an Array

    I currently use a lot of loops in my macros. Someone recently introduced me to arrays and they seem to be much faster at running through the data. However, when an action needs to be taken, it has to access the sheet again and this seems to be just as slow. Is there a way to run through the array and save all the necessary changes? Then execute all the changes in one step at the end? Here is the code.

    Sub Test()
    
    rowsend = Range("A" & Rows.Count).End(xlUp).Row
    
    varsheet = Range(Cells(1, 1), Cells(rowsend, 1))
    
    For i = 1 To rowsend
    
    If varsheet(i, 1) = "I'm Awesome" Then
    Cells(i, 1).Interior.Color = 1234545
    End If
    
    Next
    
    
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Running Loop in an Array

    Your array is correct, but you can not use or test properties in arrays, hence you used.

    Cells(i, 1).Interior.Color = 1234545
    Arrays live in memory, but you are testing property of a range, not array, if you do, you will get an error.

  3. #3
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Running Loop in an Array

    Hi Jiptastic,

    However, when an action needs to be taken, it has to access the sheet again and this seems to be just as slow.
    Although that's true, you probably only need to access the sheet a relatively few times - so by passing the array the processing will still be much quicker. You can disable the screen updating to save most of that access time:

    Sub JipTest()
    
    rowsend = Range("A" & Rows.count).End(xlUp).row
    
    Application.ScreenUpdating = False
    
    varsheet = Range(Cells(1, 1), Cells(rowsend, 1))
    
    For i = 1 To rowsend
    
    If varsheet(i, 1) = "I'm Awesome" Then
    Cells(i, 1).Interior.Color = 1234545
    End If
    
    Next
    
    
    Application.ScreenUpdating = True
    End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,086

    Re: Running Loop in an Array

    Perhaps next code
    Sub Test2()
        rowsend = Range("A" & Rows.Count).End(xlUp).Row
        varsheet = Range(Cells(1, 1), Cells(rowsend, 1))
        Dim WkRg  As Range
        For i = 1 To rowsend
            If varsheet(i, 1) = "I'm Awesome" Then
                If WkRg Is Nothing Then
                    Set WkRg = Cells(i, 1)
                Else
                    Set WkRg = Union(WkRg, Cells(i, 1))
                End If
            End If
        Next
        WkRg.Cells.Interior.Color = 1234545
    End Sub

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Running Loop in an Array

    Your code could be improved to mix array and range.

    Sub Test()
    
    rowsend = Range("A" & Rows.Count).End(xlUp).Row
    
    varsheet = Range(Cells(1, 1), Cells(rowsend, 1))
    
    For i = 1 To UBound(varsheet)
    
    If varsheet(i, 1) = "I'm Awesome" Then
    Cells(i, 1).Interior.Color = 1234545
    End If
    
    Next
    
    
    End Sub

  6. #6
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    Re: Running Loop in an Array

    PCI this seems to be my answer. Setting screenupdating is also a good suggestion. Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Running a For Loop within another For Loop
    By ATLGator in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 01-22-2014, 12:59 PM
  2. Loop new messages containing a table, populate a dynamic array, paste array to Excel
    By laripa in forum Outlook Programming / VBA / Macros
    Replies: 1
    Last Post: 05-19-2013, 07:20 AM
  3. Do loop not running
    By hattrick_123a in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-21-2011, 08:36 PM
  4. Loop not running
    By T De Villiers in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-12-2006, 10:26 AM
  5. [SOLVED] Loop running really slow...?
    By Buffyslay in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-26-2006, 09:40 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1