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

G'day everbody Help!
A B C D E
3A 4E 5A 6D Answer 18

On this row the procedure below works a treat,what I am trying for is to Sum
only the cells with "a" in mid(myrange1,2,1) Answer 8.Have tried a multitude
of different formulas but alas I admit defeat. Our dog will be gratefull for
any help as he cops my bad mood when it does not work. Regards John

Private Sub UserForm_Activate()
Dim myrange1 As Range
Dim a As Variant
Set myrange1 = Range(Selection.EntireRow.Cells(1, 4), Selection)
a = Evaluate("sumproduct(--left(0&" & myrange1.Address & ",2))")
msgbox a
end sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default VBA Sum Product

You can do it with a simple function in VBA:

Function AddUp(RR As Range, Alpha As String) As Long
Dim Total As Long
Dim R As Range
For Each R In RR.Cells
If Len(R.Text) = 2 Then
If StrComp(Right(R.Text, 1), Alpha, vbTextCompare) = 0
Then
Total = Total + CLng(Left(R.Text, 1))
End If
End If
Next R
AddUp = Total
End Function

You can call this from a cell with

=AddUp(A1:D1,"A")

or from other VBA code with

Dim Res As Long
Res = AddUp(Range("A1:D1"),"A")

RR is the range of cells to examine and Alpha is the character to test
for (case insensitive -- change vbTextCompare to vbBinaryCompare if
you need case sensitivity).

I hope this makes your dog happy.


Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Mon, 8 Dec 2008 15:38:01 -0800, John L
wrote:

G'day everbody Help!
A B C D E
3A 4E 5A 6D Answer 18

On this row the procedure below works a treat,what I am trying for is to Sum
only the cells with "a" in mid(myrange1,2,1) Answer 8.Have tried a multitude
of different formulas but alas I admit defeat. Our dog will be gratefull for
any help as he cops my bad mood when it does not work. Regards John

Private Sub UserForm_Activate()
Dim myrange1 As Range
Dim a As Variant
Set myrange1 = Range(Selection.EntireRow.Cells(1, 4), Selection)
a = Evaluate("sumproduct(--left(0&" & myrange1.Address & ",2))")
msgbox a
end sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default VBA Sum Product

Thanks Chip but my excel does not recognise AddUp ?

I am at work so dog is safe
Regards John

"Chip Pearson" wrote:

You can do it with a simple function in VBA:

Function AddUp(RR As Range, Alpha As String) As Long
Dim Total As Long
Dim R As Range
For Each R In RR.Cells
If Len(R.Text) = 2 Then
If StrComp(Right(R.Text, 1), Alpha, vbTextCompare) = 0
Then
Total = Total + CLng(Left(R.Text, 1))
End If
End If
Next R
AddUp = Total
End Function

You can call this from a cell with

=AddUp(A1:D1,"A")

or from other VBA code with

Dim Res As Long
Res = AddUp(Range("A1:D1"),"A")

RR is the range of cells to examine and Alpha is the character to test
for (case insensitive -- change vbTextCompare to vbBinaryCompare if
you need case sensitivity).

I hope this makes your dog happy.


Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Mon, 8 Dec 2008 15:38:01 -0800, John L
wrote:

G'day everbody Help!
A B C D E
3A 4E 5A 6D Answer 18

On this row the procedure below works a treat,what I am trying for is to Sum
only the cells with "a" in mid(myrange1,2,1) Answer 8.Have tried a multitude
of different formulas but alas I admit defeat. Our dog will be gratefull for
any help as he cops my bad mood when it does not work. Regards John

Private Sub UserForm_Activate()
Dim myrange1 As Range
Dim a As Variant
Set myrange1 = Range(Selection.EntireRow.Cells(1, 4), Selection)
a = Evaluate("sumproduct(--left(0&" & myrange1.Address & ",2))")
msgbox a
end sub


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default VBA Sum Product


Did you put Chip's code for that =AddUp() UDF in a general module in that
workbook's project?

If you're new to macros:

Debra Dalgleish has some notes how to implement macros he
http://www.contextures.com/xlvba01.html

David McRitchie has an intro to macros:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Ron de Bruin's intro to macros:
http://www.rondebruin.nl/code.htm

(General, Regular and Standard modules all describe the same thing.)

Debra Dalgleish has some getstarted instructions for userforms at:
http://contextures.com/xlUserForm01.html

John L wrote:

Thanks Chip but my excel does not recognise AddUp ?

I am at work so dog is safe
Regards John

"Chip Pearson" wrote:

You can do it with a simple function in VBA:

