Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I've 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 as String (either 4 or 5 characters long), CliStat as Integer (0, 1, or 99), and the rest as Ranges. I've tried adjusting the syntax in every way I can think of and 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. |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
If you were writing the formula in a cell, it would look something like:
=SUMPRODUCT((HoursCodes="A")*(HourStat="x")*Period Hours) Since both the classcode and clistat values are strings, they need to be surrounded by double quotes. =chr(34) is a double quote in VBA land. Option Explicit Sub testme() Dim TotalHrs As Double 'don't use Single Dim ClassCode As String Dim CLIStat As String Dim myFormula As String ClassCode = "A" CLIStat = "x" '=SUMPRODUCT((HourCodes="a")*(HourStat="x")*Period Hours) With Worksheets("Hours") myFormula = "SUMPRODUCT(" _ & "(HoursCodes=" & Chr(34) & ClassCode & Chr(34) & ")" _ & "*(HourStat=" & Chr(34) & CLIStat & Chr(34) & ")" _ & "*PeriodHours)" TotalHrs = .Evaluate(myFormula) End With MsgBox TotalHrs End Sub I find this more difficult to read (most the time!), but you could use it if you like it better: myFormula = "SUMPRODUCT(" _ & "(HoursCodes=""" & ClassCode & """)" _ & "*(HourStat=""" & CLIStat & """)" _ & "*PeriodHours)" ====== ps. The reason I used a separate string variable for the formula is so that I could step through the code, debug.print the formula to the immediate window and copy|Paste into a cell in excel proper. Lots of times, I like to get excel's help with my formula typing errors. pps. There's no reason ever to use Single or Integer. Always use Double and Long. Modern computers will have less work to do and you don't have to worry about overflow errors. Luke wrote: I've 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 as String (either 4 or 5 characters long), CliStat as Integer (0, 1, or 99), and the rest as Ranges. I've tried adjusting the syntax in every way I can think of and 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. -- Dave Peterson |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I'm still getting the type-mismatch error, although now it is at the
"TotalHrs = .Evaluate(myFormula)" line. CLIStat is actually an Integer (0, 1, or 99), or Long if you prefer. Will that make a difference in the formula you gave? I did change TotalHrs to Double and CLIStat to Long, however. "Dave Peterson" wrote: If you were writing the formula in a cell, it would look something like: =SUMPRODUCT((HoursCodes="A")*(HourStat="x")*Period Hours) Since both the classcode and clistat values are strings, they need to be surrounded by double quotes. =chr(34) is a double quote in VBA land. Option Explicit Sub testme() Dim TotalHrs As Double 'don't use Single Dim ClassCode As String Dim CLIStat As String Dim myFormula As String ClassCode = "A" CLIStat = "x" '=SUMPRODUCT((HourCodes="a")*(HourStat="x")*Period Hours) With Worksheets("Hours") myFormula = "SUMPRODUCT(" _ & "(HoursCodes=" & Chr(34) & ClassCode & Chr(34) & ")" _ & "*(HourStat=" & Chr(34) & CLIStat & Chr(34) & ")" _ & "*PeriodHours)" TotalHrs = .Evaluate(myFormula) End With MsgBox TotalHrs End Sub I find this more difficult to read (most the time!), but you could use it if you like it better: myFormula = "SUMPRODUCT(" _ & "(HoursCodes=""" & ClassCode & """)" _ & "*(HourStat=""" & CLIStat & """)" _ & "*PeriodHours)" ====== ps. The reason I used a separate string variable for the formula is so that I could step through the code, debug.print the formula to the immediate window and copy|Paste into a cell in excel proper. Lots of times, I like to get excel's help with my formula typing errors. pps. There's no reason ever to use Single or Integer. Always use Double and Long. Modern computers will have less work to do and you don't have to worry about overflow errors. Luke wrote: I've 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 as String (either 4 or 5 characters long), CliStat as Integer (0, 1, or 99), and the rest as Ranges. I've tried adjusting the syntax in every way I can think of and 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. -- Dave Peterson . |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
If CLIStat is a number, then remove the double quotes from that portion of the
formula. But that won't make the formula cause an error like this. It just won't find a match, so those rows will be ignored/treated as 0. I'm guessing that at least one of those ranges contains errors. Can you get the equivalent formula to work in a worksheet cell (just do it manually)? If you do, then share that formula in the follow-up. Luke wrote: I'm still getting the type-mismatch error, although now it is at the "TotalHrs = .Evaluate(myFormula)" line. CLIStat is actually an Integer (0, 1, or 99), or Long if you prefer. Will that make a difference in the formula you gave? I did change TotalHrs to Double and CLIStat to Long, however. "Dave Peterson" wrote: If you were writing the formula in a cell, it would look something like: =SUMPRODUCT((HoursCodes="A")*(HourStat="x")*Period Hours) Since both the classcode and clistat values are strings, they need to be surrounded by double quotes. =chr(34) is a double quote in VBA land. Option Explicit Sub testme() Dim TotalHrs As Double 'don't use Single Dim ClassCode As String Dim CLIStat As String Dim myFormula As String ClassCode = "A" CLIStat = "x" '=SUMPRODUCT((HourCodes="a")*(HourStat="x")*Period Hours) With Worksheets("Hours") myFormula = "SUMPRODUCT(" _ & "(HoursCodes=" & Chr(34) & ClassCode & Chr(34) & ")" _ & "*(HourStat=" & Chr(34) & CLIStat & Chr(34) & ")" _ & "*PeriodHours)" TotalHrs = .Evaluate(myFormula) End With MsgBox TotalHrs End Sub I find this more difficult to read (most the time!), but you could use it if you like it better: myFormula = "SUMPRODUCT(" _ & "(HoursCodes=""" & ClassCode & """)" _ & "*(HourStat=""" & CLIStat & """)" _ & "*PeriodHours)" ====== ps. The reason I used a separate string variable for the formula is so that I could step through the code, debug.print the formula to the immediate window and copy|Paste into a cell in excel proper. Lots of times, I like to get excel's help with my formula typing errors. pps. There's no reason ever to use Single or Integer. Always use Double and Long. Modern computers will have less work to do and you don't have to worry about overflow errors. Luke wrote: I've 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 as String (either 4 or 5 characters long), CliStat as Integer (0, 1, or 99), and the rest as Ranges. I've tried adjusting the syntax in every way I can think of and 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. -- Dave Peterson . -- Dave Peterson |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
"Luke" wrote:
TotalHrs = .Evaluate("SUMPRODUCT((HoursCodes=ClassCode)* _ (HourStat=CliStat)*PeriodHours)") End With TotalHrs is dim'd as Single, ClassCode as String (either 4 or 5 characters long), CliStat as Integer (0, 1, or 99), and the rest as Ranges. You cannot reference VBA variables directly in the quoted string passed to Evaluate. This is error-prone and difficult to read. So it is prudent to build the Evaluate argument in a string variable that you can display in debug mode or print to the Immediate window. For example: Dim sEval as String sEval = "SUMPRODUCT((" & HoursCodes.Address & "=" & _ ClassCode & ")*(" & HourStat.Address & "=" & CliStat & ")," _ & PeriodHours.Address & ")" TotalHrs = .Evaluate(sEval) Aside.... TotalHrs should probably be type Double, not Single. Generally, all floating point variables should be type Double, especially if some of them will be stored into or compared with worksheet values. The reason is complicated to explain fully. In a nutshell, the conversion from Single to Double is imperfect. ----- original message ----- "Luke" wrote: I've 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 as String (either 4 or 5 characters long), CliStat as Integer (0, 1, or 99), and the rest as Ranges. I've tried adjusting the syntax in every way I can think of and 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. |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Errata....
I wrote: Dim sEval as String sEval = "SUMPRODUCT((" & HoursCodes.Address & "=" & _ ClassCode & ")*(" & HourStat.Address & "=" & CliStat & ")," _ & PeriodHours.Address & ")" TotalHrs = .Evaluate(sEval) First, I neglected to notice that ClassCode is a String variable. So its value needs to be quoted in the Evaluate string, just as it would be quoted in the SUMPRODUCT expression in the worksheet. To wit: sEval = "SUMPRODUCT((" & HoursCodes.Address & "=""" & _ ClassCode & """)*(" & HourStat.Address & "=" & CliStat & ")," _ & PeriodHours.Address & ")" Second, I did not realize that Dave had posted nearly the same idea. The key difference is the need for .Address when referencing the Range variables. Also note that CliStat is not quoted. I assume that the values in the HourStat range are numeric, not text. ----- original message ----- "Joe User" wrote: "Luke" wrote: TotalHrs = .Evaluate("SUMPRODUCT((HoursCodes=ClassCode)* _ (HourStat=CliStat)*PeriodHours)") End With TotalHrs is dim'd as Single, ClassCode as String (either 4 or 5 characters long), CliStat as Integer (0, 1, or 99), and the rest as Ranges. You cannot reference VBA variables directly in the quoted string passed to Evaluate. This is error-prone and difficult to read. So it is prudent to build the Evaluate argument in a string variable that you can display in debug mode or print to the Immediate window. For example: Dim sEval as String sEval = "SUMPRODUCT((" & HoursCodes.Address & "=" & _ ClassCode & ")*(" & HourStat.Address & "=" & CliStat & ")," _ & PeriodHours.Address & ")" TotalHrs = .Evaluate(sEval) Aside.... TotalHrs should probably be type Double, not Single. Generally, all floating point variables should be type Double, especially if some of them will be stored into or compared with worksheet values. The reason is complicated to explain fully. In a nutshell, the conversion from Single to Double is imperfect. ----- original message ----- "Luke" wrote: I've 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 as String (either 4 or 5 characters long), CliStat as Integer (0, 1, or 99), and the rest as Ranges. I've tried adjusting the syntax in every way I can think of and 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. |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
The sweet smell of success! Thank you so very much. It was the ".Address"
part that did it. I also used Dave's suggestion for the Chr(34) instead of the double quotes as my eyes start having troubles when all those quotes marks get together. Whew. No more For-Next loops evaluating each of my 1265 rows of data. Thanks all. "Joe User" wrote: Errata.... I wrote: Dim sEval as String sEval = "SUMPRODUCT((" & HoursCodes.Address & "=" & _ ClassCode & ")*(" & HourStat.Address & "=" & CliStat & ")," _ & PeriodHours.Address & ")" TotalHrs = .Evaluate(sEval) First, I neglected to notice that ClassCode is a String variable. So its value needs to be quoted in the Evaluate string, just as it would be quoted in the SUMPRODUCT expression in the worksheet. To wit: sEval = "SUMPRODUCT((" & HoursCodes.Address & "=""" & _ ClassCode & """)*(" & HourStat.Address & "=" & CliStat & ")," _ & PeriodHours.Address & ")" Second, I did not realize that Dave had posted nearly the same idea. The key difference is the need for .Address when referencing the Range variables. Also note that CliStat is not quoted. I assume that the values in the HourStat range are numeric, not text. ----- original message ----- "Joe User" wrote: "Luke" wrote: TotalHrs = .Evaluate("SUMPRODUCT((HoursCodes=ClassCode)* _ (HourStat=CliStat)*PeriodHours)") End With TotalHrs is dim'd as Single, ClassCode as String (either 4 or 5 characters long), CliStat as Integer (0, 1, or 99), and the rest as Ranges. You cannot reference VBA variables directly in the quoted string passed to Evaluate. This is error-prone and difficult to read. So it is prudent to build the Evaluate argument in a string variable that you can display in debug mode or print to the Immediate window. For example: Dim sEval as String sEval = "SUMPRODUCT((" & HoursCodes.Address & "=" & _ ClassCode & ")*(" & HourStat.Address & "=" & CliStat & ")," _ & PeriodHours.Address & ")" TotalHrs = .Evaluate(sEval) Aside.... TotalHrs should probably be type Double, not Single. Generally, all floating point variables should be type Double, especially if some of them will be stored into or compared with worksheet values. The reason is complicated to explain fully. In a nutshell, the conversion from Single to Double is imperfect. ----- original message ----- "Luke" wrote: I've 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 as String (either 4 or 5 characters long), CliStat as Integer (0, 1, or 99), and the rest as Ranges. I've tried adjusting the syntax in every way I can think of and 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. |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I thought that HoursCode and HourStat were both range names in the worksheet.
Sorry. Luke wrote: The sweet smell of success! Thank you so very much. It was the ".Address" part that did it. I also used Dave's suggestion for the Chr(34) instead of the double quotes as my eyes start having troubles when all those quotes marks get together. Whew. No more For-Next loops evaluating each of my 1265 rows of data. Thanks all. "Joe User" wrote: Errata.... I wrote: Dim sEval as String sEval = "SUMPRODUCT((" & HoursCodes.Address & "=" & _ ClassCode & ")*(" & HourStat.Address & "=" & CliStat & ")," _ & PeriodHours.Address & ")" TotalHrs = .Evaluate(sEval) First, I neglected to notice that ClassCode is a String variable. So its value needs to be quoted in the Evaluate string, just as it would be quoted in the SUMPRODUCT expression in the worksheet. To wit: sEval = "SUMPRODUCT((" & HoursCodes.Address & "=""" & _ ClassCode & """)*(" & HourStat.Address & "=" & CliStat & ")," _ & PeriodHours.Address & ")" Second, I did not realize that Dave had posted nearly the same idea. The key difference is the need for .Address when referencing the Range variables. Also note that CliStat is not quoted. I assume that the values in the HourStat range are numeric, not text. ----- original message ----- "Joe User" wrote: "Luke" wrote: TotalHrs = .Evaluate("SUMPRODUCT((HoursCodes=ClassCode)* _ (HourStat=CliStat)*PeriodHours)") End With TotalHrs is dim'd as Single, ClassCode as String (either 4 or 5 characters long), CliStat as Integer (0, 1, or 99), and the rest as Ranges. You cannot reference VBA variables directly in the quoted string passed to Evaluate. This is error-prone and difficult to read. So it is prudent to build the Evaluate argument in a string variable that you can display in debug mode or print to the Immediate window. For example: Dim sEval as String sEval = "SUMPRODUCT((" & HoursCodes.Address & "=" & _ ClassCode & ")*(" & HourStat.Address & "=" & CliStat & ")," _ & PeriodHours.Address & ")" TotalHrs = .Evaluate(sEval) Aside.... TotalHrs should probably be type Double, not Single. Generally, all floating point variables should be type Double, especially if some of them will be stored into or compared with worksheet values. The reason is complicated to explain fully. In a nutshell, the conversion from Single to Double is imperfect. ----- original message ----- "Luke" wrote: I've 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 as String (either 4 or 5 characters long), CliStat as Integer (0, 1, or 99), and the rest as Ranges. I've tried adjusting the syntax in every way I can think of and 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. -- Dave Peterson |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Help. Sumproduct Data Type Mismatch Erro | Excel Programming | |||
Sumproduct in VBA Type Mismatch problem | Excel Programming | |||
runtime error 13 - type mismatch error in Excel 97 on Citrix | Excel Programming | |||
Help: Compile error: type mismatch: array or user defined type expected | Excel Programming | |||
Befuddled with For Next Loop ------ Run - Time Error '13' Type Mismatch Error | Excel Programming |