Consulting

Results 1 to 7 of 7

Thread: Use the VBA filter

  1. #1
    VBAX Regular
    Joined
    Nov 2021
    Posts
    14
    Location

    Arrow Use the VBA filter

    Goodmorning everyone,
    I have a macro that copies another extrapolated file, but the problem is that the automatic filter has been removed and what am I wrong?

    Option Explicit
    Public Sub Tester ()
        Dim srcWB As Workbook, destWB As Workbook
        Dim srcSH As Worksheet, destSH As Worksheet
        Dim srcRng As Range, destRng As Range
    Const sFile_Sorgente As String = "C: \ Users \ Itaerxga \ Desktop \ s.xlsx"
        Const sFoglioSorgente As String = "data" '<< === Edit
        Const sFoglio_Destinazione As String = "General"
        Const sSource_Interval As String = "E2: AB20000"
        Const sPrimaCella_Destinazione As String = "A2"
    Set srcWB = Workbooks.Open (sFile_Sorgente)
        Set srcSH = srcWB.Sheets (sSourceSheet)
        Set srcRng = srcSH.Range (sSource_Interval)
        Set destWB = ThisWorkbook
        Set destSH = destWB.Sheets (sDestination_Sheet)
        Set srcRng = srcSH.Range (sSource_Interval)
        Set destRng = destSH.Range (sFirstDestination_Cell)
    srcRng.Copy Destination: = destRng
        srcWB.Close SaveChanges: = False
    End Sub
    Then I would like to filter column D from oldest to newest from D2 to D20000.


    Thanks for your help
    Last edited by Aussiebear; 12-01-2021 at 04:36 AM. Reason: Added code tags to supplied code

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Please, use code tags in your post.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by Yegimor View Post

    I have a macro that copies another extrapolated file(Q1), but the problem is that the automatic filter has been removed and what am I wrong(Q2)?


    Then I would like to filter column D from oldest to newest (Q3) from D2 to D20000. (Q4)

    Q1. What is an 'extrapolated file'?

    Q2. Not including sample workbooks

    Q3. Terminology is important, so I'm guessing that you meant sort??

    Q4. Why hard code D20000?
    ---------------------------------------------------------------------------------------------------------------------

    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

  4. #4
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,194
    Location
    Q1: From my understanding this will be a kind of forecast based on historic actuals.


    Screenshot 2021-12-01 154619.jpg

    If it is anything else then I am stumped.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post (you may need to click 'Go Advanced')

    Excel 365, Version 2403, Build 17425.20146

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by georgiboy View Post
    Q1: From my understanding this will be a kind of forecast based on historic actuals.
    If it is anything else then I am stumpped.
    Well, I know what 'extrapolated' means, but ....

    copies another extrapolated file
    ... does this copy a Workbook or a sheet or a range with forecast data is the question.

    Decoding the macro, the only .Copy seems to be copying a range from 'Workbooks.Open (sFile_Sorgente)' = "C: \ Users \ Itaerxga \ Desktop \ s.xlsx" (I don't think the spaces below there) to ThisWorkbook

    So to answer the OP's original question -- Copying a filtered range from one workbook to another doesn't seem to bring the filter with it. You could (probably) get the Filter object and apply it to the copied data

    Option Explicit
    
    
    Public Sub Tester ()
    Dim srcWB As Workbook, destWB As Workbook
    Dim srcSH As Worksheet, destSH As Worksheet
    Dim srcRng As Range, destRng As Range
    
    
    Const sFile_Sorgente As String = "C: \ Users \ Itaerxga \ Desktop \ s.xlsx"
    Const sFoglioSorgente As String = "data" '<< === Edit
    Const sFoglio_Destinazione As String = "General"
    Const sSource_Interval As String = "E2: AB20000"
    Const sPrimaCella_Destinazione As String = "A2"
    
    
    Set srcWB = Workbooks.Open (sFile_Sorgente)
    
    
    Set srcSH = srcWB.Sheets (sSourceSheet)
    Set srcRng = srcSH.Range (sSource_Interval)
    Set destWB = ThisWorkbook
    Set destSH = destWB.Sheets (sDestination_Sheet)
    Set srcRng = srcSH.Range (sSource_Interval)
    Set destRng = destSH.Range (sFirstDestination_Cell)
    
    
    srcRng.Copy Destination: = destRng    '    <<<<<<<<<<<<<<<<<<<<<<<<
    srcWB.Close SaveChanges: = False
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    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

  6. #6
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,194
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Well, I know what 'extrapolated' means, but ....

    Point taken, I got a bit fixated on the word extrapolated...
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post (you may need to click 'Go Advanced')

    Excel 365, Version 2403, Build 17425.20146

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by georgiboy View Post
    Point taken, I got a bit fixated on the word extrapolated...

    That's ok ... I got a bit fixated on "extrapolated file"
    ---------------------------------------------------------------------------------------------------------------------

    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

Posting Permissions

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