+ Reply to Thread
Results 1 to 8 of 8

Is there any method to get data with specific criteria. with VBA or Pivot Table

  1. #1
    Registered User
    Join Date
    11-25-2021
    Location
    India
    MS-Off Ver
    2016
    Posts
    54

    Is there any method to get data with specific criteria. with VBA or Pivot Table

    I have data set want extract data from data set with specific criteria using VBA, Macros etc.
    Specific.xlsx
    Last edited by meraz; 04-07-2023 at 11:42 PM.

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this !


    For good enough readers only :

    According to your attachment first in Sheet2 :
    - Class must be in cell A5
    - its criteria in cell A6 Text formatted
    - first month in cell C6
    - last month in cell E6

    A beginner starter Excel basics VBA demonstration to paste only to the Sheet2 worksheet module :

    PHP Code: 
    Sub Demo1()
            
    UsedRange.Rows(UsedRange.Rows.Count).Row:  If 6 Then Rows("7:" V).Delete
        With 
    [Sheet1!A3].CurrentRegion
            V 
    Application.Match(Array([C6], [E6]), .Rows(1), 0):  If Application.Count(V) < 2 Then Beep: Exit Sub
            
    If V(1) > V(2Then Beep: Exit Sub
            
    If Left([A6], 1) <> "=" Then [A6] = "=" & [A6]
           .
    Range("A1:E1").Copy [A8]
           .
    Cells(V(1)).Resize(, V(2) - V(1) + 1).Copy [F8]
           .
    AdvancedFilter 2, [A5:A6], [A8].CurrentRegion
        End With
        With 
    [A8].CurrentRegion.Columns
                 
    .Range("A2:A" & .Rows.Count) = Evaluate("ROW(1:" & .Rows.Count ")")
            
    With .Item(.Count 1)
                 .
    Font.Bold True
                 
    .FormulaR1C1 "=SUM(RC[" - .Column "]:RC[-1])"
                 
    .Cells(1) = "Total"
            
    End With
            With 
    .Cells(.Rows.Count6)(2).Resize(, .Count 4)
                 .
    Font.Bold True
                 
    .Formula "=SUM(F9:F" & .Row ")"
            
    End With
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 04-07-2023 at 09:04 AM. Reason: optimization ...

  3. #3
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Is there any method to get data with specific criteria. with VBA or Pivot Table

    Hello. I show you another variant:

    PHP Code: 
    Sub Macro9()
    Dim tb1 As ListObjecttb2 As ListObjectAs RangeAs RangeqCol%
    Application.ScreenUpdating False
    Rem 
    ----------------->
    Set tb1 Range("Table1").ListObjectSet tb2 Range("Table2").ListObject
    With tb2
    .Range
      
    If .Rows.Count 2 Then .Offset(2).Resize(.Rows.Count 2).Delete xlShiftUp
      
    If .Columns.Count 6 Then .Offset(, 6).Resize(, .Columns.Count 6).Columns.Delete xlShiftToLeft
    End With
    Rem 
    ----------------->
    Set C tb1.HeaderRowRange.Find(Range("Crit2"), LookAt:=xlWhole)
    Set D tb1.HeaderRowRange.Find(Range("Crit3"), LookAt:=xlWhole)
    qCol Range(CD).Columns.Count
    tb2
    .Resize tb2.Range.Resize(2qCol): tb2.HeaderRowRange(6).Resize(, qCol).FillRight
    tb2
    .HeaderRowRange(6).Resize(, qCol) = Range(CD).Value
    tb2
    .HeaderRowRange(tb2.ListColumns.Count) = tb1.HeaderRowRange(tb1.ListColumns.Count).Value
    Rem 
    ----------------->
    [
    aa1] = Range("Crit1")(0).Value: [aa2] = "=""=" Range("Crit1") & """"
    tb1.Range.AdvancedFilter 2, [aa1:aa2], tb2.HeaderRowRangeFalse
    [aa1:aa2].Delete xlShiftUp
    Rem 
    ----------------->
    tb2.Resize tb2.Range.CurrentRegion
    tb2
    .Range(21) = 1tb2.ListColumns(1).DataBodyRange.DataSeries
    tb2
    .ListColumns(tb2.ListColumns.Count).DataBodyRange "=Sum(RC[-" qCol "]:RC[-1])"
    End Sub 
    Attached Files Attached Files
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Is there any method to get data with specific criteria. with VBA or Pivot Table

    Just for the fun of it...
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  5. #5
    Registered User
    Join Date
    11-25-2021
    Location
    India
    MS-Off Ver
    2016
    Posts
    54

    Re: Is there any method to get data with specific criteria. with VBA or Pivot Table

    Quote Originally Posted by beyond Excel View Post
    Hello. I show you another variant:

    PHP Code: 
    Sub Macro9()
    Dim tb1 As ListObjecttb2 As ListObjectAs RangeAs RangeqCol%
    Application.ScreenUpdating False
    Rem 
    ----------------->
    Set tb1 Range("Table1").ListObjectSet tb2 Range("Table2").ListObject
    With tb2
    .Range
      
    If .Rows.Count 2 Then .Offset(2).Resize(.Rows.Count 2).Delete xlShiftUp
      
    If .Columns.Count 6 Then .Offset(, 6).Resize(, .Columns.Count 6).Columns.Delete xlShiftToLeft
    End With
    Rem 
    ----------------->
    Set C tb1.HeaderRowRange.Find(Range("Crit2"), LookAt:=xlWhole)
    Set D tb1.HeaderRowRange.Find(Range("Crit3"), LookAt:=xlWhole)
    qCol Range(CD).Columns.Count
    tb2
    .Resize tb2.Range.Resize(2qCol): tb2.HeaderRowRange(6).Resize(, qCol).FillRight
    tb2
    .HeaderRowRange(6).Resize(, qCol) = Range(CD).Value
    tb2
    .HeaderRowRange(tb2.ListColumns.Count) = tb1.HeaderRowRange(tb1.ListColumns.Count).Value
    Rem 
    ----------------->
    [
    aa1] = Range("Crit1")(0).Value: [aa2] = "=""=" Range("Crit1") & """"
    tb1.Range.AdvancedFilter 2, [aa1:aa2], tb2.HeaderRowRangeFalse
    [aa1:aa2].Delete xlShiftUp
    Rem 
    ----------------->
    tb2.Resize tb2.Range.CurrentRegion
    tb2
    .Range(21) = 1tb2.ListColumns(1).DataBodyRange.DataSeries
    tb2
    .ListColumns(tb2.ListColumns.Count).DataBodyRange "=Sum(RC[-" qCol "]:RC[-1])"
    End Sub 
    Thank you for the effort and creating new sheet for me, but I got security Risk Message "Microsoft has blocked macros from running because source of these file is untrusted."

  6. #6
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Is there any method to get data with specific criteria. with VBA or Pivot Table

    Quote Originally Posted by meraz View Post
    Thank you for the effort and creating new sheet for me, but I got security Risk Message "Microsoft has blocked macros from running because source of these file is untrusted."
    I imagine this message appears when you open the workbook.

    However, it also offers you two alternatives: enable or disable macros... Select 'Enable' macros!

  7. #7
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: Is there any method to get data with specific criteria. with VBA or Pivot Table

    Quote Originally Posted by meraz View Post
    I got security Risk Message "Microsoft has blocked macros from running because source of these file is untrusted."
    Pillow reading:
    https://answers.microsoft.com/en-us/...7-a0801e264cc2
    https://learn.microsoft.com/en-us/de...macros-blocked

    Artik

  8. #8
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Is there any method to get data with specific criteria. with VBA or Pivot Table


+ 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. [SOLVED] Pivot Table or Method to Rearrange Data
    By bradlehoux in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 08-21-2018, 12:42 PM
  2. [SOLVED] Non Pivot method to gather specific data.
    By jomili in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-27-2018, 02:21 PM
  3. Assigning specific data to a specific column value in pivot table
    By dv1651st in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 09-10-2016, 05:51 PM
  4. [SOLVED] pivot table showing specific criteria
    By bzl in forum Excel Charting & Pivots
    Replies: 10
    Last Post: 04-26-2013, 03:05 AM
  5. Replies: 0
    Last Post: 09-17-2012, 11:10 AM
  6. Macro to copy specific data to specific columns from a pivot table
    By Raju Radhakrishnan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-09-2012, 07:24 PM
  7. PIVOT TABLE: Hide All Fields (without specific criteria)
    By wealthistime in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-08-2007, 01:54 PM

Tags for this Thread

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