Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 111
Default VBA SUM Function

All,

I have the following code.

I have read up on SUMIF fucntions and seen all the options with pivot
tables, sorting, and using arrays. However I would like to find a
simple way to add another criteria to the code that I already have.
For example I would only like the code to 'run' and extract the values
of there is specific information in another column (B). For example
there is another column in the worksheet labelled 'yes/no'.The value
will be either Y or N. I would like to run the code to say that if the
value is Y then it uses the data in cloumn K and S and adds it to the
sum, and if its N then it just ignores it.

Any sugestions.

Thanks in advance for your help,


Regards,

Joseph Crabtree

With Sheets("Data")
LastRow = Sheets("Data").Range("K" & Rows.Count).End(xlUp).Row
Set CodeRange = .Range("K2:K" & LastRow)
Set SumRange = .Range("S2:S" & LastRow)

End With

Sheets("data").Activate
Range("K1", "K" & LastRow).Select
Selection.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Selection.Copy Sheets("output").Range("A1")
ActiveSheet.ShowAllData

Set CriteriaRange = Sheets("Output").Range("A2")
For r = 2 To Sheets("Output").Range("A2").End(xlDown).Row
Total = WorksheetFunction.SumIf(CodeRange, CriteriaRange,
SumRange)
CriteriaRange.Offset(0, 1) = Total
Set CriteriaRange = CriteriaRange.Offset(1, 0)
Next
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default VBA SUM Function

Consider using SUMPRODUCT in place of SUMIF
--
Gary''s Student - gsnu200840


"joecrabtree" wrote:

All,

I have the following code.

I have read up on SUMIF fucntions and seen all the options with pivot
tables, sorting, and using arrays. However I would like to find a
simple way to add another criteria to the code that I already have.
For example I would only like the code to 'run' and extract the values
of there is specific information in another column (B). For example
there is another column in the worksheet labelled 'yes/no'.The value
will be either Y or N. I would like to run the code to say that if the
value is Y then it uses the data in cloumn K and S and adds it to the
sum, and if its N then it just ignores it.

Any sugestions.

Thanks in advance for your help,


Regards,

Joseph Crabtree

With Sheets("Data")
LastRow = Sheets("Data").Range("K" & Rows.Count).End(xlUp).Row
Set CodeRange = .Range("K2:K" & LastRow)
Set SumRange = .Range("S2:S" & LastRow)

End With

Sheets("data").Activate
Range("K1", "K" & LastRow).Select
Selection.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Selection.Copy Sheets("output").Range("A1")
ActiveSheet.ShowAllData

Set CriteriaRange = Sheets("Output").Range("A2")
For r = 2 To Sheets("Output").Range("A2").End(xlDown).Row
Total = WorksheetFunction.SumIf(CodeRange, CriteriaRange,
SumRange)
CriteriaRange.Offset(0, 1) = Total
Set CriteriaRange = CriteriaRange.Offset(1, 0)
Next

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default VBA SUM Function

I have a question with the code below. It looks like the following statement
is creating a new workbook. Is that what you want?

Selection.Copy Sheets("output").Range("A1")

When you do a copy and don't use AFTER or BEFORE the new worksheet gets
added into a new workbook.

You have a lot of postings in the last couple of days. I prefer to respond
to you latest code. If you want to have a Yes/No column let me know which
column the Y and N are located. I agree with Gary the way you are writing
the code that Sumproduct will work.

I find sometimes it is better to just write the code (in your case Total) by
moving down one row at a time then to use a worksheet function.

"joecrabtree" wrote:

All,

I have the following code.

I have read up on SUMIF fucntions and seen all the options with pivot
tables, sorting, and using arrays. However I would like to find a
simple way to add another criteria to the code that I already have.
For example I would only like the code to 'run' and extract the values
of there is specific information in another column (B). For example
there is another column in the worksheet labelled 'yes/no'.The value
will be either Y or N. I would like to run the code to say that if the
value is Y then it uses the data in cloumn K and S and adds it to the
sum, and if its N then it just ignores it.

Any sugestions.

Thanks in advance for your help,


Regards,

Joseph Crabtree

With Sheets("Data")
LastRow = Sheets("Data").Range("K" & Rows.Count).End(xlUp).Row
Set CodeRange = .Range("K2:K" & LastRow)
Set SumRange = .Range("S2:S" & LastRow)

End With

Sheets("data").Activate
Range("K1", "K" & LastRow).Select
Selection.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Selection.Copy Sheets("output").Range("A1")
ActiveSheet.ShowAllData

Set CriteriaRange = Sheets("Output").Range("A2")
For r = 2 To Sheets("Output").Range("A2").End(xlDown).Row
Total = WorksheetFunction.SumIf(CodeRange, CriteriaRange,
SumRange)
CriteriaRange.Offset(0, 1) = Total
Set CriteriaRange = CriteriaRange.Offset(1, 0)
Next

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 111
Default VBA SUM Function

On Mar 24, 2:46*pm, joel wrote:
I have a question with the code below. *It looks like the following statement
is creating a new workbook. *Is that what you want?

Selection.Copy Sheets("output").Range("A1")

When you do a copy and don't use AFTER or BEFORE the new worksheet gets
added into a new workbook.

You have a lot of postings in the last couple of days. *I prefer to respond
to you latest code. *If you want to have a Yes/No column let me know which
column the Y and N are located. *I agree with Gary the way you are writing
the code that Sumproduct will work.

I find sometimes it is better to just write the code (in your case Total) by
moving down one row at a time then to use a worksheet function.

"joecrabtree" wrote:
All,


I have the following code.


