Results 1 to 1 of 1

Help Making Changes To Code To Filter More Data

Threaded View

artiststevens Help Making Changes To Code... 01-23-2017, 04:30 AM
  1. #1
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Question Help Making Changes To Code To Filter More Data

    Hello,
    I have a VBA file that takes data from one sheet and filters it on another using VBA code. The original worksheet had a small amount of data but now I wish to expand the amount of data that runs but having trouble changing the code to suit my needs. I made changes to the rows and columns factored into the calculations but I keep receiving errors.

    I am seeking help with figure out what changes I need to make to the code to allow for more columns and rows of data to be calculated. I want it done the same way as the original with simply more columns and rows of data.


    Complete Code:
    Sub Filter_it()
    
    Dim i As Integer
    Dim lr As Long
    Dim keyrng As Range
    
    Application.ScreenUpdating = False
    
    Worksheets("Filtered").Range("A1:ZZ1000").ClearContents
    
    Worksheets("Filter_Formula").Activate
    
       lr = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
    
        Range("A4:O" & lr).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
            "Crit"), CopyToRange:=Range("Filter_Start"), Unique:=False
            
        Worksheets("Filtered").Activate
            
        For i = 11 To 15
        
           lr = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
    
            Set keyrng = Range(Cells(2, i), Cells(2, i))
        '
        ' Remove Duplicates
        '
            Range(Cells(1, i), Cells(lr, i)).Select
            ActiveSheet.Range(Cells(1, i), Cells(lr, i)).RemoveDuplicates Columns:=1, Header:= _
                xlYes
         '
         '  Sort
         '
            ActiveSheet.Sort.SortFields.Clear
            ActiveSheet.Sort.SortFields.Add Key:=keyrng, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
                xlSortTextAsNumbers
            With ActiveSheet.Sort
                .SetRange Range(Cells(2, i), Cells(lr, i))
                .Header = xlNo
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
            
        Next i
        
        Insert_Columns
        
        Columns("K:AN").Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
        End With
        
        Application.ScreenUpdating = True
        
        Range("A1").Select
        
    End Sub
    Sub Insert_Columns()
    
       headings = Array(" ", "Total: #/%", "P: #/%", "L: #/%", "B: #/%", "WP: #/%")
       pl = Array(" ", "*", "P", "L", "B", "WP")
    
        Columns("L:P").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Columns("R:V").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Columns("X:AB").Select
         Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Columns("AD:AH").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        
        nfrow = Cells(Rows.Count, "A").End(xlUp).Row
        
        ncc = 5
        
        For i = 11 To 35 Step 6
        
            ncc = ncc + 1
            
            For j = 1 To 5
                nn = i + j
                Cells(1, nn) = headings(j)
                Call Calc_NP(i, nn, ncc, nfrow, pl(j))
            Next j
            
        Next i
        
    End Sub
    Sub Calc_NP(col, cc, c, nfr, cat)
    
    nlast = Cells(Rows.Count, col).End(xlUp).Row
    
    For i = 2 To nlast
       If cat = "*" Then
            Var1 = Cells(i, col)
            Var2 = Application.WorksheetFunction.CountIf(Range(Cells(2, c), Cells(nfr, c)), Var1)
            Cells(i, cc) = Var2
        Else
            Var1 = Cells(i, col)
            Var2 = Application.WorksheetFunction.CountIfs(Range(Cells(2, c), Cells(nfr, c)), Var1, Range(Cells(2, 5), Cells(nfr, 5)), cat)
            pct = 0
            If Cells(i, col + 1) <> 0 Then pct = Var2 / Cells(i, col + 1)
            Cells(i, cc) = Var2 & "/" & Format(pct, "#0.0%")
        End If
        
    Next i
    
    End Sub


    I think I may need to add more rows like the following, but not completely sure:

    Columns("L:P").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Columns("R:V").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Columns("X:AB").Select
         Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Columns("AD:AH").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

    I attached a spreadsheet with three tabs, filter_formula, filtered, and filter_formula_expanded, with the expanded tab containing all of the data I wish to add.

    Thank you very much for reading and for any and all help.
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VB Code for making a user defined formula for referencing the data
    By laansesu in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-19-2016, 04:01 AM
  2. [SOLVED] Need help in making code for macro buttons to choose filter
    By Aladoran in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-27-2013, 02:57 AM
  3. VBA code to filter data
    By Knigtandday in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-18-2013, 05:36 PM
  4. Help Making Filter
    By Hoffman0070 in forum Excel General
    Replies: 3
    Last Post: 10-22-2012, 12:11 PM
  5. Making VBA Code more efficient -Filtering Data
    By greg_c in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-01-2012, 10:45 PM
  6. Code to Filter, Sum and return a value from data set
    By chiz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-01-2008, 09:50 PM
  7. [SOLVED] VBA code to filter data
    By Pelham in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-18-2006, 12:30 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