+ Reply to Thread
Results 1 to 8 of 8

Quicker way to loop through WorksheetFunction?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Quicker way to loop through WorksheetFunction?

    Hi

    I have the following code that loops through data to check what mileage each vehicle has done between "Current Month" and "Last Month".

    The code uses WorksheetFunction.Vlookup to compare vehicle registrations for current & last months and their corresponding mileage readings.

    Finally, the mileage readings difference between current month and last month is placed in column 12 of current month to give the mileage done in current month.

    The code works fine but takes about 7 seconds to complete its trawl through about 300 rows of data.

    As the list is likely to increase in future, I was wondering if there is a quicker way to loop?

    Any advice would be greatly appreciated.

    Thanks

    On Error Resume Next
        Table1 = Sheets("Last Month Data").Range("A2:K" & LastRow1)
        Table2 = Sheets("Current Month Data").Range("A2:K" & LastRow2)
            Mileage_Row = 2
            Mileage_Col = 12
        
            For Each cl In Table2
                Sheets("Current Month Data").Cells(Mileage_Row, Mileage_Col) = Sheets("Current Month Data").Cells(Mileage_Row, Mileage_Col - 10) _
                - Application.WorksheetFunction.VLookup(cl, Table1, 2, False)
                Mileage_Row = Mileage_Row + 1
            Next

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Quicker way to loop through WorksheetFunction?

    Any chance of posting a copy of your workbook?
    If you like my contribution click the star icon!

  3. #3
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Quicker way to loop through WorksheetFunction?

    Sorry about the delay. A sample is attached
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Quicker way to loop through WorksheetFunction?

    Any thoughts?


  5. #5
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Quicker way to loop through WorksheetFunction?

    Please try

    Public Sub recordLastMonthMilage()
    
    '#
    '# declare private variables
    '#
       Dim pvt_xls_LastMonth As Excel.Worksheet
       Dim pvt_dct_LastMonth As Object
       Dim pvt_lng_RowNumber As Long
       
    '#
    '# initialise
    '#
       Set pvt_xls_LastMonth = ThisWorkbook.Worksheets("Last Month Data")
       Set pvt_dct_LastMonth = CreateObject("Scripting.Dictionary")
    
    '#
    '# build a dictionary holding the milage of last month per car registration
    '#
       With pvt_xls_LastMonth
          For pvt_lng_RowNumber = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
             If Not pvt_dct_LastMonth.Exists(.Cells(pvt_lng_RowNumber, "A").Value) Then
                pvt_dct_LastMonth.Add .Cells(pvt_lng_RowNumber, "A").Value, .Cells(pvt_lng_RowNumber, "B").Value
             End If
          Next pvt_lng_RowNumber
       End With
       
    '#
    '# for each registration of the current month worksheet retrieve the milage submitted in the previous
    '# month and record the difference in column 12
    '#
       With ThisWorkbook.Worksheets("Current Month Data")
          For pvt_lng_RowNumber = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
             If Not pvt_dct_LastMonth.Exists(.Cells(pvt_lng_RowNumber, "A").Value) Then
                .Cells(pvt_lng_RowNumber, "L").Value = "NEW ?"
             Else
                .Cells(pvt_lng_RowNumber, "L").Value = .Cells(pvt_lng_RowNumber, "B").Value - pvt_dct_LastMonth(.Cells(pvt_lng_RowNumber, "A").Value)
             End If
          Next pvt_lng_RowNumber
       End With
    
    '#
    '# housekeeping
    '#
       Set pvt_dct_LastMonth = Nothing
       
    End Sub

  6. #6
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Quicker way to loop through WorksheetFunction?

    I was in a meeting at work. Will have a look

  7. #7
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Quicker way to loop through WorksheetFunction?

    or, using a different approach

    Public Sub recordLastMonthMilageUsingADO()
    
       '#
       '# declare private variables
       '#
          Dim pvt_obj_Connection As Object
          Dim pvt_obj_Recordset As Object
          Dim pvt_str_Query As String
    
          Const adOpenDynamic As Long = 2
          Const adLockReadOnly As Long = 1
       
       '#
       '# perform a LEFT JOIN on registration codes, subtracting the milage of last month from the
       '# milage of the current month where the registration codes match
       '#
          pvt_str_Query = "SELECT [S1].[Mileage Submitted] - [S2].[Mileage Submitted] " & _
                         "   FROM [Current Month Data$] AS S1 LEFT JOIN [Last Month Data$] AS S2 " & _
                         "     ON [S1].[Registration Submitted] = [S2].[Registration Submitted];"
       '#
       '# get data
       '#
          Set pvt_obj_Connection = CreateObject("ADODB.Connection")
          With pvt_obj_Connection
             .Provider = "Microsoft.Jet.OLEDB.4.0"
             .ConnectionString = "Data Source='" & ThisWorkbook.Path & "\" & ThisWorkbook.Name & "'; " & "Extended Properties=Excel 8.0"
             .Open
          End With
          
          Set pvt_obj_Recordset = CreateObject("ADODB.Recordset")
          pvt_obj_Recordset.Open pvt_str_Query, pvt_obj_Connection, adOpenDynamic, adLockReadOnly
          
          ThisWorkbook.Worksheets("Current Month Data").Range("L2").CopyFromRecordset pvt_obj_Recordset
       
       '#
       '# housekeeping
       '#
          pvt_obj_Recordset.Close
          pvt_obj_Connection.Close
                         
       End Sub

  8. #8
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Quicker way to loop through WorksheetFunction?

    Wow! A completely new approach, OllieB

    I will try this out and let you know how I get on.

    Many thanks for taking the time to help

+ 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. more efficient way to do for-loop using application.worksheetfunction.countifs
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 27
    Last Post: 09-08-2013, 10:36 PM
  2. worksheetfunction.countifs with loop from multiple worksheets
    By Lifeseeker in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2012, 04:55 PM
  3. worksheetfunction.countifs with loop
    By Lifeseeker in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-02-2012, 01:26 PM
  4. Quicker way?
    By Craiig in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-22-2008, 07:32 AM
  5. Quicker VBA
    By sparx in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-10-2007, 06:47 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