A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Excel » Worksheet Functions
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

SumProduct not Working in a Macro



 
 
Thread Tools Display Modes
  #1  
Old March 18th, 2010, 05:31 AM posted to microsoft.public.excel.worksheet.functions
Booey
external usenet poster
 
Posts: 4
Default SumProduct not Working in a Macro

Can someone please help - this is driving me nuts. I am wanting to use
SumProduct in a Macro but it does not compile.

The reason is that I am writing a macro to delete duplicate rows based on
all columns of data, in some cases up to 50 rows.

A simple example to highlight what I am trying to do - Lets say columns A to
G have data in them in and the data is in rows 2 - 10, headings are in row 1.
Firstly I concatenate all the data across Columns A to G and place the
result in Column H. I do this for all 10 rows (including the heading).

The next thing I do is test column H in row 10 to see how many occurrences
there are in the range H2:H10. If there is more than one I delete the entire
row. I then work my way up to row 2.

If I put the formula "=SUMPRODUCT(--(H2:H10=H10))" into cell I10 in the
spreadsheet itself I get a result no problem, let's say 2.

Whenever I try to use SumProduct in a Macro it won't give a result. To test
it I typed the following in to get a result:

Range("I10") = Application.WorksheetFunction.SumProduct(--(H2:H10 = H10))

When I put this into a Macro the line goes red and the : between the H2:H10
is highlighted and it displays:
Compile Error:
Expected: )

I have tried creating a string variable that contains the "H2:H10 = H10" but
when I put it in it doesn't work. I have tried many other ways but nothing
seems to give me a result that I can work with.
--
Regards,
Booey
  #2  
Old March 18th, 2010, 05:59 AM posted to microsoft.public.excel.worksheet.functions
Jacob Skaria
external usenet poster
 
Posts: 5,952
Default SumProduct not Working in a Macro

Why do you need to use SUMPRODUCT(). Try
Range("I1:I10").Formula = "=COUNTIF($H$1:$H$10,H1)"

OR try the below macro instead which use the Advanced Filter option

Sub Macro2()
Dim lngRow As Long, lngLastRow As Long
lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("A1:G" & lngLastRow).AdvancedFilter Action:=xlFilterInPlace,
Unique:=True
For lngRow = 1 To lngLastRow
If Rows(lngRow).Hidden Then Rows(lngRow).Delete
Next
ActiveSheet.ShowAllData
End Sub



--
Jacob


"Booey" wrote:

Can someone please help - this is driving me nuts. I am wanting to use
SumProduct in a Macro but it does not compile.

The reason is that I am writing a macro to delete duplicate rows based on
all columns of data, in some cases up to 50 rows.

A simple example to highlight what I am trying to do - Lets say columns A to
G have data in them in and the data is in rows 2 - 10, headings are in row 1.
Firstly I concatenate all the data across Columns A to G and place the
result in Column H. I do this for all 10 rows (including the heading).

The next thing I do is test column H in row 10 to see how many occurrences
there are in the range H2:H10. If there is more than one I delete the entire
row. I then work my way up to row 2.

If I put the formula "=SUMPRODUCT(--(H2:H10=H10))" into cell I10 in the
spreadsheet itself I get a result no problem, let's say 2.

Whenever I try to use SumProduct in a Macro it won't give a result. To test
it I typed the following in to get a result:

Range("I10") = Application.WorksheetFunction.SumProduct(--(H2:H10 = H10))

When I put this into a Macro the line goes red and the : between the H2:H10
is highlighted and it displays:
Compile Error:
Expected: )

I have tried creating a string variable that contains the "H2:H10 = H10" but
when I put it in it doesn't work. I have tried many other ways but nothing
seems to give me a result that I can work with.
--
Regards,
Booey

  #3  
Old March 18th, 2010, 06:02 AM posted to microsoft.public.excel.worksheet.functions
Joe User[_2_]
external usenet poster
 
Posts: 757
Default SumProduct not Working in a Macro

"Booey" wrote:
Range("I10") =
Application.WorksheetFunction.SumProduct(--(H2:H10 = H10))


There are ways to make the WorksheetFunction.SumProduct work.

But try:

Range("I10") = Evaluate("SumProduct(--(H2:H10 = H10))")

or for that functionality, more simply:

Range("I10") = Evaluate("countif(H2:H10,H10)")


----- original message -----

"Booey" wrote:
Can someone please help - this is driving me nuts. I am wanting to use
SumProduct in a Macro but it does not compile.

The reason is that I am writing a macro to delete duplicate rows based on
all columns of data, in some cases up to 50 rows.

A simple example to highlight what I am trying to do - Lets say columns A to
G have data in them in and the data is in rows 2 - 10, headings are in row 1.
Firstly I concatenate all the data across Columns A to G and place the
result in Column H. I do this for all 10 rows (including the heading).

