Excel - Sumproduct formula works in spreadsheet, NOT VBA. Help please
Asked By DogLover
20-Nov-09 09:35 PM

Basically, I want to replace this formula with VBA Code
I can get this one to work to sum
=SUMPRODUCT(--(DataTime="First day of employment (Time
1)"),--(DataPosition=N6),(DataQuestion1)
When I change this to Count rather than Sum, my Code Evaluates as an Error
=SUMPRODUCT(--(DataTime="First day of employment (Time
1)"),--(DataPosition="Registered Nurse"),--(DataQuestion1<>"*"))
The only line of code I switch out is the last mFormula row. The line of
code that works I have commented out. I am really stumped...
This is the code that I have..
Dim mTimeCriteria As String
Dim mPositionCriteria As String
Dim mQuestion1Criteria As String
Dim mTimeRange As Range
Dim mPositionRange As Range
Dim mQuestion1Range As Range
Dim mFormula As String
Dim Kountifs As Variant 'could be an error
mTimeCriteria = "First day of employment (Time 1)"
mPositionCriteria = "Registered Nurse"
mQuestion1Criteria = "*"
With Worksheets("Data")
Set mTimeRange = .Range("DataTime")
Set mPositionRange = .Range("DataPosition")
Set mQuestion1Range = .Range("DataQuestion1")
mFormula = "SUMPRODUCT(--(" & mTimeRange.Address & "=" & Chr(34) &
mTimeCriteria & Chr(34) & "),"
mFormula = mFormula & "--(" & mPositionRange.Address & "=" & Chr(34) &
mPositionCriteria & Chr(34) & "),"
mFormula = mFormula & "-- (" & mQuestion1Range.Address & "<>" & Chr(34)
& mQuestion1Criteria & Chr(34) & ") "
'mFormula = mFormula & mQuestion1Range.Address & ")" 'This sums
correctly
' MsgBox mFormula
Kountifs = .Evaluate(mFormula)
End With
If IsError(Kountifs) Then
MsgBox "Error in evaluating"
Else
MsgBox Kountifs
End If
Worksheets
(1)
Error
(1)
SUMPRODUCT
(1)
VBA
(1)
MQuestion1Range.Address
(1)
MPositionRange.Address
(1)
MTimeRange.Address
(1)
MQuestion1Criteria
(1)
Per Jessen replied to DogLover
Hi
You miss a closing paranthesis in your formula.
Regards,
Per
DogLover replied to Per Jessen
Dave Peterson replied to DogLover
Check your other post.
In fact, if you are going to repost your question, please go back to the other
threads and say that you reposted.
That way, others will not waste their time duplicating answers.
--
Dave Peterson
Sumproject Run-time Error 13 Excel I have this code and am having trouble with the Sumproduct working. I get a Run-Time Error 13 and Type Mismatch message? Can anyone help. Dim mTimeCriteria As String Dim mQuestion1Range As Range, mTimeRange As Range mTimeCriteria = "First day of employment (Time 1)" Set mTimeRange = Worksheets("Data").Range("DataTime") Set mQuestion1Range = Worksheets("Data").Range("DataQuestion1") mTimeCriteria = """" & mTimeCriteria & """" MsgBox Evaluate(" = SUMPRODUCT( - -(" & mTimeRange.Address & " = """ & _ mTimeCriteria & """), " & mQuestion1Range.Address & ")") Excel Programming Discussions WorksheetFunction (1) WorksheetFunction.SumIf (1) Worksheets (1) Error (1) SUMPRODUCT (1) VBA (1) MQuestion1Range.Address (1) MTimeRange.Address (1) Hi I tried to
Sumproduct error Excel I've got 2 worksheets in 1 workbook, one labelled PO and one labelled OCR. In the PO worksheet, I've entered the following formula: = SUMPRODUCT(- -(OCR!A2:A65536 = C4), - -(OCR!D2:D65536 = A1), - -(OCR!V2:V65536)) It's only returning the data manually). I must be missing something with my formula - is there an obvious error? Thanks Excel Miscellaneous Discussions SUMPRODUCT (1) Worksheet (1) Workbook (1) Sarah (1) Thenumbers (1) Isnumber (1) Mixture (1) £251 to integers and it works fine now!! Many thanks! For a well explained use of SUMPRODUCT look here http: / / www.xldynamic.com / source / xld.SUMPRODUCT.html - - The Code Cage Team Regards, The Code Cage Team http: / / www.thecodecage.com - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - The
Type mismatch error using Sumproduct Excel I have searched through similar topics and tried several variations of code, but I keep getting a type-mismatch error when trying to use Sumproduct. I am wanting to use this in several parts of my code. The first instance which is also where the error keeps occurring) is below. With Worksheets("Hours") TotalHrs = .Evaluate("SUMPRODUCT((HoursCodes = ClassCode)* _ (HourStat = CliStat)*PeriodHours)") End With TotalHrs is dim'd as Single, ClassCode based on other examples I could find, but each time I get the type-mismatch error. Do I just have something dim'd incorrectly? Thanks for time and help. Excel Programming
another sumproduct with #value error. . . Excel Hello, I am trying to compare 7 columns in two worksheets, containing text, numbers and dates. I found here a very helpful formula with sumproduct that woked on my trial spreadsheet, but returns a #value error on the real thing. The formula is SUMPRODUCT(- -(Sheet2!$A$1:$A10 = A1), - -(Sheet2!$B$1:$B10 = B1), - -(Sheet2!$C$1:$C10 = C1 1:$G$10) Any ideas what I am doing wrong? Thanks, Maya Excel Worksheet Discussions SUMPRODUCT (1) Subscribers (1) NumsTry (1) Xdemechanik (1) Nums (1) Viz (1) Try the below. The range should be same = SUMPRODUCT(- -(Sheet2!$A$1:$A10 = A1), - -(Sheet2!$B$1:$B10 = B1), - -(Sheet2!$C$1:$C10 = C1