I have read up on SUMIF fucntions and seen all the options with pivot
tables, sorting, and using arrays. However I would like to find a
simple way to add another criteria to the code that I already have.
For example I would only like the code to 'run' and extract the values
of there is specific information in another column (B). For example
there is another column in the worksheet labelled 'yes/no'.The value
will be either Y or N. I would like to run the code to say that if the
value is Y then it uses the data in cloumn K and S and adds it to the
sum, and if its N then it just ignores it.


Any sugestions.


Thanks in advance for your help,


Regards,


Joseph Crabtree


With Sheets("Data")
* * LastRow = Sheets("Data").Range("K" & Rows.Count).End(xlUp).Row
* * Set CodeRange = .Range("K2:K" & LastRow)
* * Set SumRange = .Range("S2:S" & LastRow)


End With


Sheets("data").Activate
Range("K1", "K" & LastRow).Select
Selection.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Selection.Copy Sheets("output").Range("A1")
ActiveSheet.ShowAllData


Set CriteriaRange = Sheets("Output").Range("A2")
For r = 2 To Sheets("Output").Range("A2").End(xlDown).Row
* * Total = WorksheetFunction.SumIf(CodeRange, CriteriaRange,
SumRange)
* * CriteriaRange.Offset(0, 1) = Total
* * Set CriteriaRange = CriteriaRange.Offset(1, 0)
Next


Apologies for all the postings. The Y/N column is in column B.

In essence all I wanted to do was to be able to expand the criteria of
SUMIF to include another column, i.e Y/N.

How can i modify it to use sum product?"

Thanks

Joe
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default VBA SUM Function

I'm not an expert in using Sumproduct from VBA code. This is the way I've
gotten it toook work in the past. Before you modify this code run it the way
it is and look at the formula in the message box so you understand how it
works.

MYformula produces this line of text. I use evalute to run this statement
like it was on the worksheet. Notice NO equal sign before SUMPRODUCT

"Sumproduct(--($K$2:$K$8="b"),--($B$2:$B$8="Y"),$S$2:$S$8)"



With Sheets("Data")
LastRow = Sheets("Data").Range("K" & Rows.Count).End(xlUp).Row
Set CodeRange = .Range("K2:K" & LastRow)
Set SumRange = .Range("S2:S" & LastRow)
Set CriteriaRange = .Range("B2:B" & LastRow)

