Solved

Getting SumIf values with VBA

Posted on 2010-11-17
4
649 Views
Last Modified: 2012-05-10
I have a number of formulas in Column AZ like this:
=SUMIF(AllConfigs,AV6,AllJ)

I'd like to have the option of using VBA to produce the same results. When I run this code below, I get all zeroes.  Is there a way to make this work?

Thanks,
John


Sub SumIfs()

    Dim cel As Range

    For Each cel In Range("AZ6:AZ10")

    Dim str As String

    str = cel.Offset(0, -4).Address

    cel = Application.WorksheetFunction.SumIf([AllConfigs], str, [AllJ])

    Next

    End Sub

    End Sub

Open in new window

0
Comment
Question by:gabrielPennyback
4 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 250 total points
Comment Utility
cel.formula = "=sumif(Allconfigs," & str & ",AllJ)"
cel.value = cel.value
0
 
LVL 23

Expert Comment

by:Michael74
Comment Utility
John

If you are using VBA why not just write the logic yourself for the function

The code below get a total for range A1:A20 where the value in the same row for Column B is equal to
the value in C1
Sub sumifs()



   Dim i As Integer, j As Integer

   Dim sumif As Long

   Dim test As Long

     

   test = Range("C1").Value

     

   For i = 6 To 10

      sumif = 0

      For j = 1 To 20

         If Range("B" & j).Value = test Then

            sumif = sumif + Range("A" & j).Value

         End If

      Next

      Range("AZ" & i).Value = sumif

   Next



End Sub

Open in new window

0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 250 total points
Comment Utility
That's because in VBA code you use the actual ranges not the addresses.
Try
cel = Application.WorksheetFunction.SumIf(Range("AllConfigs"), cel.Offset(0, -4).Text, Range("AllJ"))

I assume cel.Offset(0,-4) contains the condition (don't put quotes around it)
0
 
LVL 1

Author Closing Comment

by:gabrielPennyback
Comment Utility
Great answers, thanks.

Michael, I tried yours as-is and with several attempts at modification and couldn't get it to work. If it does indeed work, pls forgive me for leaving you out.

- John
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now