Results 1 to 5 of 5

VBA Macro to perform sum based on criteria not equalling largest city not working

Threaded View

  1. #1
    Registered User
    Join Date
    03-26-2019
    Location
    NY
    MS-Off Ver
    2013
    Posts
    4

    VBA Macro to perform sum based on criteria not equalling largest city not working

    In the AR sheet is an example output where the red cells are the incorrect values, with the corresponding correct values under it.

    The macro is called "Sheetloop". Not really sure what the issue is. I have tried possibly changing the variables from long to Double, to no avail.

    I figure it has something to do with the <> argument in the sumif formula.


    
    
    Sub Sheetloop()
        Dim ws As Worksheet
        Dim rng As Range
        Dim lgst_avg As Long
        Dim lgst_max As Long
        Dim lgst_min As Long
        Dim LR As Long
        Dim GA_avg As Long
        Dim GA_max As Long
        Dim GA_min As Long
        Dim other_avg As Double
        Dim other_max As Double
        Dim other_min As Double
        
        GA_avg = Worksheets("GA_AVERAGE").Range("C6")
        GA_max = Worksheets("GA_AVERAGE").Range("D6")
        GA_min = Worksheets("GA_AVERAGE").Range("E6")
        For Each ws In Worksheets
            If ws.Name <> "GA_AVERAGE" And ws.Name <> "DC" Then
                LR = ws.Range("F" & Rows.Count).End(xlUp).row
                ws.Range("O1") = "AVG"
                ws.Range("P1") = "Max"
                ws.Range("Q1") = "Min"
                ws.Range("O2:O" & LR).Formula = "=F2*M2"
                ws.Range("P2:P" & LR).Formula = "=E2*M2"
                ws.Range("Q2:Q" & LR).Formula = "=D2*M2"
                
                lgst_avg = Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), ws.Range("S12"), ws.Range("O2:O" & LR)) / Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), ws.Range("S12"), ws.Range("M2:M" & LR))
                lgst_max = Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), ws.Range("S12"), ws.Range("P2:P" & LR)) / Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), ws.Range("S12"), ws.Range("M2:M" & LR))
                lgst_min = Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), ws.Range("S12"), ws.Range("Q2:Q" & LR)) / Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), ws.Range("S12"), ws.Range("M2:M" & LR))
                ws.Range("T12") = lgst_avg
                ws.Range("U12") = lgst_max
                ws.Range("V12") = lgst_min
                ws.Range("w12") = ws.Range("T12") / GA_avg
                ws.Range("X12").Formula = ws.Range("U12") / GA_max
                ws.Range("Y12").Formula = ws.Range("V12") / GA_min
                factor_average = Application.WorksheetFunction.Average(ws.Range("w12"), ws.Range("x12"), ws.Range("y12"))
                ws.Range("z12").Value = factor_average
                
               
                other_avg = Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), "<>" & ws.Range("S12"), ws.Range("O2:O" & LR)) / Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), "<>" & ws.Range("S12"), ws.Range("M2:M" & LR))
                other_max = Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), "<>" & ws.Range("S12"), ws.Range("P2:P" & LR)) / Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), "<>" & ws.Range("S12"), ws.Range("M2:M" & LR))
                other_min = Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), "<>" & ws.Range("S12"), ws.Range("Q2:Q" & LR)) / Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), "<>" & ws.Range("S12"), ws.Range("M2:M" & LR))
                ws.Range("T13") = other_avg
                ws.Range("U13") = other_max
                ws.Range("V13") = other_min
                ws.Range("w13") = ws.Range("T13") / GA_avg
                ws.Range("X13").Formula = ws.Range("U13") / GA_max
                ws.Range("Y13").Formula = ws.Range("V13") / GA_min
                other_factor_average = Application.WorksheetFunction.Average(ws.Range("w13"), ws.Range("x13"), ws.Range("y13"))
                ws.Range("z13").Value = other_factor_average
                
                
             End If
        Next
        End Sub
    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. Macro to copy row based on criteria...not entirely working
    By orlex in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-01-2015, 03:31 PM
  2. [SOLVED] Smallest/largest values based on two criteria
    By jcswaby in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-19-2015, 06:41 AM
  3. [SOLVED] Look up largest number based on criteria
    By namluke in forum Excel General
    Replies: 9
    Last Post: 12-29-2014, 03:22 PM
  4. [SOLVED] Summing Largest 50 values based on criteria
    By Scooby5 in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 08-27-2014, 10:38 PM
  5. [SOLVED] Get second and third largest based on criteria
    By scottylad2 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-18-2013, 11:40 AM
  6. [SOLVED] Finding the largest number based on criteria
    By imerial in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-26-2013, 03:19 PM
  7. Replies: 16
    Last Post: 01-19-2012, 08:18 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