+ Reply to Thread
Results 1 to 2 of 2

Using array to make 'for' loop more efficient

Hybrid View

  1. #1
    Registered User
    Join Date
    08-31-2012
    Location
    HK
    MS-Off Ver
    Excel 2003
    Posts
    14

    Using array to make 'for' loop more efficient

    hi all!

    i have a workbook (redacted) attached that:

    1. takes user input on Sheets("Input")

    BK = Range("B1").Value
    ST = Range("B2").Value
    2. takes list of trades from Sheets("Data")

    3. pastes list on Sheets("Output") then loops through and deletes irrelevant trades (deleting rows where column A != BK OR left(3) of column C != ST)

    Worksheets("Output").Activate
        Set SRng = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
    
        For i = SRng.Rows.Count To 1 Step -1
            If Cells(i, 1) <> BK Or Left(Cells(i, 3), 3) <> ST Then Rows(i).Delete
        Next i
    so we only want to be left with trades that match both BK and ST

    there are typically ~62k rows of trades and the loop/delete row process is very slow...i've read about ways to increase the efficiency of this process using an array but don't quite understand how to execute this. any pointers?

    cheers

    sampleSS.xlsx

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Using array to make 'for' loop more efficient

    Hi,

    Perhaps you could do this by filtering the "Data" sheet before copying and pasting..

    Sub Macro1()
    Dim BK As Variant, ST As Variant
    Dim rFilterRange As Range
    
    With Sheets("Input")
    BK = .Range("B1").Value
    ST = .Range("B2").Value
    End With
    
    Sheets("Data").Select
    Set rFilterRange = Sheets("Data").Range("A1:G" & Cells(Rows.Count, 1).End(xlUp).Row)
        
        rFilterRange.AutoFilter Field:=1, Criteria1:="=" & BK
        rFilterRange.AutoFilter Field:=3, Criteria1:="=" & ST & "*"
        
        ActiveSheet.UsedRange.Select
        Selection.Copy
        Sheets("Output").Select
        Range("A1").Select
        ActiveSheet.Paste
    End Sub
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

+ 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. Replies: 2
    Last Post: 08-12-2014, 12:56 PM
  2. [SOLVED] Make large chunk of If statements efficient. Array maybe?
    By mdovey in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 04-20-2014, 11:32 AM
  3. how to make reconciliations more efficient
    By zazzz in forum Excel General
    Replies: 5
    Last Post: 07-22-2013, 09:38 AM
  4. [SOLVED] How can I make this code more efficient?
    By Mr.Whiskers in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-09-2012, 11:11 PM
  5. How to make VBA code more efficient?
    By globulous in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-14-2010, 12:02 PM

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