Consulting

Results 1 to 7 of 7

Thread: VBA Show All Data

  1. #1
    VBAX Tutor
    Joined
    Oct 2012
    Posts
    298
    Location

    VBA Show All Data

    Hi, I wonder whether someone may be able to help me please.

    I'm using the code below to perform a number of tasks upon 'Workbook Close'.

    [vba]Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim EndRow As Long
    Sheets("Input").Protect "handsoff", UserInterfaceOnly:=True
    With ThisWorkbook.Worksheets("Input")
    If Sheets("Input").FilterMode = True Then
    Sheets("Input").ShowAllData
    End If
    If .Range("B7").Value = "" Then Exit Sub
    'find last row of data
    EndRow = .Range("B7").End(xlDown).Row - 1
    .Range("B7:AG" & EndRow).Sort Key1:=.Range("B7"), _
    Order1:=xlAscending, _
    Header:=xlGuess, _
    OrderCustom:=1, _
    MatchCase:=False, _
    Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal

    End With
    End Sub[/vba]
    One of these tasks is to remove any filters that may have been set by the user, and this done via the following piece of code:

    [vba] If Sheets("Input").FilterMode = True Then
    Sheets("Input").ShowAllData
    End If[/vba]
    The problem I have is that when a user filters the records in the spreadsheet and then saves the file, rather than the filter being removed to show all the records, they receive a 'Run time error 13 mismatch' error and I've no idea why.

    The sheet is password protected, so I'm not sure whether this is the issue, but I've been trying to get this to work for most of the afternoon after searching the internet, without any success.

    I just wondered whether someone could possibly look at this please and let me know where I'm going wrong.

    Many thanks and lin dregards

    Chris

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    On which line do you get the error?
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    VBAX Tutor
    Joined
    Oct 2012
    Posts
    298
    Location
    Hi @Simon Lloyd, thank you for taking the time to reply to my post.

    I don't get the error on a specific line, but when I omit this code:

    [VBA]If Sheets("Input").FilterMode = True Then
    Sheets("Input").ShowAllData
    End If[/VBA]

    The user no longer receive the error message.

    Many thanks and kind regards

    Chris

  4. #4
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Try unprotecting the the sheet prior to filtering then protecting again afterwards. Are all the users using the same version of Excel?
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    If all data is showing, that line will error out.

    Also, as Simon states, you can't perform this action on a worksheet that has been protected, unless you protect with the ability to filter. It's easiest just to trap for the error...

    [vba] Dim WS As Worksheet
    Dim EndRow As Long
    Set WS = Me.Worksheets("Input")
    WS.Protect "handsoff", UserInterfaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
    If WS.AutoFilterMode = True Then
    On Error Resume Next
    Err.Clear
    WS.ShowAllData
    On Error GoTo 0
    If Err.Number <> 0 Then
    'filter was already removed
    End If
    End If

    If WS.Range("B7").Value = vbNullString Then Exit Sub
    EndRow = WS.Range("B7").End(xlDown).Row - 1
    WS.Range("B7:AG" & EndRow).Sort Key1:=WS.Range("B7"), _
    Order1:=xlAscending, _
    Header:=xlGuess, _
    OrderCustom:=1, _
    MatchCase:=False, _
    Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    [/vba]

    Edit: added AllowSorting

    HTH

  6. #6
    VBAX Tutor
    Joined
    Oct 2012
    Posts
    298
    Location
    Hi @Zack Barresse, thank you very much for taking the time to reply to my post and for the solution.

    I did try your solution, and at first, the 'Run time error 13' was activated when activating the filter.

    I then had a think about this, and realised that the cause of the error was that they didn't have the 'Analysis toolpack' add in activated.

    As soon as I added a line of code to activate this through VBA, your code worked great.

    Many thanks and kind regards

    Chris

  7. #7
    VBAX Tutor
    Joined
    Oct 2012
    Posts
    298
    Location
    Hi @Simon Lloyd, thank you very much for this.

    As per my reply to @Zack Barresse, the whole issue was caused by the 'Analysis Toolpack' not being activated.

    Once again, many thanks for your help.

    Kind regards

    Chris

Posting Permissions

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