Consulting

Results 1 to 13 of 13

Thread: Transfer specific columns from sheet to another

  1. #1

    Transfer specific columns from sheet to another

    Hello everyone, It would be appreciated if someone could kindly assist me
    I'm trying create a macro that upon running, will look at the dates in column "P" and if this dates is before older than from the current month,Transfer the Columns(A:F) & Columns("O:T") to archives sheet for reference when needed..Then delete of rows that were Transfered from the main sheet How this can be done? The data starts at row 8 to the last row used and headers at row 7
    Any help in the correct direction is appreciated!!! - thanks in advance....
    Attached Files Attached Files

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    331
    Location
    Instead of 'moving' records, could just set value of cell that changes record status then apply filter to not display those 'archived' records. Why use Excel as a database?
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Thanks a lot for your reply
    The main sheet will be updated once a month and at the same time, I want to keep some data in the archive sheet for yearly payments,Thanks again.

  4. #4
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,192
    Location
    Maybe something like the below:
    Sub test()
        Dim rng As Range
        
        Set rng = [A7].CurrentRegion
        
        If Not ActiveSheet.AutoFilterMode = True Then [A7].AutoFilter
        [A7].AutoFilter 16, "<" & DateSerial(Year(Now()), Month(Now()), 1)
        On Error GoTo NoCells
        Union(Range(rng(2, 1), rng(rng.Rows.Count, 6)), Range(rng(2, 15), rng(rng.Rows.Count, 20))).SpecialCells(xlCellTypeVisible).Copy
        On Error GoTo 0
        Sheet2.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
        Application.DisplayAlerts = False
        rng.Offset(1).Resize(rng.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Delete
        Application.DisplayAlerts = True
        [A7].AutoFilter
        Application.CutCopyMode = False
        Exit Sub
        
    NoCells:
        MsgBox "No older dates found"
    End Sub
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  5. #5
    many thanks for your effort!
    I tried to run this and I get a run-time error in this line
        [A7].AutoFilter 16, "<" & DateSerial(Year(Now()), Month(Now()), 1)
    how can fix that? Thanks again.

  6. #6
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,192
    Location
    Maybe the layout of the file you are trying to put the code in is different from the original example file submitted. Attached is the code running in the example file.
    Attached Files Attached Files
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  7. #7
    Will check and revert back...Thanks again.

  8. #8
    This is great when working with low row numbers, however I'm now working on a file with 15000 rows and the macro is taking very long to process
    I need to rely on VBA arrays as possible as I can as original data is very large.
    Can it be possible? if that makes sense.
    thank you for your assistance and cooperation
    Last edited by joky; 01-31-2023 at 05:39 AM.

  9. #9
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,192
    Location
    I don't think arrays will save that much here, the time is being taken deleting the rows from the sheet 'Main'

    If you manually add your filter and then delete the rows that are left manually you will see what i mean.

    You could have another tab for this month and move the data that is needed to that, thus removing the need to delete:
    Sub test()
        Dim rng As Range
        
        Set rng = [A7].CurrentRegion
        
        If Not ActiveSheet.AutoFilterMode = True Then [A7].AutoFilter
        [A7].AutoFilter 16, "<" & DateSerial(Year(Now()), Month(Now()), 1)
        On Error GoTo NoCells
        Union(Range(rng(2, 1), rng(rng.Rows.Count, 6)), Range(rng(2, 15), rng(rng.Rows.Count, 20))).SpecialCells(xlCellTypeVisible).Copy
        On Error GoTo 0
        Sheet2.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
        
        [A7].AutoFilter 16, ">=" & DateSerial(Year(Now()), Month(Now()), 1)
        On Error GoTo NoCells
        rng.Offset(1).Resize(rng.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Copy
        On Error GoTo 0
        Sheet3.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
        [A7].AutoFilter
        Exit Sub
        
    NoCells:
        MsgBox "No older/ newer dates found"
    End Sub
    Other than that maybe someone else here on the forum has a suggestion?
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  10. #10
    for a reason I don't understand, same previous error message
        [A7].AutoFilter 16, "<" & DateSerial(Year(Now()), Month(Now()), 1)

    I think it would be worthwhile using arrays if there's lots of data but I cannot find how to do this .... You can send an attachment.. Thanks again.

  11. #11
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,192
    Location
    Attached is the file i was playing with.
    Attached Files Attached Files
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  12. #12
    Thanks a lot for great help you offered in this issue, I respect for your kind time and help
    Have a nice time my Professor .... Best Regards

  13. #13
    How can this be done using arrays?

Posting Permissions

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