Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 709
Default sumproduct in Excel Macro

I have a 'sumproduct' statement that works as a function when typed in an
individual cell - but I can't get a similar command to work inside of a Macro

column A contains product type - e.g. radio, car, etc.
column B contains product cost - e.g. $100.00

I want the total cost of all the radios and cars in column B

This formula works in a cell:
=SUMPRODUCT( (A2:A5="radio")*(B2:B5)+(A2:A5="car")*(B2:B5) )

But I can't get similar formula to work in a macro:
Sub test()
total =SUMPRODUCT( (A2:A5="radio")*(B2:B5)+(A2:A5="car")*(B2:B5) )
End Sub

I've tried various things - like defining range variables; using
worksheetfunction.sumproduct; and using variables for "radio" and "car" --
but no luck.

How can I do this in an Excel Macro?
--
Richard
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 380
Default sumproduct in Excel Macro

You cannot just run an Excel function in VBA, they are different beasts.

This should work for you

Sub test()
total =
Activesheet.Evaluate("SUMPRODUCT(((A2:A5=""radio"" )+(A2:A5=""car"")),B2:B5)"
End Sub


--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)

"Richard" wrote in message
...
I have a 'sumproduct' statement that works as a function when typed in an
individual cell - but I can't get a similar command to work inside of a

Macro

column A contains product type - e.g. radio, car, etc.
column B contains product cost - e.g. $100.00

I want the total cost of all the radios and cars in column B

This formula works in a cell:
=SUMPRODUCT( (A2:A5="radio")*(B2:B5)+(A2:A5="car")*(B2:B5) )

But I can't get similar formula to work in a macro:
Sub test()
total =SUMPRODUCT( (A2:A5="radio")*(B2:B5)+(A2:A5="car")*(B2:B5) )
End Sub

I've tried various things - like defining range variables; using
worksheetfunction.sumproduct; and using variables for "radio" and "car" --
but no luck.

How can I do this in an Excel Macro?
--
Richard



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 709
Default sumproduct in Excel Macro

Bob,
Except for having to add a closing parentheses, your statement worked great.

However, it does look like this part of the statement is treated as text:
"SUMPRODUCT(((A2:A5=""radio"")+(A2:A5=""car"")),B2 :B5)"

How would I handle the more general case where the range (i.e. A2:A5), were
really a variable, say myRng1?
And what would I do if my criteria (i.e. radio) were also a variable, say
myCriteria1?

--
Richard


"Bob Phillips" wrote:

You cannot just run an Excel function in VBA, they are different beasts.

This should work for you

Sub test()
total =
Activesheet.Evaluate("SUMPRODUCT(((A2:A5=""radio"" )+(A2:A5=""car"")),B2:B5)"
End Sub


--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)

"Richard" wrote in message
...
I have a 'sumproduct' statement that works as a function when typed in an
individual cell - but I can't get a similar command to work inside of a

Macro

column A contains product type - e.g. radio, car, etc.
column B contains product cost - e.g. $100.00

I want the total cost of all the radios and cars in column B

This formula works in a cell:
=SUMPRODUCT( (A2:A5="radio")*(B2:B5)+(A2:A5="car")*(B2:B5) )

But I can't get similar formula to work in a macro:
Sub test()
total =SUMPRODUCT( (A2:A5="radio")*(B2:B5)+(A2:A5="car")*(B2:B5) )
End Sub

I've tried various things - like defining range variables; using
worksheetfunction.sumproduct; and using variables for "radio" and "car" --
but no luck.

How can I do this in an Excel Macro?
--
Richard




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default sumproduct in Excel Macro

