+ Reply to Thread
Results 1 to 4 of 4

Look for a quicker code to replace this loop approach

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019
    Posts
    436

    Look for a quicker code to replace this loop approach

    Hello everyone,

    My code below is to extract rows in sheet 1 to sheet 2 with the inefficient loop. The condition is to check if the date matches (as shown below).

    Is it possible to use something like AutoFilter to improve the efficiency? Thanks.

    Sub FindCopy()
        
        Dim lastrow As Long, i As Long, j As Long
        Dim dt As Date, lastmend As Date
    
        With Worksheets("Sheet1")
            lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        End With
        
        With Worksheets("Sheet2")
            j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
        End With
    
        Application.ScreenUpdating = False
    
        On Error Resume Next
        
        For i = 1 To lastrow
            With Worksheets("Sheet1")
                dt = .Cells(i, 1).Value
                lastmend = DateSerial(Year(dt), Month(dt), 0)
                If lastmend = Range("LastMEnd") Then
                    .Rows(i).Copy
                    Worksheets("Sheet2").Range("A" & j).PasteSpecial xlPasteValues
                    j = j + 1
                End If
            End With
        Next i
    
    End Sub

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: Look for a quicker code to replace this loop approach

    Sure try this,
    Adjust sheet names and ranges as required.

    Sub FilterAndCopy()
        Dim Lstrw As Long, Rng As Range, Prng
        Dim sh As Worksheet, ws As Worksheet
        Dim d As String
    
        d = DateSerial(2017, 1, 24)    'jan 24,2017
    
        Set sh = Sheets("Sheet1")
        Set ws = Sheets("Sheet2")
    
        With sh
            Lstrw = .Cells(.Rows.Count, "A").End(xlUp).Row
            .Range("A:A").AutoFilter Field:=1, Criteria1:=d
            Set Rng = .Range("A2:U" & Lstrw)
        End With
    
        With ws
            Set Prng = .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
            Rng.Copy Prng
        End With
    
        sh.AutoFilterMode = False
    
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019
    Posts
    436

    Re: Look for a quicker code to replace this loop approach

    Many thanks, davesexcel.

    For the criteria, I need to find the previous month end date first based on the cell value and from there to check:

                dt = .Cells(i, 1).Value
                lastmend = DateSerial(Year(dt), Month(dt), 0)
                If lastmend = Range("LastMEnd") Then
    Can AutoFilter handle this?

    Thanks again.

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: Look for a quicker code to replace this loop approach

    Attaching a sample workbook enables others to work on your problem:

    To attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include BEFORE/AFTER sheets if needed to show the process you're trying to complete or automate. Remember to desensitize the data.

    Click on GO ADVANCED and click "manage attachments" to open the upload window.



    To add a file to a post

+ 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] Looking for quicker code, in this VBA example
    By deek in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-11-2016, 04:22 PM
  2. Can this code be quicker?
    By demarc in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-09-2016, 10:26 AM
  3. [SOLVED] Quicker way to loop than using Do Until and Activate
    By starcraftbud in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-24-2015, 03:49 PM
  4. Need Help on how to approach a possible if then loop
    By noggles301 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-19-2014, 08:45 PM
  5. A quicker way to use a loop
    By bkeat in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-08-2014, 11:57 AM
  6. Quicker way to loop through WorksheetFunction?
    By newbi004 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-07-2013, 05:11 AM
  7. [SOLVED] Help for v-lookup approach instead of manually cell linking approach - Excel 2010
    By Mirisage in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-31-2013, 01:32 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