Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting SumIf values with VBA

Posted on 2010-11-17
4
Medium Priority
?
655 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 1000 total points
ID: 34161175
cel.formula = "=sumif(Allconfigs," & str & ",AllJ)"
cel.value = cel.value
0
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 34161368
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 1000 total points
ID: 34161679
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
ID: 34167660
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

722 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