total = ActiveSheet.Evaluate("SUMPRODUCT(((" & myRng1.Address & _
"=""" & myVar1 & """)+(" & myRng1.Address & _
"=""" & myVar2 & """)),B2:B5)")


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Richard" wrote in message
...
Bob,
Except for having to add a closing parentheses, your statement worked

great.

However, it does look like this part of the statement is treated as text:
"SUMPRODUCT(((A2:A5=""radio"")+(A2:A5=""car"")),B2 :B5)"

How would I handle the more general case where the range (i.e. A2:A5),

were
really a variable, say myRng1?
And what would I do if my criteria (i.e. radio) were also a variable, say
myCriteria1?

--
Richard


"Bob Phillips" wrote:

You cannot just run an Excel function in VBA, they are different beasts.

This should work for you

Sub test()
total =

Activesheet.Evaluate("SUMPRODUCT(((A2:A5=""radio"" )+(A2:A5=""car"")),B2:B5)"
End Sub


--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)

"Richard" wrote in message
...
I have a 'sumproduct' statement that works as a function when typed in

an
individual cell - but I can't get a similar command to work inside of

a
Macro

column A contains product type - e.g. radio, car, etc.
column B contains product cost - e.g. $100.00

I want the total cost of all the radios and cars in column B

This formula works in a cell:
=SUMPRODUCT( (A2:A5="radio")*(B2:B5)+(A2:A5="car")*(B2:B5) )

But I can't get similar formula to work in a macro:
Sub test()
total =SUMPRODUCT( (A2:A5="radio")*(B2:B5)+(A2:A5="car")*(B2:B5) )
End Sub

I've tried various things - like defining range variables; using
worksheetfunction.sumproduct; and using variables for "radio" and

"car" --
but no luck.

How can I do this in an Excel Macro?
--
Richard






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default sumproduct in Excel Macro

I would do it this way: You may need to add code to get qty * cost if there
is a column with qty in it.

Sub MyTotals()
Dim i As Integer
Dim total As Double


Myarray = Range("A2:B6")

For i = 1 To UBound(Myarray)
If Trim(Myarray(i, 1)) = "car" Or Trim(Myarray(i, 1)) = "radio" Then
total = total + Myarray(i, 2)
End If
Next i

MsgBox (total)

End Sub




"Richard" wrote in message
...
I have a 'sumproduct' statement that works as a function when typed in an
individual cell - but I can't get a similar command to work inside of a
Macro

column A contains product type - e.g. radio, car, etc.
column B contains product cost - e.g. $100.00

I want the total cost of all the radios and cars in column B

This formula works in a cell:
=SUMPRODUCT( (A2:A5="radio")*(B2:B5)+(A2:A5="car")*(B2:B5) )

But I can't get similar formula to work in a macro:
Sub test()
total =SUMPRODUCT( (A2:A5="radio")*(B2:B5)+(A2:A5="car")*(B2:B5) )
End Sub

I've tried various things - like defining range variables; using
worksheetfunction.sumproduct; and using variables for "radio" and "car" --
but no luck.

How can I do this in an Excel Macro?
--
Richard





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default sumproduct in Excel Macro

Try using evaluate...

total =Evaluate(SUMPRODUCT(
(A2:A5=""radio"")*(B2:B5)+(A2:A5=""car"")*(B2:B5) )")
--
HTH...

Jim Thomlinson


"Richard" wrote:

I have a 'sumproduct' statement that works as a function when typed in an
individual cell - but I can't get a similar command to work inside of a Macro

column A contains product type - e.g. radio, car, etc.
column B contains product cost - e.g. $100.00

I want the total cost of all the radios and cars in column B

This formula works in a cell:
=SUMPRODUCT( (A2:A5="radio")*(B2:B5)+(A2:A5="car")*(B2:B5) )

But I can't get similar formula to work in a macro:
Sub test()
total =SUMPRODUCT( (A2:A5="radio")*(B2:B5)+(A2:A5="car")*(B2:B5) )
End Sub

I've tried various things - like defining range variables; using
worksheetfunction.sumproduct; and using variables for "radio" and "car" --
but no luck.

How can I do this in an Excel Macro?
--
Richard

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SumProduct not Working in a Macro Booey Excel Worksheet Functions 6 March 18th 10 09:13 AM
Sumproduct with Condition OR Sumproduct with ADDRESS function - HE gholly Excel Discussion (Misc queries) 2 September 28th 09 05:07 PM
SUMPRODUCT macro Kim Excel Discussion (Misc queries) 2 September 21st 09 04:36 PM
Conditional SUMPRODUCT or SUMPRODUCT with Filters Ted M H Excel Worksheet Functions 4 August 14th 08 07:50 PM
How to use SUMPRODUCT in macro? Tarek Excel Programming 6 April 27th 05 07:03 AM


All times are GMT +1. The time now is 01:37 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"