Function AddUp(RR As Range, Alpha As String) As Long
Dim Total As Long
Dim R As Range
For Each R In RR.Cells
If Len(R.Text) = 2 Then
If StrComp(Right(R.Text, 1), Alpha, vbTextCompare) = 0
Then
Total = Total + CLng(Left(R.Text, 1))
End If
End If
Next R
AddUp = Total
End Function

You can call this from a cell with

=AddUp(A1:D1,"A")

or from other VBA code with

Dim Res As Long
Res = AddUp(Range("A1:D1"),"A")

RR is the range of cells to examine and Alpha is the character to test
for (case insensitive -- change vbTextCompare to vbBinaryCompare if
you need case sensitivity).

I hope this makes your dog happy.


Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Mon, 8 Dec 2008 15:38:01 -0800, John L
wrote:

G'day everbody Help!
A B C D E
3A 4E 5A 6D Answer 18

On this row the procedure below works a treat,what I am trying for is to Sum
only the cells with "a" in mid(myrange1,2,1) Answer 8.Have tried a multitude
of different formulas but alas I admit defeat. Our dog will be gratefull for
any help as he cops my bad mood when it does not work. Regards John

Private Sub UserForm_Activate()
Dim myrange1 As Range
Dim a As Variant
Set myrange1 = Range(Selection.EntireRow.Cells(1, 4), Selection)
a = Evaluate("sumproduct(--left(0&" & myrange1.Address & ",2))")
msgbox a
end sub



--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default VBA Sum Product

You need to put the function code in a standard VBA module. Open the
VBA editor (ALT F11), then open the Project window (CTRL R) if it is
not already visible (typically docked on the left side of the screen).
Select your workbook project in the TreeView in the Project window,
then go to the Insert menu and choose "Module". This will create a new
code module named "Module1" in your workbook project. Paste the AddUp
code in that module and the close the editor and return to Excel. With
the function code in a module, you can call it from a worksheet cell
with


=AddUp(A1:D1,"A")

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Mon, 8 Dec 2008 17:31:01 -0800, John L
wrote:

Thanks Chip but my excel does not recognise AddUp ?

I am at work so dog is safe
Regards John

"Chip Pearson" wrote:

You can do it with a simple function in VBA:

Function AddUp(RR As Range, Alpha As String) As Long
Dim Total As Long
Dim R As Range
For Each R In RR.Cells
If Len(R.Text) = 2 Then
If StrComp(Right(R.Text, 1), Alpha, vbTextCompare) = 0
Then
Total = Total + CLng(Left(R.Text, 1))
End If
End If
Next R
AddUp = Total
End Function

You can call this from a cell with

=AddUp(A1:D1,"A")

or from other VBA code with

Dim Res As Long
Res = AddUp(Range("A1:D1"),"A")

RR is the range of cells to examine and Alpha is the character to test
for (case insensitive -- change vbTextCompare to vbBinaryCompare if
you need case sensitivity).

I hope this makes your dog happy.


Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Mon, 8 Dec 2008 15:38:01 -0800, John L
wrote:

G'day everbody Help!
A B C D E
3A 4E 5A 6D Answer 18

On this row the procedure below works a treat,what I am trying for is to Sum
only the cells with "a" in mid(myrange1,2,1) Answer 8.Have tried a multitude
of different formulas but alas I admit defeat. Our dog will be gratefull for
any help as he cops my bad mood when it does not work. Regards John

Private Sub UserForm_Activate()
Dim myrange1 As Range
Dim a As Variant
Set myrange1 = Range(Selection.EntireRow.Cells(1, 4), Selection)
a = Evaluate("sumproduct(--left(0&" & myrange1.Address & ",2))")
msgbox a
end sub




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default VBA Sum Product


Thanks Chip, Being self taught I have never used Functions and what you have
explained is great and works well ,and can be used in a few other sheets
that I have done.But in this sheet I need the answer in a label in a
userform, have tried to use your worksheet function in the userform but no go.
Please keep the answers rolling, the dog is happy.
Regards John

"Chip Pearson" wrote:

You need to put the function code in a standard VBA module. Open the
VBA editor (ALT F11), then open the Project window (CTRL R) if it is
not already visible (typically docked on the left side of the screen).
Select your workbook project in the TreeView in the Project window,
then go to the Insert menu and choose "Module". This will create a new
code module named "Module1" in your workbook project. Paste the AddUp
code in that module and the close the editor and return to Excel. With
the function code in a module, you can call it from a worksheet cell
with


=AddUp(A1:D1,"A")

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Mon, 8 Dec 2008 17:31:01 -0800, John L
wrote:

Thanks Chip but my excel does not recognise AddUp ?

I am at work so dog is safe
Regards John

"Chip Pearson" wrote:

