+ Reply to Thread
Results 1 to 9 of 9

VBA code review for Auto filter issue

Hybrid View

  1. #1
    Registered User
    Join Date
    09-11-2019
    Location
    india
    MS-Off Ver
    2016
    Posts
    78

    VBA code review for Auto filter issue

    Hello All,

    Wish you happy new year

    I have a VBA code which I have gotten from this forum with the help of you guys Thank you for your support.
    The code was working fine, but suddenly it started taking wrong data.
    EX: I put a Auto filter (manually) and the count (from row 2) is 228,but when MACRO does the Auto filtering it takes the count (from row 2) as 229 (1 extra). I believe its Auto filter issue that takes hidden data ??(Not sure). Could you please review and help me out on this.

    Sub DMANSQueries()
         Dim wb As Workbook
    
        On Error Resume Next
        Set wb = Workbooks.Open(Application.GetOpenFilename("Excel Files (*.xlsx*), *.xlsx"))
        On Error GoTo 0
        
        If wb Is Nothing Then
            MsgBox "File selection was cancelled." & vbCrLf & vbCrLf & "Exiting...", , "No File Selected"
            Exit Sub
        End If
        
        ThisWorkbook.Worksheets("DM Ans Query ").Rows("2:" & Rows.Count).ClearContents
        With wb.Worksheets(1).Cells(2, 1).Resize(wb.Worksheets(1).UsedRange.Rows.Count, 14)
            .AutoFilter field:=7, Criteria1:="Answered"
            .Columns(3).SpecialCells(xlCellTypeVisible).Copy
            ThisWorkbook.Worksheets("DM Ans Query ").Cells(2, 1).PasteSpecial xlPasteValues
        End With
        
        With ThisWorkbook.Worksheets("DM Ans Query ").Columns(1)
            .NumberFormat = "General"
            .Value = .Value
        End With
        
        wb.Close False
    End Sub

    What I want is, once the macro does the auto filter it should copy and paste the data (as per the existing macro) and it should count from Row2. If the auto filter have zero values (nothing matching the filter criteria) then it should show a message to user as "No data"

    EDIT-1:

    1:I have 2 Workbook.
    A. Query Backlog Report - Open and Answered Queries (I want to put filter for Column7 and 11 (see the VB codes in the Clean Patient Tracker_24Dec2020 already there )

    B:Clean Patient Tracker_24Dec2020: It has multiple sheets . However I want to Copy the filtered data (for Criterias in the VBA) from Query Backlog Report. Once Copied, I want to paste the data in this workbook and in the sheet named "Open query" "DM ANS query" and so on.

    2: I will keep the "Cleanpatienttracker" open. And I will enable the macro button in this sheet, hence it should ask me to choose the file from a folder (as the name could be different sometime)

    In the Query Backlog Report, I want filter for
    .AutoFilter field:=7, Criteria1:="Opened"
    , then copy and paste the data in the respective sheet of "Clean Patient Tracker". Now here I am facing the issue. As it copy 1 extra row which is not correct.

    Similarly I want to put filter for below and copy paste data.
    .AutoFilter field:=7, Criteria1:="Answered"
            .AutoFilter field:=11, Criteria1:="Monitor"
    .AutoFilter field:=7, Criteria1:="Answered"
            .AutoFilter field:=11, Criteria1:="GPS"
    .AutoFilter field:=7, Criteria1:="Answered"
            .AutoFilter field:=11, Criteria1:=Array("Data Manager", "AutoQuery RG"), Operator:=xlFilterValues
    I hope, am not confusing with lot of things.

    Thank you for looking into it.
    Last edited by sudhansu121; 01-19-2021 at 05:31 AM. Reason: Added a file

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: VBA code review for Auto filter issue

    What do you actually mean "Count"?

    Counting the result one without header and with the header the other?
    Last edited by jindon; 01-07-2021 at 03:51 AM.

  3. #3
    Registered User
    Join Date
    09-11-2019
    Location
    india
    MS-Off Ver
    2016
    Posts
    78

    Re: VBA code review for Auto filter issue

    Hi Jindon,
    Thanks for the helping hand here.
    By count I mean, Counting the result one without header.

    What I want is, once the macro does the auto filter it should copy and paste the data (as per the existing macro) and it should count from Row2. If the auto filter have zero values (nothing matching the filter criteria) then it should show a message to user as "No data"

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: VBA code review for Auto filter issue

    Perhaps, resizing by rows count of used range.
    Tyr change to
        With wb.Worksheets(1).Cells(2, 1).Resize(wb.Worksheets(1).UsedRange.Rows.Count - 1, 14)
    I strongly suggest not to use UsedRange.Rows.Count for this situation.
    If ever the sheet has any value/formatted cell/conditional formatting etc in far down from the data range, It will fail to count.

  5. #5
    Registered User
    Join Date
    09-11-2019
    Location
    india
    MS-Off Ver
    2016
    Posts
    78

    Re: VBA code review for Auto filter issue

    I am so sorry, but this is not working. If I put the -1 after Usedrange.rows.count-1

  6. #6
    Registered User
    Join Date
    09-11-2019
    Location
    india
    MS-Off Ver
    2016
    Posts
    78

    Re: VBA code review for Auto filter issue

    Thanks jindon,
    Thanks for the suggestion. Then could you please help me with that. I do not have idea in VBA and if used range needs to be changed to something better.

    Also could you please let me knwo how to get the message box if filtering has no data.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: VBA code review for Auto filter issue

    Something like
        With wb.Sheets(1)
            .AutoFilterMode = False
            With .Range("a2:n" & .Cells(Rows.Count, 14).End(xlUp).Row)
                .AutoFilter 7, "Answered"
                If .Columns(7).SpecialCells(12).Count = 1 Then
                    MsgBox "No data"
                Else
                    MsgBox .Columns(7).SpecialCells(12).Count - 1
                End If
            End With
            .AutoFilterMode = False
        End With

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: VBA code review for Auto filter issue

    Upload the workbook that you are working with.
    I already shutdown my pc, so tomorrow.

  9. #9
    Registered User
    Join Date
    09-11-2019
    Location
    india
    MS-Off Ver
    2016
    Posts
    78

    Re: VBA code review for Auto filter issue

    Thanks buddy will do so

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Auto-comlpete List Code Issue
    By hollysq in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-12-2020, 06:45 PM
  2. Issue copying visible cells after auto filter
    By Ianmacros in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-05-2019, 08:13 PM
  3. [SOLVED] Auto Filter OFF in vba Code
    By evertjvr in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-01-2015, 04:52 PM
  4. Replies: 2
    Last Post: 09-22-2012, 01:18 PM
  5. Using VBA code to auto filter ?
    By AnthonyWB in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-26-2010, 03:43 PM
  6. Auto Filter issue
    By bizmoh in forum Excel General
    Replies: 1
    Last Post: 07-14-2008, 01:04 AM
  7. Auto filter off code
    By pepito_1ton in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-09-2007, 12:10 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1