Sumif question

P

Paul Hyett

Is it possible, given a row of mixed values & formulas, to sum just the
numbers that are values, not the numbers that are generated by formulas?
(Excel 2003).

If Sumif can't handle that, is there another way of doing so?
 
J

joeu2004

Is it possible, given a row of mixed values & formulas, to sum just the
numbers that are values, not the numbers that are generated by formulas?
(Excel 2003). If Sumif can't handle that, is there another way of doing so?

Why do you want a formula that sums only constants? What exclude
formulas that return numbers?

Seems like an odd requirement. I'm just wondering if something else
would satisfy your ultimate needs. For example, perhaps you could
simply use Edit > Goto (ctrl-G) > Special, select Constants and Number
(deselect everything else), and look at Sum on the status bar.

Alternatively, you could enter the UDF myHasFormula below and use it
as:

=SUMPRODUCT(--(myHasFormula(A1:A100)=FALSE),A1:A100)

The myHasFormula function can serve many purposes. But of course, you
could simply use the UDF sumConstants below as follows:

=sumConstants(A1:A100)

The UDFs....

Function myHasFormula(rng As Range)
Dim r As Long, c As Long, i As Long, j As Long
If TypeName(rng) = "Range" Then
If rng.Count = 1 Then
myHasFormula = rng.HasFormula
Else
r = rng.Rows.Count
c = rng.Columns.Count
ReDim x(1 To r, 1 To c) As Boolean
For i = 1 To r: For j = 1 To c
x(i, j) = rng.Cells(i, j).HasFormula
Next: Next
myHasFormula = x
End If
Else
myHasFormula = False
End If
End Function

Function sumConstants(rng As Range) As Double
Dim r As Long, c As Long, i As Long, j As Long, s As Double
If TypeName(rng) = "Range" Then
r = rng.Rows.Count
c = rng.Columns.Count
s = 0
For i = 1 To r: For j = 1 To c
If Not rng.Cells(i, j).HasFormula Then
' use IsNumeric(...) instead of
WorksheetFunction.IsNumber(...)
' if you want to include text numbers -- numbers entered
' or formatted as text
If WorksheetFunction.IsNumber(rng.Cells(i, j)) Then s = s +
rng.Cells(i, j)
End If
Next: Next
sumConstants = s
End If
End Function
 
P

Paul Hyett

Why do you want a formula that sums only constants? What exclude
formulas that return numbers?

Seems like an odd requirement.

I don't deny that. :)
I'm just wondering if something else
would satisfy your ultimate needs. For example, perhaps you could
simply use Edit > Goto (ctrl-G) > Special, select Constants and Number
(deselect everything else), and look at Sum on the status bar.

Which would be OK if it were just one, rather than the hundreds of rows
I'd need it for.
Alternatively, you could enter the UDF myHasFormula below and use it
as:

=SUMPRODUCT(--(myHasFormula(A1:A100)=FALSE),A1:A100)

The myHasFormula function can serve many purposes. But of course, you
could simply use the UDF sumConstants below as follows:

=sumConstants(A1:A100)

The UDFs....

Function myHasFormula(rng As Range)
Dim r As Long, c As Long, i As Long, j As Long
If TypeName(rng) = "Range" Then
If rng.Count = 1 Then
myHasFormula = rng.HasFormula
Else
r = rng.Rows.Count
c = rng.Columns.Count
ReDim x(1 To r, 1 To c) As Boolean
For i = 1 To r: For j = 1 To c
x(i, j) = rng.Cells(i, j).HasFormula
Next: Next
myHasFormula = x
End If
Else
myHasFormula = False
End If
End Function

Function sumConstants(rng As Range) As Double
Dim r As Long, c As Long, i As Long, j As Long, s As Double
If TypeName(rng) = "Range" Then
r = rng.Rows.Count
c = rng.Columns.Count
s = 0
For i = 1 To r: For j = 1 To c
If Not rng.Cells(i, j).HasFormula Then
' use IsNumeric(...) instead of
WorksheetFunction.IsNumber(...)
' if you want to include text numbers -- numbers entered
' or formatted as text
If WorksheetFunction.IsNumber(rng.Cells(i, j)) Then s = s +
rng.Cells(i, j)
End If
Next: Next
sumConstants = s
End If
End Function

Thanks for this, but I have never learned VBA, and wouldn't even know
where to start in using it.

Never mind, I knew it was a long shot anyway. :)
 
G

Gord Dibben

Pre-select the hundreds of rows then F5>Special>Constants

Or are the rows non-contiguous?
Thanks for this, but I have never learned VBA, and wouldn't even know
where to start in using it.
Never mind, I knew it was a long shot anyway. :)

Don't just give up. As far as VBA goes, it is about time you started to make
your life easier.

For getting started with VBA see David McRitchie's site.

http://www.mvps.org/dmcritchie/excel/getstarted.htm

Or Ron de Bruin's "where to put code"

http://www.rondebruin.nl/code.htm

In the meantime.....................

FIRST..............make a copy of your original workbook.

Open your copied workbook.

Alt + F11 to open VB Editor. CTRL + r to open Project Explorer.

Select your workbook/project and Insert>Module.

Copy/paste the UDF's macro into that module.

Alt + q to return to Excel.

Save the workbook as a Macro-enabled workbook *.xlsm if running 2007

If running 2003 simply save as *.xls

Enter formula(s) as instructed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top