For r = 2 To LastRow
MyFormula = "Sumproduct(" & _
"--(" & CodeRange.Address & "=""" & .Range("K" & r).Value & """),"
& _
"--(" & CriteriaRange.Address & "=""Y"")," & _
SumRange.Address & ")"

msgbox(Myformula)
Total = Application.Evaluate(MyFormula)

Next r

End With

"joecrabtree" wrote:

On Mar 24, 2:46 pm, joel wrote:
I have a question with the code below. It looks like the following statement
is creating a new workbook. Is that what you want?

Selection.Copy Sheets("output").Range("A1")

When you do a copy and don't use AFTER or BEFORE the new worksheet gets
added into a new workbook.

You have a lot of postings in the last couple of days. I prefer to respond
to you latest code. If you want to have a Yes/No column let me know which
column the Y and N are located. I agree with Gary the way you are writing
the code that Sumproduct will work.

I find sometimes it is better to just write the code (in your case Total) by
moving down one row at a time then to use a worksheet function.

"joecrabtree" wrote:
All,


I have the following code.


I have read up on SUMIF fucntions and seen all the options with pivot
tables, sorting, and using arrays. However I would like to find a
simple way to add another criteria to the code that I already have.
For example I would only like the code to 'run' and extract the values
of there is specific information in another column (B). For example
there is another column in the worksheet labelled 'yes/no'.The value
will be either Y or N. I would like to run the code to say that if the
value is Y then it uses the data in cloumn K and S and adds it to the
sum, and if its N then it just ignores it.


Any sugestions.


Thanks in advance for your help,


Regards,


Joseph Crabtree


With Sheets("Data")
LastRow = Sheets("Data").Range("K" & Rows.Count).End(xlUp).Row
Set CodeRange = .Range("K2:K" & LastRow)
Set SumRange = .Range("S2:S" & LastRow)


End With


Sheets("data").Activate
Range("K1", "K" & LastRow).Select
Selection.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Selection.Copy Sheets("output").Range("A1")
ActiveSheet.ShowAllData


Set CriteriaRange = Sheets("Output").Range("A2")
For r = 2 To Sheets("Output").Range("A2").End(xlDown).Row
Total = WorksheetFunction.SumIf(CodeRange, CriteriaRange,
SumRange)
CriteriaRange.Offset(0, 1) = Total
Set CriteriaRange = CriteriaRange.Offset(1, 0)
Next


Apologies for all the postings. The Y/N column is in column B.

In essence all I wanted to do was to be able to expand the criteria of
SUMIF to include another column, i.e Y/N.

How can i modify it to use sum product?"

Thanks

Joe



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default VBA SUM Function

I didn't look at the code very closely, but I think that the data is spread over
two sheets. That means that the OP needs to go back to the original code to set
those ranges.

And this formula:
=Sumproduct(--($K$2:$K$8="b"),--($B$2:$B$8="Y"),$S$2:$S$8)
needs to look more like:
=Sumproduct(--(Data!$K$2:$K$8="B"),--(Data!$B$2:$B$8="Y"),data!$S$2:$S$8)

Application.evaluate() will use the cells on the activesheet for any unqualified
address.

You could activate Data, then create the formula or even use:
total = worksheets("data").evaluate(myformula)

Or you could qualify those ranges:

MyFormula = "Sumproduct(" & _
"--(" & CodeRange.Address & "=""" & .Range("K" & r).Value & """)," & _
"--(" & CriteriaRange.Address & "=""Y"")," & _
SumRange.Address & ")"

would look more like:

MyFormula = "Sumproduct(" & _
"--(" & CodeRange.Address(external:=true) & "=""" _
& .Range("K" & r).Value & """)," & _
"--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
SumRange.Address(external:=true) & ")"

Since you used worksheets("Data").range("K" & r).value, it won't change.

Untested, uncompiled!





joel wrote:

I'm not an expert in using Sumproduct from VBA code. This is the way I've
gotten it toook work in the past. Before you modify this code run it the way
it is and look at the formula in the message box so you understand how it
works.

MYformula produces this line of text. I use evalute to run this statement
like it was on the worksheet. Notice NO equal sign before SUMPRODUCT

"Sumproduct(--($K$2:$K$8="b"),--($B$2:$B$8="Y"),$S$2:$S$8)"

With Sheets("Data")
LastRow = Sheets("Data").Range("K" & Rows.Count).End(xlUp).Row
Set CodeRange = .Range("K2:K" & LastRow)
Set SumRange = .Range("S2:S" & LastRow)
Set CriteriaRange = .Range("B2:B" & LastRow)

For r = 2 To LastRow
MyFormula = "Sumproduct(" & _
"--(" & CodeRange.Address & "=""" & .Range("K" & r).Value & """),"
& _
"--(" & CriteriaRange.Address & "=""Y"")," & _
SumRange.Address & ")"

msgbox(Myformula)
Total = Application.Evaluate(MyFormula)

Next r

End With

"joecrabtree" wrote:

On Mar 24, 2:46 pm, joel wrote:
I have a question with the code below. It looks like the following statement
is creating a new workbook. Is that what you want?

Selection.Copy Sheets("output").Range("A1")

When you do a copy and don't use AFTER or BEFORE the new worksheet gets
added into a new workbook.

You have a lot of postings in the last couple of days. I prefer to respond
to you latest code. If you want to have a Yes/No column let me know which
column the Y and N are located. I agree with Gary the way you are writing
the code that Sumproduct will work.

I find sometimes it is better to just write the code (in your case Total) by
moving down one row at a time then to use a worksheet function.

"joecrabtree" wrote:
All,

I have the following code.

I have read up on SUMIF fucntions and seen all the options with pivot
tables, sorting, and using arrays. However I would like to find a
simple way to add another criteria to the code that I already have.
For example I would only like the code to 'run' and extract the values
of there is specific information in another column (B). For example
there is another column in the worksheet labelled 'yes/no'.The value
will be either Y or N. I would like to run the code to say that if the
value is Y then it uses the data in cloumn K and S and adds it to the
sum, and if its N then it just ignores it.

Any sugestions.

Thanks in advance for your help,

Regards,

Joseph Crabtree

With Sheets("Data")
LastRow = Sheets("Data").Range("K" & Rows.Count).End(xlUp).Row
Set CodeRange = .Range("K2:K" & LastRow)
Set SumRange = .Range("S2:S" & LastRow)

End With

Sheets("data").Activate
Range("K1", "K" & LastRow).Select
Selection.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Selection.Copy Sheets("output").Range("A1")
ActiveSheet.ShowAllData

Set CriteriaRange = Sheets("Output").Range("A2")
For r = 2 To Sheets("Output").Range("A2").End(xlDown).Row
Total = WorksheetFunction.SumIf(CodeRange, CriteriaRange,
SumRange)
CriteriaRange.Offset(0, 1) = Total
Set CriteriaRange = CriteriaRange.Offset(1, 0)
Next


Apologies for all the postings. The Y/N column is in column B.

In essence all I wanted to do was to be able to expand the criteria of
SUMIF to include another column, i.e Y/N.

How can i modify it to use sum product?"

Thanks

Joe


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default VBA SUM Function

Dave: Thanks. I was thinking about multiple worksheets after I posted. The
external = true is the best solution. I don't like selecting worksheets.


MyFormula = "Sumproduct(" & _
"--(" & CodeRange.Address(external:=true) & "=""" _
& .Range("K" & r).Value & """)," & _
"--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
SumRange.Address(external:=true) & ")"

"Dave Peterson" wrote:

I didn't look at the code very closely, but I think that the data is spread over
two sheets. That means that the OP needs to go back to the original code to set
those ranges.

And this formula:
=Sumproduct(--($K$2:$K$8="b"),--($B$2:$B$8="Y"),$S$2:$S$8)
needs to look more like:
=Sumproduct(--(Data!$K$2:$K$8="B"),--(Data!$B$2:$B$8="Y"),data!$S$2:$S$8)

Application.evaluate() will use the cells on the activesheet for any unqualified
address.

You could activate Data, then create the formula or even use:
total = worksheets("data").evaluate(myformula)

Or you could qualify those ranges:

MyFormula = "Sumproduct(" & _
"--(" & CodeRange.Address & "=""" & .Range("K" & r).Value & """)," & _
"--(" & CriteriaRange.Address & "=""Y"")," & _
SumRange.Address & ")"

would look more like:

MyFormula = "Sumproduct(" & _
"--(" & CodeRange.Address(external:=true) & "=""" _
& .Range("K" & r).Value & """)," & _
"--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
SumRange.Address(external:=true) & ")"

Since you used worksheets("Data").range("K" & r).value, it won't change.

Untested, uncompiled!





joel wrote:

I'm not an expert in using Sumproduct from VBA code. This is the way I've
gotten it toook work in the past. Before you modify this code run it the way
it is and look at the formula in the message box so you understand how it
works.

MYformula produces this line of text. I use evalute to run this statement
like it was on the worksheet. Notice NO equal sign before SUMPRODUCT

"Sumproduct(--($K$2:$K$8="b"),--($B$2:$B$8="Y"),$S$2:$S$8)"

With Sheets("Data")
LastRow = Sheets("Data").Range("K" & Rows.Count).End(xlUp).Row
Set CodeRange = .Range("K2:K" & LastRow)
Set SumRange = .Range("S2:S" & LastRow)
Set CriteriaRange = .Range("B2:B" & LastRow)

For r = 2 To LastRow
MyFormula = "Sumproduct(" & _
"--(" & CodeRange.Address & "=""" & .Range("K" & r).Value & """),"
& _
"--(" & CriteriaRange.Address & "=""Y"")," & _
SumRange.Address & ")"

msgbox(Myformula)
Total = Application.Evaluate(MyFormula)

Next r

End With

"joecrabtree" wrote:

On Mar 24, 2:46 pm, joel wrote:
I have a question with the code below. It looks like the following statement
is creating a new workbook. Is that what you want?

Selection.Copy Sheets("output").Range("A1")

When you do a copy and don't use AFTER or BEFORE the new worksheet gets
added into a new workbook.

You have a lot of postings in the last couple of days. I prefer to respond
to you latest code. If you want to have a Yes/No column let me know which
column the Y and N are located. I agree with Gary the way you are writing
the code that Sumproduct will work.

I find sometimes it is better to just write the code (in your case Total) by
moving down one row at a time then to use a worksheet function.

"joecrabtree" wrote:
All,

I have the following code.

I have read up on SUMIF fucntions and seen all the options with pivot
tables, sorting, and using arrays. However I would like to find a
simple way to add another criteria to the code that I already have.
For example I would only like the code to 'run' and extract the values
of there is specific information in another column (B). For example
there is another column in the worksheet labelled 'yes/no'.The value
will be either Y or N. I would like to run the code to say that if the
value is Y then it uses the data in cloumn K and S and adds it to the
sum, and if its N then it just ignores it.

Any sugestions.

Thanks in advance for your help,

Regards,

Joseph Crabtree

With Sheets("Data")
LastRow = Sheets("Data").Range("K" & Rows.Count).End(xlUp).Row
Set CodeRange = .Range("K2:K" & LastRow)
Set SumRange = .Range("S2:S" & LastRow)

End With

Sheets("data").Activate
Range("K1", "K" & LastRow).Select
Selection.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Selection.Copy Sheets("output").Range("A1")
ActiveSheet.ShowAllData

Set CriteriaRange = Sheets("Output").Range("A2")
For r = 2 To Sheets("Output").Range("A2").End(xlDown).Row
Total = WorksheetFunction.SumIf(CodeRange, CriteriaRange,
SumRange)
CriteriaRange.Offset(0, 1) = Total
Set CriteriaRange = CriteriaRange.Offset(1, 0)
Next

Apologies for all the postings. The Y/N column is in column B.

In essence all I wanted to do was to be able to expand the criteria of
SUMIF to include another column, i.e Y/N.

How can i modify it to use sum product?"

Thanks

Joe


--

Dave Peterson

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 111
Default VBA SUM Function

On Mar 24, 6:00*pm, joel wrote:
Dave: Thanks. *I was thinking about multiple worksheets after I posted. *The
external = true is the best solution. *I don't like selecting worksheets.

*MyFormula = "Sumproduct(" & _
* * * "--(" & CodeRange.Address(external:=true) & "=""" _
* * * * * * * * & .Range("K" & r).Value & """)," *& _
* * * "--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
* * * SumRange.Address(external:=true) & ")"

"Dave Peterson" wrote:
I didn't look at the code very closely, but I think that the data is spread over
two sheets. *That means that the OP needs to go back to the original code to set
those ranges.


And this formula:
=Sumproduct(--($K$2:$K$8="b"),--($B$2:$B$8="Y"),$S$2:$S$8)
needs to look more like:
=Sumproduct(--(Data!$K$2:$K$8="B"),--(Data!$B$2:$B$8="Y"),data!$S$2:$S$8)


Application.evaluate() will use the cells on the activesheet for any unqualified
address.


You could activate Data, then create the formula or even use:
total = worksheets("data").evaluate(myformula)


Or you could qualify those ranges:


MyFormula = "Sumproduct(" & _
* * *"--(" & CodeRange.Address & "=""" & .Range("K" & r).Value & """)," *& _
* * *"--(" & CriteriaRange.Address & "=""Y"")," & _
* * *SumRange.Address & ")"


would look more like:


MyFormula = "Sumproduct(" & _
* * *"--(" & CodeRange.Address(external:=true) & "=""" _
* * * * * * * *& .Range("K" & r).Value & """)," *& _
* * *"--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
* * *SumRange.Address(external:=true) & ")"


Since you used worksheets("Data").range("K" & r).value, it won't change..


Untested, uncompiled!


joel wrote:


I'm not an expert in using Sumproduct from VBA code. *This is the way I've
gotten it toook work in the past. *Before you modify this code run it the way
it is and look at the formula in the message box so you understand how it
works.


MYformula produces this line of text. *I use evalute to run this statement
like it was on the worksheet. *Notice NO equal sign before SUMPRODUCT


"Sumproduct(--($K$2:$K$8="b"),--($B$2:$B$8="Y"),$S$2:$S$8)"


With Sheets("Data")
* * LastRow = Sheets("Data").Range("K" & Rows.Count).End(xlUp).Row
* * Set CodeRange = .Range("K2:K" & LastRow)
* * Set SumRange = .Range("S2:S" & LastRow)
* * Set CriteriaRange = .Range("B2:B" & LastRow)


* * For r = 2 To LastRow
* * * MyFormula = "Sumproduct(" & _
* * * * *"--(" & CodeRange.Address & "=""" & .Range("K" & r).Value & """),"
& _
* * * * *"--(" & CriteriaRange.Address & "=""Y"")," & _
* * * * *SumRange.Address & ")"


* * * msgbox(Myformula)
* * * Total = Application.Evaluate(MyFormula)


* *Next r


End With


"joecrabtree" wrote:


On Mar 24, 2:46 pm, joel wrote:
I have a question with the code below. *It looks like the following statement
is creating a new workbook. *Is that what you want?


Selection.Copy Sheets("output").Range("A1")


When you do a copy and don't use AFTER or BEFORE the new worksheet gets
added into a new workbook.


You have a lot of postings in the last couple of days. *I prefer to respond
to you latest code. *If you want to have a Yes/No column let me know which
column the Y and N are located. *I agree with Gary the way you are writing
the code that Sumproduct will work.


I find sometimes it is better to just write the code (in your case Total) by
moving down one row at a time then to use a worksheet function.


"joecrabtree" wrote:
All,


I have the following code.


I have read up on SUMIF fucntions and seen all the options with pivot
tables, sorting, and using arrays. However I would like to find a
simple way to add another criteria to the code that I already have.
For example I would only like the code to 'run' and extract the values
of there is specific information in another column (B). For example
there is another column in the worksheet labelled 'yes/no'.The value
will be either Y or N. I would like to run the code to say that if the
value is Y then it uses the data in cloumn K and S and adds it to the
sum, and if its N then it just ignores it.


Any sugestions.


Thanks in advance for your help,


Regards,


Joseph Crabtree


With Sheets("Data")
* * LastRow = Sheets("Data").Range("K" & Rows.Count).End(xlUp).Row
* * Set CodeRange = .Range("K2:K" & LastRow)
* * Set SumRange = .Range("S2:S" & LastRow)


End With


Sheets("data").Activate
Range("K1", "K" & LastRow).Select
Selection.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Selection.Copy Sheets("output").Range("A1")
ActiveSheet.ShowAllData


Set CriteriaRange = Sheets("Output").Range("A2")
For r = 2 To Sheets("Output").Range("A2").End(xlDown).Row
* * Total = WorksheetFunction.SumIf(CodeRange, CriteriaRange,
SumRange)
* * CriteriaRange.Offset(0, 1) = Total
* * Set CriteriaRange = CriteriaRange.Offset(1, 0)
Next


Apologies for all the postings. The Y/N column is in column B.


In essence all I wanted to do was to be able to expand the criteria of
SUMIF to include another column, i.e Y/N.


How can i modify it to use sum product?"


Thanks


Joe


--


Dave Peterson


Thanks for all your help. Ive now got this working, and understand how
it works. However I havent been able to get an output, i.e. displayed
sumproduct value. For example how would I be able to get this to
display the outputs an output worksheet?

Thanks again,

Joe Crabtree
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default VBA SUM Function

Got it working means that
Total = Application.Evaluate(MyFormula)
actually evaluated to the correct number?

If yes, then maybe something like after this line:

Total = Application.Evaluate(MyFormula)
..Range("X" & r).Value = total


joecrabtree wrote:

<<snipped

Thanks for all your help. Ive now got this working, and understand how
it works. However I havent been able to get an output, i.e. displayed
sumproduct value. For example how would I be able to get this to
display the outputs an output worksheet?

Thanks again,

Joe Crabtree


--

Dave Peterson
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default VBA SUM Function

You can also put the sumproduct formula in the worksheet

from
MyFormula = "Sumproduct(" & _
"--(" & CodeRange.Address(external:=true) & "=""" _
& .Range("K" & r).Value & """)," & _
"--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
SumRange.Address(external:=true) & ")"

to (added equal sign in front of forumal)
MyFormula = "=Sumproduct(" & _
"--(" & CodeRange.Address(external:=true) & "=""" _
& .Range("K" & r).Value & """)," & _
"--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
SumRange.Address(external:=true) & ")"
Range("A1").formula = Myformula


"Dave Peterson" wrote:

Got it working means that
Total = Application.Evaluate(MyFormula)
actually evaluated to the correct number?

If yes, then maybe something like after this line:

Total = Application.Evaluate(MyFormula)
..Range("X" & r).Value = total


joecrabtree wrote:

<<snipped

Thanks for all your help. Ive now got this working, and understand how
it works. However I havent been able to get an output, i.e. displayed
sumproduct value. For example how would I be able to get this to
display the outputs an output worksheet?

Thanks again,

Joe Crabtree


--

Dave Peterson



  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 111
Default VBA SUM Function

On Mar 25, 2:29*pm, joel wrote:
You can also put the sumproduct formula in the worksheet

from
*MyFormula = "Sumproduct(" & _
* * * "--(" & CodeRange.Address(external:=true) & "=""" _
* * * * * * * * & .Range("K" & r).Value & """)," *& _
* * * "--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
* * * SumRange.Address(external:=true) & ")"

to (added equal sign in front of forumal)
*MyFormula = "=Sumproduct(" & _
* * * "--(" & CodeRange.Address(external:=true) & "=""" _
* * * * * * * * & .Range("K" & r).Value & """)," *& _
* * * "--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
* * * SumRange.Address(external:=true) & ")"
*Range("A1").formula = Myformula

"Dave Peterson" wrote:
Got it working means that
Total = Application.Evaluate(MyFormula)
actually evaluated to the correct number?


If yes, then maybe something like after this line:


Total = Application.Evaluate(MyFormula)
..Range("X" & r).Value = total


joecrabtree wrote:


<<snipped


Thanks for all your help. Ive now got this working, and understand how
it works. However I havent been able to get an output, i.e. displayed
sumproduct value. For example how would I be able to get this to
display the outputs an output worksheet?


Thanks again,


Joe Crabtree


--


Dave Peterson


All,

Thankyou both for your input. I now have another question based on
this code.Using this code I have a workbook with a worksheet in it
called 'data'. The format is shown below:

Column Letter B K S
1 YES/NO CODE VALUE
2 Y ABC 10
3 Y ABC 2
4 N ABB 44
5 N ABC 23
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 111
Default VBA SUM Function

On Mar 26, 8:17*am, joecrabtree wrote:
On Mar 25, 2:29*pm, joel wrote:



You can also put the sumproduct formula in the worksheet


from
*MyFormula = "Sumproduct(" & _
* * * "--(" & CodeRange.Address(external:=true) & "=""" _
* * * * * * * * & .Range("K" & r).Value & """)," *& _
* * * "--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
* * * SumRange.Address(external:=true) & ")"


to (added equal sign in front of forumal)
*MyFormula = "=Sumproduct(" & _
* * * "--(" & CodeRange.Address(external:=true) & "=""" _
* * * * * * * * & .Range("K" & r).Value & """)," *& _
* * * "--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
* * * SumRange.Address(external:=true) & ")"
*Range("A1").formula = Myformula


"Dave Peterson" wrote:
Got it working means that
Total = Application.Evaluate(MyFormula)
actually evaluated to the correct number?


If yes, then maybe something like after this line:


Total = Application.Evaluate(MyFormula)
..Range("X" & r).Value = total


joecrabtree wrote:


<<snipped


Thanks for all your help. Ive now got this working, and understand how
it works. However I havent been able to get an output, i.e. displayed
sumproduct value. For example how would I be able to get this to
display the outputs an output worksheet?


Thanks again,


Joe Crabtree


--


Dave Peterson


All,

Thankyou both for your input. I now have another question based on
this code.Using this code I have a workbook with a worksheet in it
called 'data'. The format is shown below:

Column Letter * B * * * K * * * S
1 * * * YES/NO *CODE * *VALUE
2 * * * Y * * * ABC * * 10
3 * * * Y * * * ABC * * 2
4 * * * N * * * ABB * * 44
5 * * * N * * * ABC * * 23


Apologies- that was posted in error!

Thankyou both for your input. I now have another question based on
this code.Using this code I have a workbook with a worksheet in it
called 'data'. The format is shown below:

B K S
1 YES/NO CODE VALUE
2 Y ABC 10
3 Y ABC 2
4 N ABB 44
5 N ABC 23

I also have another sheet called 'output' . What I would like to do is
sum all of the code values based on the Y/N criteria. For example in
this case. The output would be:

B K S
1 YES/NO CODE VALUE
2 Y ABC 12

If the data changed to:

B K S
1 YES/NO CODE VALUE
2 Y ABC 10
3 Y ABC 2
4 Y ABB 44
5 N ABC 23

Then the output would be:


B K S
1 YES/NO CODE VALUE
2 Y ABC 12
3 Y ABB 44

and so on. The data is in columns B (Y/N), K (CODE), and S(Value).

Do you have any idea how I can make this work?

Thanks in advance for all your help,

Regards

Joseph Crabtree





  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default VBA SUM Function

I'm not sure exactly what you have in mind. There are a few different
methods people ask for in similar situations.

1) If you want to create the table in the output and don't havve all the
codes. then what you need to do is to get unique codes. On a worksheet you
can go to the menu - Data - Advance filter. Select the original data table
to the code colun. Then select copy to a new location. Specify the new
location, and also select Unique. You can record a macro will performing
this operation.

2) Once you have the unique values in the outp[ut table then you can add the
Y or N in the column B and the formula in column S.

----------------------------------------------------------------------------
Now if you already have the table and want to condense the table yo would
want to replace the formula with values. Then combine rows that have the
same code. You would need to sort the table to make sure items with the same
code are adjacent. Then it is a simple macro to combine the rows and add the
values.

I will help with anything you want. But I want to make sure I'm writing
the macro you want.

"joecrabtree" wrote:

On Mar 26, 8:17 am, joecrabtree wrote:
On Mar 25, 2:29 pm, joel wrote:



You can also put the sumproduct formula in the worksheet


from
MyFormula = "Sumproduct(" & _
"--(" & CodeRange.Address(external:=true) & "=""" _
& .Range("K" & r).Value & """)," & _
"--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
SumRange.Address(external:=true) & ")"


to (added equal sign in front of forumal)
MyFormula = "=Sumproduct(" & _
"--(" & CodeRange.Address(external:=true) & "=""" _
& .Range("K" & r).Value & """)," & _
"--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
SumRange.Address(external:=true) & ")"
Range("A1").formula = Myformula


"Dave Peterson" wrote:
Got it working means that
Total = Application.Evaluate(MyFormula)
actually evaluated to the correct number?


If yes, then maybe something like after this line:


Total = Application.Evaluate(MyFormula)
..Range("X" & r).Value = total


joecrabtree wrote:


<<snipped


Thanks for all your help. Ive now got this working, and understand how
it works. However I havent been able to get an output, i.e. displayed
sumproduct value. For example how would I be able to get this to
display the outputs an output worksheet?


Thanks again,


Joe Crabtree


--


Dave Peterson


All,

Thankyou both for your input. I now have another question based on
this code.Using this code I have a workbook with a worksheet in it
called 'data'. The format is shown below:

Column Letter B K S
1 YES/NO CODE VALUE
2 Y ABC 10
3 Y ABC 2
4 N ABB 44
5 N ABC 23


Apologies- that was posted in error!

Thankyou both for your input. I now have another question based on
this code.Using this code I have a workbook with a worksheet in it
called 'data'. The format is shown below:

B K S
1 YES/NO CODE VALUE
2 Y ABC 10
3 Y ABC 2
4 N ABB 44
5 N ABC 23

I also have another sheet called 'output' . What I would like to do is
sum all of the code values based on the Y/N criteria. For example in
this case. The output would be:

B K S
1 YES/NO CODE VALUE
2 Y ABC 12

If the data changed to:

B K S
1 YES/NO CODE VALUE
2 Y ABC 10
3 Y ABC 2
4 Y ABB 44
5 N ABC 23

Then the output would be:


B K S
1 YES/NO CODE VALUE
2 Y ABC 12
3 Y ABB 44

and so on. The data is in columns B (Y/N), K (CODE), and S(Value).

Do you have any idea how I can make this work?

Thanks in advance for all your help,

Regards

Joseph Crabtree






  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 111
Default VBA SUM Function

On Mar 26, 11:19*am, joel wrote:
I'm not sure exactly what you have in mind. *There are a few different
methods people ask for in similar situations.

1) If you want to create the table in the output and don't havve all the
codes. *then what you need to do is to get unique codes. *On a worksheet you
can go to the menu - Data - Advance filter. *Select the original data table
to the code colun. *Then select copy to a new location. *Specify the new
location, and also select Unique. *You can record a macro will performing
this operation.

2) Once you have the unique values in the outp[ut table then you can add the
Y or N in the column B and the formula in column S.

----------------------------------------------------------------------------
Now if you already have the table and want to condense the table yo would
want to replace the formula with values. *Then combine rows that have the
same code. *You would need to sort the table to make sure items with the same
code are adjacent. *Then it is a simple macro to combine the rows and add the
values.

I will help with anything you want. * But I want to make sure I'm writing
the macro you want.

"joecrabtree" wrote:
On Mar 26, 8:17 am, joecrabtree wrote:
On Mar 25, 2:29 pm, joel wrote:


You can also put the sumproduct formula in the worksheet


from
*MyFormula = "Sumproduct(" & _
* * * "--(" & CodeRange.Address(external:=true) & "=""" _
* * * * * * * * & .Range("K" & r).Value & """)," *& _
* * * "--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
* * * SumRange.Address(external:=true) & ")"


to (added equal sign in front of forumal)
*MyFormula = "=Sumproduct(" & _
* * * "--(" & CodeRange.Address(external:=true) & "=""" _
* * * * * * * * & .Range("K" & r).Value & """)," *& _
* * * "--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
* * * SumRange.Address(external:=true) & ")"
*Range("A1").formula = Myformula


"Dave Peterson" wrote:
Got it working means that
Total = Application.Evaluate(MyFormula)
actually evaluated to the correct number?


If yes, then maybe something like after this line:


Total = Application.Evaluate(MyFormula)
..Range("X" & r).Value = total


joecrabtree wrote:


<<snipped


Thanks for all your help. Ive now got this working, and understand how
it works. However I havent been able to get an output, i.e. displayed
sumproduct value. For example how would I be able to get this to
display the outputs an output worksheet?


Thanks again,


Joe Crabtree


--


Dave Peterson


All,


Thankyou both for your input. I now have another question based on
this code.Using this code I have a workbook with a worksheet in it
called 'data'. The format is shown below:


Column Letter * B * * * K * * * S
1 * * * YES/NO *CODE * *VALUE
2 * * * Y * * * ABC * * 10
3 * * * Y * * * ABC * * 2
4 * * * N * * * ABB * * 44
5 * * * N * * * ABC * * 23


Apologies- that was posted in error!


Thankyou both for your input. I now have another question based on
this code.Using this code I have a workbook with a worksheet in it
called 'data'. The format is shown below:


* * * *B * * * * * * * K * * * * * * * * S
1 *YES/NO *CODE * *VALUE
2 *Y * * * * * * * *ABC * *10
3 *Y * * * * * * * *ABC * *2
4 *N * * * * * * * *ABB * * * * * *44
5 *N * * * * * * * * ABC * 23


I also have another sheet called 'output' . What I would like to do is
sum all of the code values based on the Y/N criteria. For example in
this case. The output would be:


* * * *B * * * * * * * K * * * * * * * * S
1 *YES/NO *CODE * *VALUE
2 *Y * * * * * * * *ABC * *12


If the data changed to:


* * * *B * * * * * * * K * * * * * * * * S
1 *YES/NO *CODE * *VALUE
2 *Y * * * * * * * *ABC * *10
3 *Y * * * * * * * *ABC * *2
4 *Y * * * * * * * *ABB * * * * * *44
5 *N * * * * * * * * ABC * 23


Then the output would be:


* * * *B * * * * * * * K * * * * * * * * S
1 *YES/NO *CODE * *VALUE
2 *Y * * * * * * * *ABC * *12
3 *Y * * * * * * * *ABB * *44


and so on. The data is in columns B (Y/N), K (CODE), and S(Value).


Do you have any idea how I can make this work?


Thanks in advance for all your help,


Regards


Joseph Crabtree


My idea was to do the whole thing using VBA, i.e completed automated.
The ideal solution would be the first one that you suggested:

1) If you want to create the table in the output and don't havve all
the
codes. then what you need to do is to get unique codes. On a
worksheet you
can go to the menu - Data - Advance filter. Select the original data
table
to the code colun. Then select copy to a new location. Specify the
new
location, and also select Unique. You can record a macro will
performing
this operation.

2) Once you have the unique values in the outp[ut table then you can
add the
Y or N in the column B and the formula in column S.

Any help you could give would be much appreciate.

Regards

Joe Crabtree
  #15   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default VBA SUM Function

Dave and I have shown you the SUMPRODUCT and you keep on going back to the
SUMIF which won't work.

"joecrabtree" wrote:

On Mar 26, 11:19 am, joel wrote:
I'm not sure exactly what you have in mind. There are a few different
methods people ask for in similar situations.

1) If you want to create the table in the output and don't havve all the
codes. then what you need to do is to get unique codes. On a worksheet you
can go to the menu - Data - Advance filter. Select the original data table
to the code colun. Then select copy to a new location. Specify the new
location, and also select Unique. You can record a macro will performing
this operation.

2) Once you have the unique values in the outp[ut table then you can add the
Y or N in the column B and the formula in column S.

----------------------------------------------------------------------------
Now if you already have the table and want to condense the table yo would
want to replace the formula with values. Then combine rows that have the
same code. You would need to sort the table to make sure items with the same
code are adjacent. Then it is a simple macro to combine the rows and add the
values.

I will help with anything you want. But I want to make sure I'm writing
the macro you want.

"joecrabtree" wrote:
On Mar 26, 8:17 am, joecrabtree wrote:
On Mar 25, 2:29 pm, joel wrote:


You can also put the sumproduct formula in the worksheet


from
MyFormula = "Sumproduct(" & _
"--(" & CodeRange.Address(external:=true) & "=""" _
& .Range("K" & r).Value & """)," & _
"--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
SumRange.Address(external:=true) & ")"


to (added equal sign in front of forumal)
MyFormula = "=Sumproduct(" & _
"--(" & CodeRange.Address(external:=true) & "=""" _
& .Range("K" & r).Value & """)," & _
"--(" & CriteriaRange.Address(external:=true) & "=""Y"")," & _
SumRange.Address(external:=true) & ")"
Range("A1").formula = Myformula


"Dave Peterson" wrote:
Got it working means that
Total = Application.Evaluate(MyFormula)
actually evaluated to the correct number?


If yes, then maybe something like after this line:


Total = Application.Evaluate(MyFormula)
..Range("X" & r).Value = total


joecrabtree wrote:


<<snipped


Thanks for all your help. Ive now got this working, and understand how
it works. However I havent been able to get an output, i.e. displayed
sumproduct value. For example how would I be able to get this to
display the outputs an output worksheet?


Thanks again,


Joe Crabtree


--


Dave Peterson


All,


Thankyou both for your input. I now have another question based on
this code.Using this code I have a workbook with a worksheet in it
called 'data'. The format is shown below:


Column Letter B K S
1 YES/NO CODE VALUE
2 Y ABC 10
3 Y ABC 2
4 N ABB 44
5 N ABC 23


Apologies- that was posted in error!


Thankyou both for your input. I now have another question based on
this code.Using this code I have a workbook with a worksheet in it
called 'data'. The format is shown below:


B K S
1 YES/NO CODE VALUE
2 Y ABC 10
3 Y ABC 2
4 N ABB 44
5 N ABC 23


I also have another sheet called 'output' . What I would like to do is
sum all of the code values based on the Y/N criteria. For example in
this case. The output would be:


B K S
1 YES/NO CODE VALUE
2 Y ABC 12


If the data changed to:


B K S
1 YES/NO CODE VALUE
2 Y ABC 10
3 Y ABC 2
4 Y ABB 44
5 N ABC 23


Then the output would be:


B K S
1 YES/NO CODE VALUE
2 Y ABC 12
3 Y ABB 44


and so on. The data is in columns B (Y/N), K (CODE), and S(Value).


Do you have any idea how I can make this work?


Thanks in advance for all your help,


Regards


Joseph Crabtree


My idea was to do the whole thing using VBA, i.e completed automated.
The ideal solution would be the first one that you suggested:

1) If you want to create the table in the output and don't havve all
the
codes. then what you need to do is to get unique codes. On a
worksheet you
can go to the menu - Data - Advance filter. Select the original data
table
to the code colun. Then select copy to a new location. Specify the
new
location, and also select Unique. You can record a macro will
performing
this operation.

2) Once you have the unique values in the outp[ut table then you can
add the
Y or N in the column B and the formula in column S.

Any help you could give would be much appreciate.

Regards

Joe Crabtree

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
Excel Data Validation/Lookup function does function correcty Kirkey Excel Worksheet Functions 2 May 25th 09 09:22 PM
User Function Question: Collect Condition in Dialog Box - But How toInsert into Function Equation? SteveM Excel Programming 1 January 3rd 08 03:45 PM
LINKEDRANGE function - a complement to the PULL function (for getting values from a closed workbook) [email protected] Excel Worksheet Functions 0 September 5th 06 03:44 PM
Excel - User Defined Function Error: This function takes no argume BruceInCalgary Excel Programming 3 August 23rd 06 08:53 PM
Adding a custom function to the default excel function list DonutDel Excel Programming 3 November 21st 03 03:41 PM


All times are GMT +1. The time now is 12:04 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"