The next thing I do is test column H in row 10 to see how many occurrences
there are in the range H2:H10. If there is more than one I delete the entire
row. I then work my way up to row 2.

If I put the formula "=SUMPRODUCT(--(H2:H10=H10))" into cell I10 in the
spreadsheet itself I get a result no problem, let's say 2.

Whenever I try to use SumProduct in a Macro it won't give a result. To test
it I typed the following in to get a result:

Range("I10") = Application.WorksheetFunction.SumProduct(--(H2:H10 = H10))

When I put this into a Macro the line goes red and the : between the H2:H10
is highlighted and it displays:
Compile Error:
Expected: )

I have tried creating a string variable that contains the "H2:H10 = H10" but
when I put it in it doesn't work. I have tried many other ways but nothing
seems to give me a result that I can work with.
--
Regards,
Booey

  #4  
Old March 18th, 2010, 06:36 AM posted to microsoft.public.excel.worksheet.functions
Booey
external usenet poster
 
Posts: 4
Default SumProduct not Working in a Macro

Hi Jacob. I need to use SUMPRODUCT as COUNTIF only works on cell values up
to 255 characters, whereas this is not a problem with SUMPRODUCT. Some of
the concatenated cells might be as much as 1000 characters.

I eventually solved it using the following code:

mFormula = "SUMPRODUCT(--(" & ConcCol & 2 & ":" & ConcCol & R &
"=" & ConcCol & R & "))"
Q = Application.Evaluate(mFormula)

Where mFormula is defined as String and Q as Long. ConcCol and R are
variables in a For Next loop that I use as I work up from the bottom to the
top of the data.

Thanks for trying to help.

--
Regards,
Booey


"Jacob Skaria" wrote:

Why do you need to use SUMPRODUCT(). Try
Range("I1:I10").Formula = "=COUNTIF($H$1:$H$10,H1)"

OR try the below macro instead which use the Advanced Filter option

Sub Macro2()
Dim lngRow As Long, lngLastRow As Long
lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("A1:G" & lngLastRow).AdvancedFilter Action:=xlFilterInPlace,
Unique:=True
For lngRow = 1 To lngLastRow
If Rows(lngRow).Hidden Then Rows(lngRow).Delete
Next
ActiveSheet.ShowAllData
End Sub



--
Jacob


"Booey" wrote:

Can someone please help - this is driving me nuts. I am wanting to use
SumProduct in a Macro but it does not compile.

The reason is that I am writing a macro to delete duplicate rows based on
all columns of data, in some cases up to 50 rows.

A simple example to highlight what I am trying to do - Lets say columns A to
G have data in them in and the data is in rows 2 - 10, headings are in row 1.
Firstly I concatenate all the data across Columns A to G and place the
result in Column H. I do this for all 10 rows (including the heading).

The next thing I do is test column H in row 10 to see how many occurrences
there are in the range H2:H10. If there is more than one I delete the entire
row. I then work my way up to row 2.

If I put the formula "=SUMPRODUCT(--(H2:H10=H10))" into cell I10 in the
spreadsheet itself I get a result no problem, let's say 2.

Whenever I try to use SumProduct in a Macro it won't give a result. To test
it I typed the following in to get a result:

Range("I10") = Application.WorksheetFunction.SumProduct(--(H2:H10 = H10))

When I put this into a Macro the line goes red and the : between the H2:H10
is highlighted and it displays:
Compile Error:
Expected: )

I have tried creating a string variable that contains the "H2:H10 = H10" but
when I put it in it doesn't work. I have tried many other ways but nothing
seems to give me a result that I can work with.
--
Regards,
Booey

  #5  
Old March 18th, 2010, 06:38 AM posted to microsoft.public.excel.worksheet.functions
Booey
external usenet poster
 
Posts: 4
Default SumProduct not Working in a Macro

Hi Joe.

I eventually solved it using the following code:

mFormula = "SUMPRODUCT(--(" & ConcCol & 2 & ":" & ConcCol & R &
"=" & ConcCol & R & "))"
Q = Application.Evaluate(mFormula)

Where mFormula is defined as String and Q as Long. ConcCol and R are
variables in a For Next loop that I use as I work up from the bottom to the
top of the data.

I had to muck around a bit to get the syntax exactly right in mFormula, but
it works perfectly.

Thanks for trying to help.

--
Regards,
Booey


"Joe User" wrote:

"Booey" wrote:
Range("I10") =
Application.WorksheetFunction.SumProduct(--(H2:H10 = H10))


There are ways to make the WorksheetFunction.SumProduct work.

But try:

Range("I10") = Evaluate("SumProduct(--(H2:H10 = H10))")

or for that functionality, more simply:

Range("I10") = Evaluate("countif(H2:H10,H10)")


----- original message -----

"Booey" wrote:
Can someone please help - this is driving me nuts. I am wanting to use
SumProduct in a Macro but it does not compile.

