Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 130
Default worksheetfunction question

Sub rework()
Dim ans As Variant
Dim ans2 As Variant
Dim var As Variant

ans = InputBox("Which advisor do you wish to use?")
ans2 = InputBox("Which payment method do you wish to use?")

var = WorksheetFunction.SumProduct(--(Range("A1:A10000") = ans),
--(Range("H1:H10000") = ans2))

MsgBox (var & " cases match your criteria.")
End Sub

I'm trying to work out how to use WorksheetFunction so I can make some
macro's that can calculate data without having to enter the formulas into the
spreadsheet. I tried off with .Index and got that to work quite easily, but
..SumProduct would be the most useful for me but i'm unsure how to make them
different to than if I was typing the formula into a cell...
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default worksheetfunction question

try this. Sumproduct does NOT work like other functions.

Sub rework()
Dim ans As Variant
Dim ans2 As Variant
Dim var As Variant

ans = InputBox("Which advisor do you wish to use?")
ans2 = InputBox("Which payment method do you wish to use?")
var = Application.Evaluate("SUMPRODUCT((A1:A10000 =" & ans & ")*(H1:H10000="
& ans2 & "))")
MsgBox (var & " cases match your criteria.")
'Range("j1") = var
End Sub

--
Don Guillett
SalesAid Software

"PaulW" wrote in message
...
Sub rework()
Dim ans As Variant
Dim ans2 As Variant
Dim var As Variant

ans = InputBox("Which advisor do you wish to use?")
ans2 = InputBox("Which payment method do you wish to use?")

var = WorksheetFunction.SumProduct(--(Range("A1:A10000") = ans),
--(Range("H1:H10000") = ans2))

MsgBox (var & " cases match your criteria.")
End Sub

I'm trying to work out how to use WorksheetFunction so I can make some
macro's that can calculate data without having to enter the formulas into
the
spreadsheet. I tried off with .Index and got that to work quite easily,
but
.SumProduct would be the most useful for me but i'm unsure how to make
them
different to than if I was typing the formula into a cell...



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 130
Default worksheetfunction question

var = Application.Evaluate("SUMPRODUCT((A1:A10000 =" & ans & ")*(H1:H10000="
& ans2 & "))")


Tried, this seems to error, although the code doesn't debug until the next
line when it tries to show the msgbox and gives me error 13 as type mismatch.
Debugging it and having a look shows me that this line gives the variable
"var" the value "Error 2029". Did a quick look on google, and found a page
that said that this was a problem which sometimes occured on Excel 2002
normal edition, which confused me further since i'm using Excel 2003
Professional Edition.

Do I need to alter something in VBA to get this to work? (I know theres
loads of references that I might need to activate some of these to get
certain things to work)

Also, you mentioned that Sumproduct doesn't work like other functions. Does
this mean that Worksheetfunction.SumProduct doesn't work at all? or only when
i'm doing an actual Sumproduct rather than converting statements that produce
arrays of True/False into 0/1s?
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default worksheetfunction question

Error 2029 is a NAME error, so you must be passing a string/alpha numeric
response to the inputbox. If you pass an alpha numeric answer to both of
the input boxes rather than a number you would need the code to be:

Sub rework()
Dim ans As Variant
Dim ans2 As Variant
Dim var As Variant

