+ Reply to Thread
Results 1 to 4 of 4

VBA to filter Data

  1. #1
    Registered User
    Join Date
    02-21-2017
    Location
    bloomfield, NJ
    MS-Off Ver
    2016
    Posts
    29

    VBA to filter Data

    Good Evening and thank you for coming to my Aid!

    I would like the data on worksheet "List of accounts" to filter and display only the newest date in "Column N". How can I add this at the end of my VBA?


    Sub Pivot_Refresh()

    Dim ws As Worksheet
    Set ws = Worksheets("List of all accounts")
    If ws.Application.WorksheetFunction.CountBlank(ws.Range("O2:O199")) > 0 Then
    MsgBox "There Are Blank Cells in Range O2:O199"
    Else

    Dim Table As PivotTable
    Set Table = ActiveSheet.PivotTables("PivotTable3")

    Table.RefreshTable

    Dim shtP As Worksheet
    Dim shtL As Worksheet
    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem
    Dim DateString As String
    Dim LRow1 As Long
    Dim LRow2 As Long

    Set shtP = Sheets("Pivot")
    Set shtL = Sheets("List of all Accounts")
    Set pt = shtP.PivotTables("PivotTable3")
    Set pf = pt.PivotFields("date")
    DateString = Format(Now(), "m/d/yyyy")
    LRow1 = shtL.Range("A" & Rows.Count).End(xlUp).Row + 1

    ActiveSheet.PivotTables ("PivotTable3")

    For Each pi In pf.PivotItems
    If pi.Value <> DateString Then
    End If
    Next pi

    shtL.Range("A2:O199").Copy shtL.Cells(LRow1, "A")

    LRow2 = shtL.Range("A" & Rows.Count).End(xlUp).Row

    If ws.Application.WorksheetFunction.Weekday(ws.Range("N2") + 1) = 6 Then
    shtL.Range("N2:N199").Value = shtL.Range("N2").Value + 4
    Else

    shtL.Range("N2:N199").Value = shtL.Range("N2").Value + 1
    End If

    shtL.Range("O2:O199").ClearContents

    MsgBox "Now Filter B1 to todays Date"
    End If

    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,469

    Re: VBA to filter Data

    How can I add this at the end of my VBA?
    Record a macro while you do it manually. Then copy and paste the code into your routine. It'll probably need some tweaking for ranges, etc.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    02-21-2017
    Location
    bloomfield, NJ
    MS-Off Ver
    2016
    Posts
    29

    Re: VBA to filter Data

    Thanks,

    I originally wanted to have the sheet filter by newly added data which wouldn't have been availabe prior to running the VBA. So macro recording wouldn't have worked. However after I played around with the VBA a little I realized I could just have the filter look for blank cells. I rewrote the vba and it works perfectly for my purposes now.

    Thank you for your suggestion.

    Sub Pivot_Refresh()

    Dim ws As Worksheet
    Set ws = Worksheets("List of all accounts")
    If ws.Application.WorksheetFunction.CountBlank(ws.Range("O2:O199")) > 0 Then
    MsgBox "There Are Blank Cells in Range O2:O199"
    Else

    Dim Table As PivotTable
    Set Table = ActiveSheet.PivotTables("PivotTable3")

    Table.RefreshTable

    Dim shtP As Worksheet
    Dim shtL As Worksheet
    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem
    Dim DateString As String
    Dim LRow1 As Long
    Dim LRow2 As Long

    Set shtP = Sheets("Pivot")
    Set shtL = Sheets("List of all Accounts")
    Set pt = shtP.PivotTables("PivotTable3")
    Set pf = pt.PivotFields("date")
    DateString = Format(Now(), "m/d/yyyy")
    LRow1 = shtL.Range("A" & Rows.Count).End(xlUp).Row + 1

    ActiveSheet.PivotTables ("PivotTable3")

    For Each pi In pf.PivotItems
    If pi.Value <> DateString Then
    End If
    Next pi

    shtL.Range("A2:O199").Copy shtL.Cells(LRow1, "A")

    LRow2 = shtL.Range("A" & Rows.Count).End(xlUp).Row

    If ws.Application.WorksheetFunction.Weekday(ws.Range("N2") + 1) = 6 Then
    shtL.Range("N2:N199").Value = shtL.Range("N2").Value + 4
    Else

    shtL.Range("N2:N199").Value = shtL.Range("N2").Value + 1
    End If

    shtL.Range("O2:O199").ClearContents

    MsgBox "Now Filter B1 to todays Date"

    shtL.Range("O1").AutoFilter Field:=15, Criteria1:=""

    End If
    End Sub

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,469

    Re: VBA to filter Data

    You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ 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. using google sheets create filter tab does not filter importrange data correctly
    By western in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 0
    Last Post: 02-23-2021, 08:14 AM
  2. Filter data on sheet1 and show visible filter data on sheet 2 in colmn A
    By TAMMY32 in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 01-25-2021, 09:35 PM
  3. Replies: 1
    Last Post: 04-23-2020, 03:44 PM
  4. VBA Advanced Filter - Two Filters Without Deleting Bottom Filter Data In Same Column Range
    By hysterical.useless in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-14-2017, 02:54 PM
  5. Replies: 1
    Last Post: 12-12-2014, 06:46 AM
  6. Replies: 0
    Last Post: 01-16-2014, 10:55 AM
  7. Replies: 2
    Last Post: 10-07-2013, 04:57 AM

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