The reason is that I am writing a macro to delete duplicate rows based on
all columns of data, in some cases up to 50 rows.

A simple example to highlight what I am trying to do - Lets say columns A to
G have data in them in and the data is in rows 2 - 10, headings are in row 1.
Firstly I concatenate all the data across Columns A to G and place the
result in Column H. I do this for all 10 rows (including the heading).

The next thing I do is test column H in row 10 to see how many occurrences
there are in the range H2:H10. If there is more than one I delete the entire
row. I then work my way up to row 2.

If I put the formula "=SUMPRODUCT(--(H2:H10=H10))" into cell I10 in the
spreadsheet itself I get a result no problem, let's say 2.

Whenever I try to use SumProduct in a Macro it won't give a result. To test
it I typed the following in to get a result:

Range("I10") = Application.WorksheetFunction.SumProduct(--(H2:H10 = H10))

When I put this into a Macro the line goes red and the : between the H2:H10
is highlighted and it displays:
Compile Error:
Expected: )

I have tried creating a string variable that contains the "H2:H10 = H10" but
when I put it in it doesn't work. I have tried many other ways but nothing
seems to give me a result that I can work with.
--
Regards,
Booey

  #6  
Old March 18th, 2010, 06:58 AM posted to microsoft.public.excel.worksheet.functions
Jacob Skaria
external usenet poster
 
Posts: 5,952
Default SumProduct not Working in a Macro

Did you try the macro..
--
Jacob


"Booey" wrote:

Hi Jacob. I need to use SUMPRODUCT as COUNTIF only works on cell values up
to 255 characters, whereas this is not a problem with SUMPRODUCT. Some of
the concatenated cells might be as much as 1000 characters.

I eventually solved it using the following code:

mFormula = "SUMPRODUCT(--(" & ConcCol & 2 & ":" & ConcCol & R &
"=" & ConcCol & R & "))"
Q = Application.Evaluate(mFormula)

Where mFormula is defined as String and Q as Long. ConcCol and R are
variables in a For Next loop that I use as I work up from the bottom to the
top of the data.

Thanks for trying to help.

--
Regards,
Booey


"Jacob Skaria" wrote:

Why do you need to use SUMPRODUCT(). Try
Range("I1:I10").Formula = "=COUNTIF($H$1:$H$10,H1)"

OR try the below macro instead which use the Advanced Filter option

Sub Macro2()
Dim lngRow As Long, lngLastRow As Long
lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("A1:G" & lngLastRow).AdvancedFilter Action:=xlFilterInPlace,
Unique:=True
For lngRow = 1 To lngLastRow
If Rows(lngRow).Hidden Then Rows(lngRow).Delete
Next
ActiveSheet.ShowAllData
End Sub



--
Jacob


"Booey" wrote:

Can someone please help - this is driving me nuts. I am wanting to use
SumProduct in a Macro but it does not compile.

The reason is that I am writing a macro to delete duplicate rows based on
all columns of data, in some cases up to 50 rows.

A simple example to highlight what I am trying to do - Lets say columns A to
G have data in them in and the data is in rows 2 - 10, headings are in row 1.
Firstly I concatenate all the data across Columns A to G and place the
result in Column H. I do this for all 10 rows (including the heading).

The next thing I do is test column H in row 10 to see how many occurrences
there are in the range H2:H10. If there is more than one I delete the entire
row. I then work my way up to row 2.

If I put the formula "=SUMPRODUCT(--(H2:H10=H10))" into cell I10 in the
spreadsheet itself I get a result no problem, let's say 2.

Whenever I try to use SumProduct in a Macro it won't give a result. To test
it I typed the following in to get a result:

Range("I10") = Application.WorksheetFunction.SumProduct(--(H2:H10 = H10))

When I put this into a Macro the line goes red and the : between the H2:H10
is highlighted and it displays:
Compile Error:
Expected: )

I have tried creating a string variable that contains the "H2:H10 = H10" but
when I put it in it doesn't work. I have tried many other ways but nothing
seems to give me a result that I can work with.
--
Regards,
Booey

  #7  
Old March 18th, 2010, 09:13 AM posted to microsoft.public.excel.worksheet.functions
Bob Phillips[_3_]
external usenet poster
 
Posts: 489
Default SumProduct not Working in a Macro



"Joe User" joeu2004 wrote in message
...
"Booey" wrote:
Range("I10") =
Application.WorksheetFunction.SumProduct(--(H2:H10 = H10))


There are ways to make the WorksheetFunction.SumProduct work.

But try:

Range("I10") = Evaluate("SumProduct(--(H2:H10 = H10))")

or for that functionality, more simply:

Range("I10") = Evaluate("countif(H2:H10,H10)")


If you are going to use COUNTIF there is no need for evaluate, that is only
necessary for array formulae.


 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 12:43 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.