Consulting

Results 1 to 4 of 4

Thread: Filter in a secondary workbook

  1. #1

    Filter in a secondary workbook

    I have 2 files;
    1st - Forecast Review file which reports month by month spend by account code. The account code is in column A.
    2nd - Detailed Transaction list for each account code.

    On selecting the cell in column A (for the account code I would like to review) of the Forecast Review file I am then trying to filter in a second workbook that is open so I can see the detail transaction behind the account code within the selected cell.

    Both files are stored in the same location: C:\MyDrive\Month Forecasts\
    Main file is: Forecast Workings.xlsm (sheet name = YTD Review)
    Second file is: Subledger Transactions.xlsx (sheet name = Trans)

    I believe I am using Microsoft 365 apps for enterprise.

    The macro is in the main file and I am using a selectionchange event to trigger.

    It fails at the following line:
    Sheets("Trans").Range("A5").AutoFilter field = 1, Criteria1:=ACKCode


    The error I get is Run-time error ‘1004’: Autofilter method of Range class failed


    It's been a while since I have compiled code so help is much appreciated. I am assuming this is something to do with the code being in one workbook and the filter action in a second and I am not doing this right.

    Thank you
    Jay

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
       If Intersect(Target, Range("A10:A5000")) Is Nothing Then
          Exit Sub
       Else
          Call FilterReviewDTR
       End If
    End Sub
    
    Sub FilterReviewDTR()
    
    Dim fileName As String
    fileName = "C:\MyDrive\Month Forecasts\Subledger Transactions.xlsx"
    
    
    'Call function to check if the file is open
        If IsFileOpen(fileName) = False Then
            MsgBox "The Review DTR is not open - if you want to review transactions open the file and re-select the cell"
        Else
            ACKCode = Selection.Value
    
    
    Set DetailData = Workbooks.Open("C:\MyDrive\Month Forcasts\Subledger Transactions.xlsx")
        
    With DetailData
    Sheets("Trans").Range("A5").AutoFilter field = 1, Criteria1:=ACKCode  
    End With
        
    End If
    End sub

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    443
    Location
    I think the With DetailData syntax is incomplete. Precede Sheets with period.
    With DetailData
    .Sheets("Trans").Range("A5").AutoFilter field = 1, Criteria1:=ACKCode   
    End With
    Or don't use With since it's only one line.
    DetailData.Sheets("Trans").Range("A5").AutoFilter field = 1, Criteria1:=ACKCode
    Consider migrating database functionality to an actual database app, like Access.
    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
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,800
    Location
    You're also missing a colon in the argument:

    With DetailData.Sheets("Trans").Range("A5").AutoFilter field:=1, Criteria1:=ACKCode  
    End With
    Be as you wish to seem

  4. #4
    Thank you June7 and Aflatoon. I see exactly what you both pointed out. Just goes to show when you have been looking at something for a while you can miss the basics. Thank you.

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
  •