Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 34 of 34

Thread: Looping through cutting and pasting values between two dates

  1. #21
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    it's pretty fast.
    because no records meet the conditions.
    the dates are all "20 June 2014".

    i assume Row 1 is header row.

    if your table does not have one, i recommend you add manually. insert a blank row, type "Header1" in A1, drag the cell from its bottom-right corner (click and hold left mouse button) to the rightmost column with data.

    Sub Final_Cleanup()
    'assumtion: Row 1 is header row
        Dim StartDate As Date, EndDate As Date
        Dim i As Long, calc As Long, LastRow As Long, LastCol As Long
        Dim PasteRange As Range
        
        With Application
            .DisplayAlerts = False
            .ScreenUpdating = False
            .EnableEvents = False
            calc = .Calculation
            .Calculation = xlCalculationManual
        End With
        
        StartDate = DateSerial(Year(Date - 20), Month(Date - 20), Day(Date - 20))
        EndDate = DateSerial(Year(Date), Month(Date), Day(Date))
        
        On Error Resume Next
        Worksheets("Archive").Delete
        On Error GoTo 0
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Archive"
        
        With Worksheets("Invoice")
            LastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
            LastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
            
            For i = 2 To LastRow
                If IsDate(.Cells(i, 1)) Then
                    If CLng(.Cells(i, 1).Value) < CLng(StartDate) Then
                    Set PasteRange = Worksheets("Archive").Cells(Rows.Count, 1).End(xlUp).Offset(1)
                    PasteRange.Resize(, LastCol).Value = .Range(.Cells(i, 1), .Cells(i, LastCol)).Value
                    .Cells(i, LastCol + 1).Value = "Del"
                    End If
                End If
            Next i
            
            .Cells(1).AutoFilter Field:=LastCol + 1, Criteria1:="=Del"
            .UsedRange.Columns(1).Offset(1).SpecialCells(12).EntireRow.Delete '12 = xlCellTypeVisible
            .AutoFilterMode = False
            .UsedRange.Columns(LastCol + 1).ClearContents
        End With
        
        With Application
        .EnableEvents = True
        .Calculation = calc
        End With
    End Sub
    Last edited by mancubus; 06-23-2014 at 02:18 AM.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  2. #22
    Thank you, will try that tomorrow. Is there a way I can do a
    progress bar and for it to not slow down the code anymore?

  3. #23
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome.

    afaik, progress bars mostly increase the code execution times.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #24
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    1. You have links to a workbook called 'Basware Daily Report Data.xlsm' -- needed?

    2. In your Book1.xlsm I don't see any formulas, just data.

    3. You said 'compile' -- that's something different than 'complete' or 'run' or 'execute' or 'finally get done' -- no biggie, just terminology

    4. Application.Statusbar does not slow it down as much as a progress bar

    5. Your Book1.xlsm did not have any macros in it, so I used my previous post and updated ...

    6. I had to fiddle some dates for test since they were all 6/20


    Option Explicit
    Sub Final_Cleanup()
        Dim iNumberOfDaysInPast As Long
        Dim iInvoiceRow As Long, iArchiveRow As Long, iTotalRows As Long
         
         'set configuration
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        Application.DisplayAlerts = False
        Application.EnableEvents = False
         
         
        iNumberOfDaysInPast = 20
         
        On Error Resume Next
        Sheets("Archive").Delete
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Archive"
        On Error GoTo 0
         
        With Worksheets("Invoice")
        
            'copy header
            Call .Rows(1).Copy(Worksheets("Archive").Rows(1))
         
             'pass 1 to copy the rows top to bottom
            iArchiveRow = 2
            iTotalRows = .Cells(1, 1).CurrentRegion.Rows.Count
            For iInvoiceRow = 2 To iTotalRows
                
                If iInvoiceRow Mod 100 = 0 Then Application.StatusBar = "Pass #1 - " & Format(iInvoiceRow, "#,###") & " of " & Format(iTotalRows, "#,###")
                
                If IsDate(.Cells(iInvoiceRow, 1)) Then
                    If CLng(.Cells(iInvoiceRow, 1).Value) < Date - iNumberOfDaysInPast Then
                        Call .Rows(iInvoiceRow).Copy(Worksheets("Archive").Rows(iArchiveRow))
                        .Cells(iInvoiceRow, 1).Value = True '   marker to delete next pass
                        iArchiveRow = iArchiveRow + 1
                    End If
                End If
            Next iInvoiceRow
        End With
         
         'pass 2 to delete the marked rows bottom to top
        With Worksheets("Invoice")
            For iInvoiceRow = .Cells(1, 1).CurrentRegion.Rows.Count To 2 Step -1
                If iInvoiceRow Mod 100 = 0 Then Application.StatusBar = "Pass #1 - " & Format(iInvoiceRow, "#,###") & " of " & Format(iTotalRows, "#,###")
                If .Cells(iInvoiceRow, 1).Value = True Then .Rows(iInvoiceRow).Delete
            Next iInvoiceRow
        End With
         
         
         'reset configuration
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
        Application.DisplayAlerts = True
        Application.EnableEvents = True
        Application.StatusBar = False
         
        Exit Sub
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #25
    mancubus, I get an error on
    .Cells(1).AutoFilter Field:=LastCol + 1, Criteria1:="=Del"
    ... but your code seems to working pretty fast up until that line of code.

    Paul_Hossler - Thank you for taking so much effort with your answer, your Application status bar is working fine, however it is still taking a very long time to run. I think Auto filter is the only thing that will speed up the run time....

  6. #26
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i dont duplicate the error. works fine for me with the uploaded file.
    what error message do you get?




    sorting the range on helper column before deleting the rows will speed up the code.

    i added another helper column to keep the row order.

    if you are sure all values in column A are dates, deleting the first condition (If IsDate(.Cells(i, 1)) Then) will also speed up the code.

    Sub Final_Cleanup()
        
        Dim StartDate As Date, EndDate As Date
        Dim i As Long, calc As Long, LastRow As Long, LastCol As Long
        Dim PasteRange As Range
         
        With Application
            .DisplayAlerts = False
            .ScreenUpdating = False
            .EnableEvents = False
            calc = .Calculation
            .Calculation = xlCalculationManual
        End With
         
        StartDate = DateSerial(Year(Date - 20), Month(Date - 20), Day(Date - 20))
        EndDate = DateSerial(Year(Date), Month(Date), Day(Date))
         
        On Error Resume Next
        Worksheets("Archive").Delete
        On Error GoTo 0
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Archive"
         
        With Worksheets("Invoice")
            LastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
            LastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
            .Cells(1, LastCol + 1).Value = "Mark"
            .Cells(1, LastCol + 2).Value = "Seq No"
            
            For i = 2 To LastRow
                .Cells(i, LastCol + 2).Value = i
                If IsDate(.Cells(i, 1)) Then
                    If CLng(.Cells(i, 1).Value) < CLng(StartDate) Then
                        Set PasteRange = Worksheets("Archive").Cells(Rows.Count, 1).End(xlUp).Offset(1)
                        PasteRange.Resize(, LastCol).Value = .Range(.Cells(i, 1), .Cells(i, LastCol)).Value
                        .Cells(i, LastCol + 1).Value = "Del"
                    End If
                End If
            Next i
             
            .UsedRange.Sort Key1:=.Cells(2, LastCol + 1), Order1:=xlAscending, Header:=xlYes
            .Cells(1).AutoFilter Field:=LastCol + 1, Criteria1:="=Del"
            .UsedRange.Columns(1).Offset(1).SpecialCells(12).EntireRow.Delete '12 = xlCellTypeVisible
            .AutoFilterMode = False
            .UsedRange.Sort Key1:=.Cells(2, LastCol + 2), Order1:=xlAscending, Header:=xlYes
            .UsedRange.Columns(LastCol + 1).ClearContents
            .UsedRange.Columns(LastCol + 2).ClearContents
        End With
         
        With Application
            .EnableEvents = True
            .Calculation = calc
        End With
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #27
    Error is 'Run-time error Sort method of Range class failed for
     .UsedRange.Sort Key1:=.Cells(2, LastCol + 1), Order1:=xlAscending, Header:=xlYes
    this code is taking more time the previous piece of code.

    For the last line that threw an error the error was "Autofilter method of Range class failed" for
            .Cells(1).AutoFilter Field:=LastCol + 1, Criteria1:="=Del"
    And not all values are Dates so I need IsDate

  8. #28
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    there may be blank columns in the usedrange. so make sure row 1 has headers for all columns in the used range. insert dummy header names where necessary.

    i attach my test file.

    i added a line to remove existing filters after .... With Works..... line.
    Attached Files Attached Files
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  9. #29
    Hi mancubus, I keep getting 'Autofilter method of Range class failed' error on line .Cells(1).AutoFilter Field:=LastCol + 1, Criteria1:="=Del". I think I will just use the code without Autofilter even though it takes an extremely long time. But this lot of code only needs to be done once a month which is okay. I could just do it more regularly so that there is less data to go through which is the main problem I think for the slowness.

    Thank you for all your help though and everyone's help. I really appreciate it.

  10. #30
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you're welcome.

    insert Exit Sub after Next i line so rest of the code will not be executed.

    after running the code, click "macro recorder", manually select, sort, filter used range (include last two columns added by above code), click "stop recording".

    paste recorded macro here.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  11. #31
    Thanks mancubus

  12. #32
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome.

    does that mean it's solved?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  13. #33
    Yup, Thank you

  14. #34
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you're welcome.

    thanks for the feedback and marking the thread as solved.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Tags for this Thread

Posting Permissions

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