mquestion1range has numerical data which should sum if the mTimeCriteria is
found to match.
Dim mTimeCriteria, mPositionCriteria As String
Dim mQuestion1Range, mTimeRange, mPositionRange As Range
Dim mFormula As String
Dim mCount As Long
mTimeCriteria = "First day of employment (Time 1)"
mPositionCriteria = "Registered Nurse"
Set mPositionRange = Worksheets("Data").Range("DataPosition")
Set mTimeRange = Worksheets("Data").Range("DataTime")
Set mQuestion1Range = Worksheets("Data").Range("DataQuestion1")J
MsgBox Evaluate("=SUMPRODUCT( --(mTimeRange= " & mTimeCriteria &
")*(mQuestion1Range) )")
--
Don Guillett
Microsoft MVP Excel
SalesAid Software
dguil...@austin.rr.com
"DogLover" <DogL...@discussions.microsoft.com> wrote in message
news:6DD52602-3896-40BC...@microsoft.com...
Dim mQuestion1Range, mTimeRange, mPositionRange As Range
to
Dim mQuestion1Range as Range
Dim mTimeRange as Range
Dim mPositionRange As Range
--
HTH,
Barb Reinhardt
Not looked too closely at this but what sticks out a mile is the Dim
statements. You may want to specify the data type of each variable
separately, otherwise those not explicitly typed will be variants:
Dim mTimeCriteria, mPositionCriteria As String
Dim mQuestion1Range, mTimeRange, mPositionRange As Range
becomes:
Dim mTimeCriteria As String, mPositionCriteria As
String
Dim mQuestion1Range As Range, mTimeRange As Range, mPositionRange As
Range
Still guessing.. I would have thought that perhaps you're looking for
the likes of this:
MsgBox Evaluate("=SUMPRODUCT(--(" & mTimeRange.Address
& " =""" & mTimeCriteria & """)*" & mQuestion1Range.Address & "
)")
instead of:
MsgBox Evaluate("=SUMPRODUCT( --(mTimeRange= " &
mTimeCriteria & ")*(mQuestion1Range) )")
Have you tried using SumProduct via:
Application.Worksheetfunction.Sumproduct
You supply ranges, strings etc. but you don't have to worry about
multiple quotation marks.
--
p45cal
*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread: http://www.thecodecage.com/forumz/showthread.php?t=155665
[url="http://www.thecodecage.com"]Microsoft Office Help[/url]
> Dim mQuestion1Range, mTimeRange, mPositionRange As Range
That works as-is. But I believe the following is better style, and there
will be situations where it can make a difference.
Dim mQuestion1Range as Range, mTimeRange as Range, mPositionRange As Range
> Set mQuestion1Range = Worksheets("Data").Range("DataQuestion1")J
Remove the "J", an obvious typo.
> MsgBox Evaluate("=SUMPRODUCT( --(mTimeRange= " & mTimeCriteria &
> ")*(mQuestion1Range) )")
Replace with:
MsgBox Evaluate("=SUMPRODUCT( --(" & mTimeRange.Address & "= """ & _
mTimeCriteria & """), " & mQuestion1Range.Address & ")")
The primary problems were the placements of quotes, the failure to use
.Address where appropriate, and the lack of quotes (in the Evaluate string)
around mTimeCriteria.
I replaced effectively "*(mQuestion1Range.Address)" with
",mQuestion1Range.Address". That is optional an style issue, a personal
choice if the mQuestion1Range.Address range has only numbers and truly
empty cells (i.e. no formula and no constant).
----- original message -----
"DogLover" <DogL...@discussions.microsoft.com> wrote in message
news:6DD52602-3896-40BC...@microsoft.com...
I wanted to make the same suggestion. But I could not make the syntax work
for the exact logic that DogLover wanted to evaluate.
Can you provide a working example using WorksheetFunction.SumProduct?
The working example must evaluate the following Excel equivalent, using the
variables in DogLover's posting:
=SUMPRODUCT(--(DataTime="First day..."),DataQuestion1)
where DataTime and DataQuestion1 are named ranges.
----- original message -----
"p45cal" <p45cal...@thecodecage.com> wrote in message
news:p45cal...@thecodecage.com...
Private Sub VBATEST_Click()
Dim mTimeCriteria, Var As Variant
Dim mQuestion1Range As Range
Dim mTimeRange As Range
mTimeCriteria = "First day of employment (Time 1)"
Set mTimeRange = Worksheets("Data").Range("DataTime")
Set mQuestion1Range = Worksheets("Data").Range("DataQuestion1")
mTimeCriteria = """" & mTimeCriteria & """"
' Var = Application.Evaluate("SUMPRODUCT((Data!G3:A250 =" & mTimeCriteria &
")*(Data!M3:M250))")
Var = Application.Evaluate("SUMPRODUCT((Data!G3:A250 =" & mTimeCriteria &
")*(mQuestion1Range))")
MsgBox (Var)
End Sub
"Don Guillett" wrote:
> .
>
End Sub
Notice that I used the with/end with structure -- even with the .evaluate
method. That means that the unqualified addresses (no workbook/worksheet names
included) will refer to that worksheet in the With statement.
If I had used Application.evaluate() or just Evaluate(), then the addresses
would have referred to the activesheet (if the code was in a General module).
I could have used:
mFormula = "SUMPRODUCT(--(" & mTimeRange.Address(external:=true) _
& "=" & Chr(34) & mTimeCriteria & Chr(34) _
& ")," & mQuestion1Range.Address(external:=true & ")"
res = application.evaluate(mFormula)
ps. Notice that those strings have to be surrounded by double quotes--just like
in a formula in a cell:
=SUMPRODUCT(--(a1:a10="First day of employment (Time 1)"),b1:b10)
--
Dave Peterson
Dim mTimeCriteria
Dim Var As Variant
Dim mQuestion1Range As Range
Dim mTimeRange As Range
mTimeCriteria = "First day of employment (Time 1)"
Set mTimeRange = Worksheets("Data").Range("DataTime")
Set mQuestion1Range = Worksheets("Data").Range("DataQuestion1")
MsgBox Evaluate("=SUMPRODUCT(--(" & mTimeRange.Address & " =""" &
mTimeCriteria & """)*" & mQuestion1Range.Address & " )")
End Sub
"p45cal" wrote:
> [url="http://www.thecodecage.com"]Microsoft Office Help[/url]
>
> .
>
Sticking with Evaluate you could simplify the code by not having to set
variables to ranges by using the existing names directly in the formula,
as you would on the sheet:
MsgBox Evaluate("=SUMPRODUCT(--(DataTime=""" &
mTimeCriteria & """)*DataQuestion1)")
So after removing everything extraneous to the evaluate line all you
need is:
Dim mTimeCriteria As String
mTimeCriteria = "First day of employment (Time 1)"
MsgBox Evaluate("=SUMPRODUCT(--(DataTime=""" & mTimeCriteria &
""")*DataQuestion1)")
--
p45cal
*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread: http://www.thecodecage.com/forumz/showthread.php?t=155665
[url="http://www.thecodecage.com"]Microsoft Office Help[/url]
"Dave Peterson" wrote:
> .
>
Here is what I'm trying to do, but it suggest I am missing a parenthesis,
and just don't see it. What would the formula be to add an additional
criteria like this?
mFormula = "SUMPRODUCT(--(" & mTimeRange.Address _
& "=" & Chr(34) & mTimeCriteria & Chr(34) & ")," _
--(" & mPositionRange.Address _
& "=" & Chr(34) & mPositionCriteria & Chr(34) &
")," _
& mQuestion1Range.Address & ")"
mFormula = "SUMPRODUCT(--(" & mTimeRange.Address _
& "=" & Chr(34) & mTimeCriteria & Chr(34) & ")," _
& "--(" & mPositionRange.Address _
--
Dave Peterson