You can do it with a simple function in VBA:

Function AddUp(RR As Range, Alpha As String) As Long
Dim Total As Long
Dim R As Range
For Each R In RR.Cells
If Len(R.Text) = 2 Then
If StrComp(Right(R.Text, 1), Alpha, vbTextCompare) = 0
Then
Total = Total + CLng(Left(R.Text, 1))
End If
End If
Next R
AddUp = Total
End Function

You can call this from a cell with

=AddUp(A1:D1,"A")

or from other VBA code with

Dim Res As Long
Res = AddUp(Range("A1:D1"),"A")

RR is the range of cells to examine and Alpha is the character to test
for (case insensitive -- change vbTextCompare to vbBinaryCompare if
you need case sensitivity).

I hope this makes your dog happy.


Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Mon, 8 Dec 2008 15:38:01 -0800, John L
wrote:

G'day everbody Help!
A B C D E
3A 4E 5A 6D Answer 18

On this row the procedure below works a treat,what I am trying for is to Sum
only the cells with "a" in mid(myrange1,2,1) Answer 8.Have tried a multitude
of different formulas but alas I admit defeat. Our dog will be gratefull for
any help as he cops my bad mood when it does not work. Regards John

Private Sub UserForm_Activate()
Dim myrange1 As Range
Dim a As Variant
Set myrange1 = Range(Selection.EntireRow.Cells(1, 4), Selection)
a = Evaluate("sumproduct(--left(0&" & myrange1.Address & ",2))")
msgbox a
end sub


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default VBA Sum Product


Chip no need to answer I found this in your other answer
or from other VBA code with
Dim Res As Long
Res = AddUp(Range("A1:D1"),"A")

And this is great Many Many Thanks and Jack(the Dog) is Very Very Happy
Regards John

"Chip Pearson" wrote:

You need to put the function code in a standard VBA module. Open the
VBA editor (ALT F11), then open the Project window (CTRL R) if it is
not already visible (typically docked on the left side of the screen).
Select your workbook project in the TreeView in the Project window,
then go to the Insert menu and choose "Module". This will create a new
code module named "Module1" in your workbook project. Paste the AddUp
code in that module and the close the editor and return to Excel. With
the function code in a module, you can call it from a worksheet cell
with


=AddUp(A1:D1,"A")

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Mon, 8 Dec 2008 17:31:01 -0800, John L
wrote:

Thanks Chip but my excel does not recognise AddUp ?

I am at work so dog is safe
Regards John

"Chip Pearson" wrote:

You can do it with a simple function in VBA:

Function AddUp(RR As Range, Alpha As String) As Long
Dim Total As Long
Dim R As Range
For Each R In RR.Cells
If Len(R.Text) = 2 Then
If StrComp(Right(R.Text, 1), Alpha, vbTextCompare) = 0
Then
Total = Total + CLng(Left(R.Text, 1))
End If
End If
Next R
AddUp = Total
End Function

You can call this from a cell with

=AddUp(A1:D1,"A")

or from other VBA code with

Dim Res As Long
Res = AddUp(Range("A1:D1"),"A")

RR is the range of cells to examine and Alpha is the character to test
for (case insensitive -- change vbTextCompare to vbBinaryCompare if
you need case sensitivity).

I hope this makes your dog happy.


Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Mon, 8 Dec 2008 15:38:01 -0800, John L
wrote:

G'day everbody Help!
A B C D E
3A 4E 5A 6D Answer 18

On this row the procedure below works a treat,what I am trying for is to Sum
only the cells with "a" in mid(myrange1,2,1) Answer 8.Have tried a multitude
of different formulas but alas I admit defeat. Our dog will be gratefull for
any help as he cops my bad mood when it does not work. Regards John

Private Sub UserForm_Activate()
Dim myrange1 As Range
Dim a As Variant
Set myrange1 = Range(Selection.EntireRow.Cells(1, 4), Selection)
a = Evaluate("sumproduct(--left(0&" & myrange1.Address & ",2))")
msgbox a
end sub


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
Product Code and Product Description setup Nastyashman Excel Worksheet Functions 4 July 6th 09 05:48 PM
I need a product key for my Trail product, 2007 Microsoft Office s sltchsyi Setting up and Configuration of Excel 0 May 4th 09 01:32 AM
Sum Product flds New Users to Excel 2 June 20th 08 08:22 PM
Convert numbers to text (from product id # to name of product) [email protected] Excel Programming 1 September 12th 07 09:58 PM
Vlookup code product and to copy commentary with photo of the product in vba [email protected] Excel Programming 0 October 2nd 06 03:54 AM


All times are GMT +1. The time now is 02:18 AM.

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"