ans = InputBox("Which advisor do you wish to use?")
ans2 = InputBox("Which payment method do you wish to use?")
var = Application.Evaluate("SUMPRODUCT((A1:A10000 =""" _
& ans & """)*(H1:H10000=""" & ans2 & """))")
MsgBox (var & " cases match your criteria.")
'Range("j1") = var
End Sub

this produces a formula to be evaluated like this:
SUMPRODUCT((A1:A10="AAA")*(H1:H10="HHH"))

If one of them will be numeric, then you will need to remove the extra
quotes for that answer.

--
Regards,
Tom Ogilvy


"PaulW" wrote:

var = Application.Evaluate("SUMPRODUCT((A1:A10000 =" & ans & ")*(H1:H10000="
& ans2 & "))")


Tried, this seems to error, although the code doesn't debug until the next
line when it tries to show the msgbox and gives me error 13 as type mismatch.
Debugging it and having a look shows me that this line gives the variable
"var" the value "Error 2029". Did a quick look on google, and found a page
that said that this was a problem which sometimes occured on Excel 2002
normal edition, which confused me further since i'm using Excel 2003
Professional Edition.

Do I need to alter something in VBA to get this to work? (I know theres
loads of references that I might need to activate some of these to get
certain things to work)

Also, you mentioned that Sumproduct doesn't work like other functions. Does
this mean that Worksheetfunction.SumProduct doesn't work at all? or only when
i'm doing an actual Sumproduct rather than converting statements that produce
arrays of True/False into 0/1s?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 130
Default worksheetfunction question

I'm hoping to use this at some point in a userform, on any sheet a user
wants. This means that it needs to be able to work on values and numbers.

I was using the example of "AS" for advisor, and "SO" for payment method, so
thats why Don's suggestion failed. I then changed the columns to look at a
date, and a payment amount, and input 39098 for ans, and 20 for ans2, this
produced an error on yours, which I obviously expected due to the last line
in your post.

Would it be possible for this to work on Numeric and a String? or should I
put this 4 times (with and without extra quotes dependant differently for
each one), and get the code to choose the appropriate one depending on if
each variable is a String/Numeric?

"Tom Ogilvy" wrote:

Error 2029 is a NAME error, so you must be passing a string/alpha numeric
response to the inputbox. If you pass an alpha numeric answer to both of
the input boxes rather than a number you would need the code to be:

Sub rework()
Dim ans As Variant
Dim ans2 As Variant
Dim var As Variant

ans = InputBox("Which advisor do you wish to use?")
ans2 = InputBox("Which payment method do you wish to use?")
var = Application.Evaluate("SUMPRODUCT((A1:A10000 =""" _
& ans & """)*(H1:H10000=""" & ans2 & """))")
MsgBox (var & " cases match your criteria.")
'Range("j1") = var
End Sub

this produces a formula to be evaluated like this:
SUMPRODUCT((A1:A10="AAA")*(H1:H10="HHH"))

If one of them will be numeric, then you will need to remove the extra
quotes for that answer.

--
Regards,
Tom Ogilvy


"PaulW" wrote:

var = Application.Evaluate("SUMPRODUCT((A1:A10000 =" & ans & ")*(H1:H10000="
& ans2 & "))")


Tried, this seems to error, although the code doesn't debug until the next
line when it tries to show the msgbox and gives me error 13 as type mismatch.
Debugging it and having a look shows me that this line gives the variable
"var" the value "Error 2029". Did a quick look on google, and found a page
that said that this was a problem which sometimes occured on Excel 2002
normal edition, which confused me further since i'm using Excel 2003
Professional Edition.

Do I need to alter something in VBA to get this to work? (I know theres
loads of references that I might need to activate some of these to get
certain things to work)

Also, you mentioned that Sumproduct doesn't work like other functions. Does
this mean that Worksheetfunction.SumProduct doesn't work at all? or only when
i'm doing an actual Sumproduct rather than converting statements that produce
arrays of True/False into 0/1s?



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default worksheetfunction question

Try this one:

Sub rework()
Dim ans As Variant
Dim ans2 As Variant
Dim var As Variant

ans = InputBox("Which advisor do you wish to use?")
ans2 = InputBox("Which payment method do you wish to use?")
if not isnumeric(ans) then
ans = """" & ans & """"
end if
if not isnumeric(ans2) then
ans2 = """" & ans & """"
End if
var = Application.Evaluate("SUMPRODUCT((A1:A10000 =" _
& ans & ")*(H1:H10000=" & ans2 & "))")
MsgBox (var & " cases match your criteria.")
'Range("j1") = var
End Sub

--
Regards,
Tom Ogilvy

"PaulW" wrote:

I'm hoping to use this at some point in a userform, on any sheet a user
wants. This means that it needs to be able to work on values and numbers.

I was using the example of "AS" for advisor, and "SO" for payment method, so
thats why Don's suggestion failed. I then changed the columns to look at a
date, and a payment amount, and input 39098 for ans, and 20 for ans2, this
produced an error on yours, which I obviously expected due to the last line
in your post.

Would it be possible for this to work on Numeric and a String? or should I
put this 4 times (with and without extra quotes dependant differently for
each one), and get the code to choose the appropriate one depending on if
each variable is a String/Numeric?

"Tom Ogilvy" wrote:

Error 2029 is a NAME error, so you must be passing a string/alpha numeric
response to the inputbox. If you pass an alpha numeric answer to both of
the input boxes rather than a number you would need the code to be:

Sub rework()
Dim ans As Variant
Dim ans2 As Variant
Dim var As Variant

ans = InputBox("Which advisor do you wish to use?")
ans2 = InputBox("Which payment method do you wish to use?")
var = Application.Evaluate("SUMPRODUCT((A1:A10000 =""" _
& ans & """)*(H1:H10000=""" & ans2 & """))")
MsgBox (var & " cases match your criteria.")
'Range("j1") = var
End Sub

this produces a formula to be evaluated like this:
SUMPRODUCT((A1:A10="AAA")*(H1:H10="HHH"))

If one of them will be numeric, then you will need to remove the extra
quotes for that answer.

--
Regards,
Tom Ogilvy


"PaulW" wrote:

var = Application.Evaluate("SUMPRODUCT((A1:A10000 =" & ans & ")*(H1:H10000="
& ans2 & "))")

Tried, this seems to error, although the code doesn't debug until the next
line when it tries to show the msgbox and gives me error 13 as type mismatch.
Debugging it and having a look shows me that this line gives the variable
"var" the value "Error 2029". Did a quick look on google, and found a page
that said that this was a problem which sometimes occured on Excel 2002
normal edition, which confused me further since i'm using Excel 2003
Professional Edition.

Do I need to alter something in VBA to get this to work? (I know theres
loads of references that I might need to activate some of these to get
certain things to work)

Also, you mentioned that Sumproduct doesn't work like other functions. Does
this mean that Worksheetfunction.SumProduct doesn't work at all? or only when
i'm doing an actual Sumproduct rather than converting statements that produce
arrays of True/False into 0/1s?

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 130
Default worksheetfunction question

Cheers, works great

"Tom Ogilvy" wrote:

Try this one:

Sub rework()
Dim ans As Variant
Dim ans2 As Variant
Dim var As Variant

ans = InputBox("Which advisor do you wish to use?")
ans2 = InputBox("Which payment method do you wish to use?")
if not isnumeric(ans) then
ans = """" & ans & """"
end if
if not isnumeric(ans2) then
ans2 = """" & ans & """"
End if
var = Application.Evaluate("SUMPRODUCT((A1:A10000 =" _
& ans & ")*(H1:H10000=" & ans2 & "))")
MsgBox (var & " cases match your criteria.")
'Range("j1") = var
End Sub

--
Regards,
Tom Ogilvy

"PaulW" wrote:

I'm hoping to use this at some point in a userform, on any sheet a user
wants. This means that it needs to be able to work on values and numbers.

I was using the example of "AS" for advisor, and "SO" for payment method, so
thats why Don's suggestion failed. I then changed the columns to look at a
date, and a payment amount, and input 39098 for ans, and 20 for ans2, this
produced an error on yours, which I obviously expected due to the last line
in your post.

Would it be possible for this to work on Numeric and a String? or should I
put this 4 times (with and without extra quotes dependant differently for
each one), and get the code to choose the appropriate one depending on if
each variable is a String/Numeric?

"Tom Ogilvy" wrote:

Error 2029 is a NAME error, so you must be passing a string/alpha numeric
response to the inputbox. If you pass an alpha numeric answer to both of
the input boxes rather than a number you would need the code to be:

Sub rework()
Dim ans As Variant
Dim ans2 As Variant
Dim var As Variant

ans = InputBox("Which advisor do you wish to use?")
ans2 = InputBox("Which payment method do you wish to use?")
var = Application.Evaluate("SUMPRODUCT((A1:A10000 =""" _
& ans & """)*(H1:H10000=""" & ans2 & """))")
MsgBox (var & " cases match your criteria.")
'Range("j1") = var
End Sub

this produces a formula to be evaluated like this:
SUMPRODUCT((A1:A10="AAA")*(H1:H10="HHH"))

If one of them will be numeric, then you will need to remove the extra
quotes for that answer.

--
Regards,
Tom Ogilvy


"PaulW" wrote:

var = Application.Evaluate("SUMPRODUCT((A1:A10000 =" & ans & ")*(H1:H10000="
& ans2 & "))")

Tried, this seems to error, although the code doesn't debug until the next
line when it tries to show the msgbox and gives me error 13 as type mismatch.
Debugging it and having a look shows me that this line gives the variable
"var" the value "Error 2029". Did a quick look on google, and found a page
that said that this was a problem which sometimes occured on Excel 2002
normal edition, which confused me further since i'm using Excel 2003
Professional Edition.

Do I need to alter something in VBA to get this to work? (I know theres
loads of references that I might need to activate some of these to get
certain things to work)

Also, you mentioned that Sumproduct doesn't work like other functions. Does
this mean that Worksheetfunction.SumProduct doesn't work at all? or only when
i'm doing an actual Sumproduct rather than converting statements that produce
arrays of True/False into 0/1s?

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
WorksheetFunction help Ayo Excel Discussion (Misc queries) 2 July 20th 08 10:48 PM
Can't use WorksheetFunction Yossi Excel Discussion (Misc queries) 5 January 25th 06 10:07 PM
WorkSheetFunction.CountIf & WorkSheetFunction.SumIf with 2 conditions? Etien[_2_] Excel Programming 3 January 13th 04 04:07 PM
WorksheetFunction Mike Fogleman Excel Programming 2 January 1st 04 11:17 PM
WorksheetFunction Stuart[_10_] Excel Programming 3 November 13th 03 12:33 AM


All times are GMT +1. The time now is 07:28 PM.

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"