+ Reply to Thread
Results 1 to 16 of 16

Array looping, increase speed of array macro

Hybrid View

  1. #1
    Registered User
    Join Date
    11-22-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Array looping, increase speed of array macro

    I have two sheets, the array macro below detects the last row, then indexes column D, then matches data from column A on Sheet1 to column A on Sheet2 and matches column B on sheet 1 to column E on Sheet2. Then it returns the corresponding value from the indexed column to column G on Sheet1 if the value exists. The macro works fine however I am not advanced enough to make it work faster. Sheet2 contains over 270K rows to search through and it takes quite a long time with 50k rows on Sheet1. Is there a way to make this loop through the records to speed up the process? or in general make the returns faster?
    Sub Macro4()
    
    Range("G2").Select
    Dim Lastrow As Long
    Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    Selection.FormulaArray = _
    "=INDEX(Sheet2!R8C4:R280007C4, MATCH(1,(RC[-6]=Sheet2!R8C1:R280007C1)*(RC[-5]=Sheet2!R8C5:R280007C5),0))"
    Selection.AutoFill Destination:=Range("G2:G" & Lastrow), Type:=xlFillDefault
    Range("G2:G280007").Select
    
    End Sub
    Last edited by Leith Ross; 11-22-2013 at 01:41 PM. Reason: Added Code Tags

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Array looping, increase speed of array macro

    You could do it like this:
     Range("G2:G" & Range("G" & rows.count).end(xlup).row).formulaarray = _
    "=INDEX(Sheet2!R8C4:R" & Range("D" & rows.count).end(xlup).row & "C4, MATCH(1,(RC[-6]=Sheet2!R8C1:R" & Range("A" & rows.count).end(xlup).row & "C1)*(RC[-5]=Sheet2!R8C5:R2" & Range("E" & rows.count).end(xlup).row & "C5),0))"
    ?

  3. #3
    Registered User
    Join Date
    11-22-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Array looping, increase speed of array macro

    Thanks Yudlugar but this did not work.

    I may not be clear in my statement, The macro works, I need it to loop or in some form work faster without brining my CPU useage to 100%. The macro you provided replaces my header (G1) and only returns the same value that was placed in my header in (G2). If I can provide more information please let me know.

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Array looping, increase speed of array macro

    I'm not sure if there is a quicker way than the worksheet fucntions. You could maybe try something along the lines of this, based on using find to loop through the data but dependant on what your data is like it might not be any better.

    Sub macro4_1()
    Dim lrow, count, counta
    application.screenupdating = false
    lrow = Range("A" & Rows.count).End(xlUp).Row
    For count = 2 To lrow
        Set counta = Sheets("Sheet2").Range("A:A").Find(Range("a" & count))
        Do Until counta Is Nothing
            If Sheets("Sheet2").Range("E" & counta.Row) = Range("B" & count) Then
                Range("G" & count) = Sheets("Sheet2").Range("D" & counta.Row)
            End If
            Set counta = FindNext(counta)
        Loop
    Next
    application.screenupdating = true
    End Sub

  5. #5
    Registered User
    Join Date
    11-22-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Array looping, increase speed of array macro

    Thanks again Yudlugar,

    Can you show me how to put that in with my code?

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Array looping, increase speed of array macro

    It was to replace your code.

  7. #7
    Registered User
    Join Date
    11-22-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Array looping, increase speed of array macro

    I receive a compile error on this line, says "Sub or Function not defined"


    Set counta = FindNext(counta)

    Error on FindNext

  8. #8
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Array looping, increase speed of array macro

    try changing it to:
    set counta = Sheets("Sheet2").Range("A:A").findnext(counta)

  9. #9
    Registered User
    Join Date
    11-22-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Array looping, increase speed of array macro

    I really appreciate all your work, however this code actually returns a Run-time error2013-11-25_0948.png

  10. #10
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Array looping, increase speed of array macro

    Can you upload a sample workbook please.

    Click go advanced and then "manage attachments"

  11. #11
    Registered User
    Join Date
    11-22-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Array looping, increase speed of array macro

    I hope this works, I had to knock down the size and had to remove modules and formatting. Also there should be an empty column on sheet 1 between E and F.
    Attached Files Attached Files
    Last edited by techrcn; 11-25-2013 at 12:28 PM.

  12. #12
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Array looping, increase speed of array macro

    Sub macro4_1()
    Dim lrow, count, counta, ws1, ws2
    Set ws1 = Sheets("Sheet1")
    Set ws2 = Sheets("Sheet2")
    Application.ScreenUpdating = False
    lrow = ws1.Range("A" & Rows.count).End(xlUp).Row
    For count = 2 To lrow
        Set counta = ws2.Range("A:A").Find(ws1.Range("a" & count))
        Do Until counta Is Nothing
            If Sheets("Sheet2").Range("E" & counta.Row) = ws1.Range("B" & count) Then
                ws1.Range("G" & count) = Sheets("Sheet2").Range("D" & counta.Row)
                Exit Do
            End If
            On Error GoTo next_cell
            Set counta = ws2.Range("A:A").FindNext(counta)
            On Error GoTo 0
        Loop
    next_cell:
    Next
    Application.ScreenUpdating = True
    End Sub
    That seems to work on your example.

  13. #13
    Registered User
    Join Date
    11-22-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Array looping, increase speed of array macro

    At a large scale it seems to work a little faster, its not eating up the processor as much as the old one did. Thanks again, I'll see what I can manage.

  14. #14
    Registered User
    Join Date
    11-22-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Array looping, increase speed of array macro

    Yudlugar,

    It seems to lock up if data in sheet 1 is not found on sheet 2. Is there an else statement I can put in there?
    Last edited by techrcn; 11-26-2013 at 09:35 AM.

  15. #15
    Registered User
    Join Date
    11-22-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Array looping, increase speed of array macro

    Yudlugar,

    It seems to lock up if data in sheet 1 is not found on sheet 2. Is there an else statement I can put in there?

  16. #16
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Array looping, increase speed of array macro

    You could maybe try putting a counter in and then exiting if it has searched more than the count, for example
    Sub macro4_1()
    Dim lrow, count, counta, ws1, ws2,i
    Set ws1 = Sheets("Sheet1")
    Set ws2 = Sheets("Sheet2")
    Application.ScreenUpdating = False
    lrow = ws1.Range("A" & Rows.count).End(xlUp).Row
    For count = 2 To lrow
        i = 1
        Set counta = ws2.Range("A:A").Find(ws1.Range("a" & count))
        Do Until counta Is Nothing or i >500
            If Sheets("Sheet2").Range("E" & counta.Row) = ws1.Range("B" & count) Then
                ws1.Range("G" & count) = Sheets("Sheet2").Range("D" & counta.Row)
                Exit Do
            End If
            On Error GoTo next_cell
            Set counta = ws2.Range("A:A").FindNext(counta)
            On Error GoTo 0
            i = i+1
        Loop
    next_cell:
    Next
    Application.ScreenUpdating = True
    End Sub
    change i>500 as neccessary

+ 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. speed up sum if array formula with VBA??
    By jed38 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-30-2013, 01:32 PM
  2. How to speed up this macro? How to use an array?
    By djvino in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-15-2012, 07:39 AM
  3. Speed up max if array
    By reddwarf in forum Excel General
    Replies: 9
    Last Post: 03-02-2011, 08:20 AM
  4. Will an Array speed this up?
    By Mase in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-04-2005, 06:53 AM
  5. [SOLVED] Increase macro speed?
    By Valeria in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-14-2005, 05:06 AM

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