Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.excel.misc,microsoft.public.fr.excel
external usenet poster
 
Posts: 2
Default How to write a VBA code to select certain rows based on certain conditions

I need help to write part of a VBA code in Excel. I am trying to do
calculations on rows that meeting certain requirements, but I do not
know how to established that in VBA. I know that if I use the
worksheet itself, I can do something like SUMIF() function. But, when
I put the same function in the VBA editor, it returns error. A simple
version of my data:

Group No Member No Amount spent
1 1 10
1 2 3
1 3 4
2 1 6
2 2 1
3 1 12
4 1 3
4 2 1

I am trying to calculate the total amount spent based on the group no.
FOr example, how much does group no.1 spent, how much does group no.2
spent, etc.

I appreciate anybody who can help me with this. Thank you very much.
  #2   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.excel.misc,microsoft.public.fr.excel
external usenet poster
 
Posts: 11,272
Default How to write a VBA code to select certain rows based on certain conditions

Use something like

var1 = WorksheetFunction.SUMIF(range1, value, Range2)

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

wrote in message
om...
I need help to write part of a VBA code in Excel. I am trying to do
calculations on rows that meeting certain requirements, but I do not
know how to established that in VBA. I know that if I use the
worksheet itself, I can do something like SUMIF() function. But, when
I put the same function in the VBA editor, it returns error. A simple
version of my data:

Group No Member No Amount spent
1 1 10
1 2 3
1 3 4
2 1 6
2 2 1
3 1 12
4 1 3
4 2 1

I am trying to calculate the total amount spent based on the group no.
FOr example, how much does group no.1 spent, how much does group no.2
spent, etc.

I appreciate anybody who can help me with this. Thank you very much.



  #3   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.excel.misc,microsoft.public.fr.excel
external usenet poster
 
Posts: 135
Default How to write a VBA code to select certain rows based on certain conditions

Hi houstoncity2004,


Try this :

'------------------
Sub SommeIF()

With Worksheets("Sheet1")
Group1 = Application.SumIf(.Range("A1:A10"), "=" & 1, .Range("B1:B10"))
Group2 = Application.SumIf(.Range("A1:A10"), "=" & 2, .Range("B1:B10"))
End With

End Sub
'------------------


Salutations!




a écrit dans le message de om...
I need help to write part of a VBA code in Excel. I am trying to do
calculations on rows that meeting certain requirements, but I do not
know how to established that in VBA. I know that if I use the
worksheet itself, I can do something like SUMIF() function. But, when
I put the same function in the VBA editor, it returns error. A simple
version of my data:

Group No Member No Amount spent
1 1 10
1 2 3
1 3 4
2 1 6
2 2 1
3 1 12
4 1 3
4 2 1

I am trying to calculate the total amount spent based on the group no.
FOr example, how much does group no.1 spent, how much does group no.2
spent, etc.

I appreciate anybody who can help me with this. Thank you very much.


  #4   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.excel.misc,microsoft.public.fr.excel
external usenet poster
 
Posts: 4,624
Default How to write a VBA code to select certain rows based on certain conditions

First, while I assume you have a reason for wanting to do this in VBA,
I'd feel remiss if I didn't suggest you look at Pivot Tables instead (see

http://peltiertech.com/Excel/Pivots/pivotstart.htm

for an intro).

You can use the SUMIF function in VBA:

Dim dResult As Double
dResult = Application.SumIf(Range("A1:A10"), "=1", Range("C1:C10"))



In article ,
) wrote:

I need help to write part of a VBA code in Excel. I am trying to do
calculations on rows that meeting certain requirements, but I do not
know how to established that in VBA. I know that if I use the
worksheet itself, I can do something like SUMIF() function. But, when
I put the same function in the VBA editor, it returns error. A simple
version of my data:

Group No Member No Amount spent
1 1 10
1 2 3
1 3 4
2 1 6
2 2 1
3 1 12
4 1 3
4 2 1

I am trying to calculate the total amount spent based on the group no.
FOr example, how much does group no.1 spent, how much does group no.2
spent, etc.

I appreciate anybody who can help me with this. Thank you very much.

  #5   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.excel.misc,microsoft.public.fr.excel
external usenet poster
 
Posts: 3
Default How to write a VBA code to select certain rows based on certain conditions

Hi houstoncity2004 ;-))
as i understand the simple version of your data :
you can use something like this :

Sub Macro1()
' Macro enregistrée le 24/06/2004 par GeeDee
Range("A2:A100").Name = "Group_no"
Range("C2:C100").Name = "Amount_spent"
'----a loop may be better used for large number of Group No----
Range("G1").Name = "Group1"
Range("G2").Name = "Group2"
Range("G3").Name = "Group3"
Range("G4").Name = "Group4"
Range("Group1").Formula = "=SUMPRODUCT(Amount_spent*(Group_no=1))"
Range("Group2").Formula = "=SUMPRODUCT(Amount_spent*(Group_no=2))"
Range("Group3").Formula = "=SUMPRODUCT(Amount_spent*(Group_no=3))"
Range("Group4").Formula = "=SUMPRODUCT(Amount_spent*(Group_no=4))"
'-----------
End Sub

@+ Modeste(®) GeeDee
HTH
Much more tips and tricks on the best "Excel Addicts" Site in french
language
http://www.excelabo.net



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
Write a macro that hides rows based on empty cells Mac0001UK Excel Discussion (Misc queries) 3 March 10th 09 12:37 PM
VBA code to select rows ub Excel Discussion (Misc queries) 4 February 15th 09 11:05 PM
+/- in sheet2 based on conditions and dynamic rows in sheet1 deepika :excel help[_2_] Excel Discussion (Misc queries) 0 February 4th 08 08:33 AM
Conditionally summing cells based on conditions in other rows Bert Excel Worksheet Functions 3 June 20th 06 11:06 AM
How do I write a formula to color code based on dates provided or. jaime Excel Worksheet Functions 2 February 17th 05 12:41 AM


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

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

About Us

"It's about Microsoft Excel"