Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 504
Default sum of text positions

Range A1= 15,10,1,8,1,9,0,1
Range A2= 15,10,1,8,1,9,0,1

I need 8 different formulas for A3-A10 to get the sum of each position.
So A3 would only get the sum of the first number in A1 and A2 (30)
A4 would get the sum of the second number in A1 and A2 (20)
A5=2
A6=16
A7=2
and so on...

Is that do-able?

Thanks,
Kevin
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 791
Default sum of text positions

The Numbers you have is a string isn't it?
You could try something like this:
=SUM(VALUE(MID(A1,1,2)),VALUE(MID(A1,1,2)))
Granted in my example the number of digits is always 2; how often will the
numbers change? you can substitute the Last Parameter for a Find, searching
for a comma.
--
If this posting was helpful, please click on the Yes button.
Regards,

Michael Arch.




"Kevin" wrote:

Range A1= 15,10,1,8,1,9,0,1
Range A2= 15,10,1,8,1,9,0,1

I need 8 different formulas for A3-A10 to get the sum of each position.
So A3 would only get the sum of the first number in A1 and A2 (30)
A4 would get the sum of the second number in A1 and A2 (20)
A5=2
A6=16
A7=2
and so on...

Is that do-able?

Thanks,
Kevin

  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 791
Default sum of text positions

The sercond value should read A2 not A!, then you will have to adjust it for
the next set of values.
=SUM(VALUE(MID(A1,1,2)),VALUE(MID(A2,1,2)))
--
If this posting was helpful, please click on the Yes button.
Regards,

Michael Arch.




"Michael" wrote:

The Numbers you have is a string isn't it?
You could try something like this:
=SUM(VALUE(MID(A1,1,2)),VALUE(MID(A1,1,2)))
Granted in my example the number of digits is always 2; how often will the
numbers change? you can substitute the Last Parameter for a Find, searching
for a comma.
--
If this posting was helpful, please click on the Yes button.
Regards,

Michael Arch.




"Kevin" wrote:

Range A1= 15,10,1,8,1,9,0,1
Range A2= 15,10,1,8,1,9,0,1

I need 8 different formulas for A3-A10 to get the sum of each position.
So A3 would only get the sum of the first number in A1 and A2 (30)
A4 would get the sum of the second number in A1 and A2 (20)
A5=2
A6=16
A7=2
and so on...

Is that do-able?

Thanks,
Kevin

  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5,651
Default sum of text positions

On Thu, 11 Dec 2008 09:17:01 -0800, Kevin
wrote:

Range A1= 15,10,1,8,1,9,0,1
Range A2= 15,10,1,8,1,9,0,1

I need 8 different formulas for A3-A10 to get the sum of each position.
So A3 would only get the sum of the first number in A1 and A2 (30)
A4 would get the sum of the second number in A1 and A2 (20)
A5=2
A6=16
A7=2
and so on...

Is that do-able?

Thanks,
Kevin


There are a number of ways to do this.

One way is to use a UDF which interprets "Regular Expression".

To enter the UDF, <alt-F11 opens the VB Editor. Ensure your project is
highlighted in the Project Explorer window, then Insert/Module and paste the
code below into the window that opens.

To use this UDF, enter the following formula into A3 and fill down to A10.

If there are not "matching" entries in A1 and A2, the formula will return a
#VALUE! error.

A3: =RegexMid($A$1,"\d+",ROWS($1:1))+RegexMid($A$2,"\d +",ROWS($1:1))

============================================
Option Explicit
Function RegexMid(Str As String, Pattern As String, _
Optional Index As Variant = 1, _
Optional CaseSensitive As Boolean = True, _
Optional MultiLin As Boolean = False) _
As Variant 'Variant as value may be string or array


Dim objRegExp As Object
Dim objMatch As Object
Dim colMatches As Object

Dim i As Long 'counter
Dim T() As String 'container for array results

' Create a regular expression object.
Set objRegExp = CreateObject("vbscript.regexp")

'Set the pattern by using the Pattern property.
objRegExp.Pattern = Pattern

' Set Case Insensitivity.
objRegExp.IgnoreCase = Not CaseSensitive

'Set global applicability.
objRegExp.Global = True

'Set multiline
objRegExp.MultiLine = MultiLin

'Test whether the String can be compared.
If (objRegExp.Test(Str) = True) Then

'Get the matches.
Set colMatches = objRegExp.Execute(Str) ' Execute search.

On Error Resume Next 'return null string if a colmatch index is non-existent
If IsArray(Index) Then
ReDim T(1 To UBound(Index))
For i = 1 To UBound(Index)
T(i) = colMatches(Index(i) - 1)
Next i
RegexMid = T()
Else
RegexMid = CStr(colMatches(Index - 1))
If IsEmpty(RegexMid) Then RegexMid = ""
End If
On Error GoTo 0 'reset error handler
Else
RegexMid = ""
End If
End Function
=======================================
--ron
  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 504
Default sum of text positions

The digits could change at any time I was looking at find but I'm not sure
how to get it to find anything but the first instance.

"Michael" wrote:

The sercond value should read A2 not A!, then you will have to adjust it for
the next set of values.
=SUM(VALUE(MID(A1,1,2)),VALUE(MID(A2,1,2)))
--
If this posting was helpful, please click on the Yes button.
Regards,

Michael Arch.




"Michael" wrote:

The Numbers you have is a string isn't it?
You could try something like this:
=SUM(VALUE(MID(A1,1,2)),VALUE(MID(A1,1,2)))
Granted in my example the number of digits is always 2; how often will the
numbers change? you can substitute the Last Parameter for a Find, searching
for a comma.
--
If this posting was helpful, please click on the Yes button.
Regards,

Michael Arch.




"Kevin" wrote:

Range A1= 15,10,1,8,1,9,0,1
Range A2= 15,10,1,8,1,9,0,1

I need 8 different formulas for A3-A10 to get the sum of each position.
So A3 would only get the sum of the first number in A1 and A2 (30)
A4 would get the sum of the second number in A1 and A2 (20)
A5=2
A6=16
A7=2
and so on...

Is that do-able?

Thanks,
Kevin



  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5,651
Default sum of text positions

On Thu, 11 Dec 2008 13:50:27 -0500, Ron Rosenfeld
wrote:

On Thu, 11 Dec 2008 09:17:01 -0800, Kevin
wrote:

Range A1= 15,10,1,8,1,9,0,1
Range A2= 15,10,1,8,1,9,0,1

I need 8 different formulas for A3-A10 to get the sum of each position.
So A3 would only get the sum of the first number in A1 and A2 (30)
A4 would get the sum of the second number in A1 and A2 (20)
A5=2
A6=16
A7=2
and so on...

Is that do-able?

Thanks,
Kevin


There are a number of ways to do this.

One way is to use a UDF which interprets "Regular Expression".

To enter the UDF, <alt-F11 opens the VB Editor. Ensure your project is
highlighted in the Project Explorer window, then Insert/Module and paste the
code below into the window that opens.

To use this UDF, enter the following formula into A3 and fill down to A10.

If there are not "matching" entries in A1 and A2, the formula will return a
#VALUE! error.

A3: =RegexMid($A$1,"\d+",ROWS($1:1))+RegexMid($A$2,"\d +",ROWS($1:1))

============================================
Option Explicit
Function RegexMid(Str As String, Pattern As String, _
Optional Index As Variant = 1, _
Optional CaseSensitive As Boolean = True, _
Optional MultiLin As Boolean = False) _
As Variant 'Variant as value may be string or array


Dim objRegExp As Object
Dim objMatch As Object
Dim colMatches As Object

Dim i As Long 'counter
Dim T() As String 'container for array results

' Create a regular expression object.
Set objRegExp = CreateObject("vbscript.regexp")

'Set the pattern by using the Pattern property.
objRegExp.Pattern = Pattern

' Set Case Insensitivity.
objRegExp.IgnoreCase = Not CaseSensitive

'Set global applicability.
objRegExp.Global = True

'Set multiline
objRegExp.MultiLine = MultiLin

'Test whether the String can be compared.
If (objRegExp.Test(Str) = True) Then

'Get the matches.
Set colMatches = objRegExp.Execute(Str) ' Execute search.

On Error Resume Next 'return null string if a colmatch index is non-existent
If IsArray(Index) Then
ReDim T(1 To UBound(Index))
For i = 1 To UBound(Index)
T(i) = colMatches(Index(i) - 1)
Next i
RegexMid = T()
Else
RegexMid = CStr(colMatches(Index - 1))
If IsEmpty(RegexMid) Then RegexMid = ""
End If
On Error GoTo 0 'reset error handler
Else
RegexMid = ""
End If
End Function
=======================================
--ron



Another consideration:

If your values are not all integers; in other words, if some of them are
include decimals, then you may want to use this formula instead, to split the
string up at the commas:

=regexmid($A$1,"[^,]+",ROWS($1:1))+regexmid($A$2,"[^,]+",ROWS($1:1))

--ron
  #7   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 504
Default sum of text positions

Well, that works and I like it, however when I stated my example I simplified
it thinking I could change the formula prett easy. In actuallity the cells
that need to be added are
C4, E4, G4, I4, K4, M4, O4, Q4, S4, U4, W4, Y4, AA4, AC4, AE4, AG4

That being the case how would the RegexMid formula be written?


"Ron Rosenfeld" wrote:

On Thu, 11 Dec 2008 13:50:27 -0500, Ron Rosenfeld
wrote:

On Thu, 11 Dec 2008 09:17:01 -0800, Kevin
wrote:

Range A1= 15,10,1,8,1,9,0,1
Range A2= 15,10,1,8,1,9,0,1

I need 8 different formulas for A3-A10 to get the sum of each position.
So A3 would only get the sum of the first number in A1 and A2 (30)
A4 would get the sum of the second number in A1 and A2 (20)
A5=2
A6=16
A7=2
and so on...

Is that do-able?

Thanks,
Kevin


There are a number of ways to do this.

One way is to use a UDF which interprets "Regular Expression".

To enter the UDF, <alt-F11 opens the VB Editor. Ensure your project is
highlighted in the Project Explorer window, then Insert/Module and paste the
code below into the window that opens.

To use this UDF, enter the following formula into A3 and fill down to A10.

If there are not "matching" entries in A1 and A2, the formula will return a
#VALUE! error.

A3: =RegexMid($A$1,"\d+",ROWS($1:1))+RegexMid($A$2,"\d +",ROWS($1:1))

============================================
Option Explicit
Function RegexMid(Str As String, Pattern As String, _
Optional Index As Variant = 1, _
Optional CaseSensitive As Boolean = True, _
Optional MultiLin As Boolean = False) _
As Variant 'Variant as value may be string or array


Dim objRegExp As Object
Dim objMatch As Object
Dim colMatches As Object

Dim i As Long 'counter
Dim T() As String 'container for array results

' Create a regular expression object.
Set objRegExp = CreateObject("vbscript.regexp")

'Set the pattern by using the Pattern property.
objRegExp.Pattern = Pattern

' Set Case Insensitivity.
objRegExp.IgnoreCase = Not CaseSensitive

'Set global applicability.
objRegExp.Global = True

'Set multiline
objRegExp.MultiLine = MultiLin

'Test whether the String can be compared.
If (objRegExp.Test(Str) = True) Then

'Get the matches.
Set colMatches = objRegExp.Execute(Str) ' Execute search.

On Error Resume Next 'return null string if a colmatch index is non-existent
If IsArray(Index) Then
ReDim T(1 To UBound(Index))
For i = 1 To UBound(Index)
T(i) = colMatches(Index(i) - 1)
Next i
RegexMid = T()
Else
RegexMid = CStr(colMatches(Index - 1))
If IsEmpty(RegexMid) Then RegexMid = ""
End If
On Error GoTo 0 'reset error handler
Else
RegexMid = ""
End If
End Function
=======================================
--ron



Another consideration:

If your values are not all integers; in other words, if some of them are
include decimals, then you may want to use this formula instead, to split the
string up at the commas:

=regexmid($A$1,"[^,]+",ROWS($1:1))+regexmid($A$2,"[^,]+",ROWS($1:1))

--ron

  #8   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5,934
Default sum of text positions

Here is my take on a UDF solution (see notes after the code)...

Function AddCellNums(IndexNum As Long, ParamArray CellRef()) As Variant
Dim C As Variant
Dim CC As Range
If UBound(CellRef) = -1 Then
AddCellNums -CVErr(xlErrValue)
Exit Function
ElseIf Not TypeOf CellRef(LBound(CellRef)) Is Range Then
AddCellNums -CVErr(xlErrRef)
Exit Function
Else
On Error GoTo CancelFunction
For Each C In CellRef
If C.Count = 1 Then
AddCellNums = AddCellNums + CDbl(Split(C, ",")(IndexNum - 1))
Else
For Each CC In C
AddCellNums = AddCellNums + CDbl(Split(CC, ",")(IndexNum - 1))
Next
End If
Next
End If
Exit Function
CancelFunction:
AddCellNums = CVErr(xlErrNum)
End Function

This function can handle more than two cell... just put the range or cell
references in a comma delimited list after the item number in the list that
you want to find. So, if you wanted the sum of the 3 item in cells A1, A2
and A4, you would call the UDF with this formula...

=AddCellNums(3,A1:A2,A4)

However, since you will want to copy this formula down in order to get all
the individual sums, you will need to make the index number variable and the
cell references absolute. So, to get all the sum, use something like this...

=AddCellNums(ROW(A1),A$1:A$2,A$4)

and copy that down.

Note that if all the cells do not contain a list of values containing the
same number of values in each list, an error is generated.

Oh, and if you do not know how to install a UDF, follow this procedure.
Press Alt+F11 to get into the VB editor and then click Insert/Module from
its menu bar, then just copy/paste the above function into the code window
that opened up. That's it... you can now use the AddCellNums like any other
worksheet function (provided you feed it the correct arguments as outlined
above).

--
Rick (MVP - Excel)


"Kevin" wrote in message
...
Range A1= 15,10,1,8,1,9,0,1
Range A2= 15,10,1,8,1,9,0,1

I need 8 different formulas for A3-A10 to get the sum of each position.
So A3 would only get the sum of the first number in A1 and A2 (30)
A4 would get the sum of the second number in A1 and A2 (20)
A5=2
A6=16
A7=2
and so on...

Is that do-able?

Thanks,
Kevin


  #9   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5,934
Default sum of text positions

While waiting for Ron to modify his function, you might want to try out the
function I just posted. You would use this formula to call it...

=AddCellNums(ROW(A1),C4,E4,G4,I4,K4,M4,O4,Q4,S4,U4 ,W4,Y4,AA4,AC4,AE4,AG4)

and then copy it down as needed.

--
Rick (MVP - Excel)


"Kevin" wrote in message
...
Well, that works and I like it, however when I stated my example I
simplified
it thinking I could change the formula prett easy. In actuallity the
cells
that need to be added are
C4, E4, G4, I4, K4, M4, O4, Q4, S4, U4, W4, Y4, AA4, AC4, AE4, AG4

That being the case how would the RegexMid formula be written?


"Ron Rosenfeld" wrote:

On Thu, 11 Dec 2008 13:50:27 -0500, Ron Rosenfeld

wrote:

On Thu, 11 Dec 2008 09:17:01 -0800, Kevin

wrote:

Range A1= 15,10,1,8,1,9,0,1
Range A2= 15,10,1,8,1,9,0,1

I need 8 different formulas for A3-A10 to get the sum of each position.
So A3 would only get the sum of the first number in A1 and A2 (30)
A4 would get the sum of the second number in A1 and A2 (20)
A5=2
A6=16
A7=2
and so on...

Is that do-able?

Thanks,
Kevin

There are a number of ways to do this.

One way is to use a UDF which interprets "Regular Expression".

To enter the UDF, <alt-F11 opens the VB Editor. Ensure your project is
highlighted in the Project Explorer window, then Insert/Module and paste
the
code below into the window that opens.

To use this UDF, enter the following formula into A3 and fill down to
A10.

If there are not "matching" entries in A1 and A2, the formula will
return a
#VALUE! error.

A3: =RegexMid($A$1,"\d+",ROWS($1:1))+RegexMid($A$2,"\d +",ROWS($1:1))

============================================
Option Explicit
Function RegexMid(Str As String, Pattern As String, _
Optional Index As Variant = 1, _
Optional CaseSensitive As Boolean = True, _
Optional MultiLin As Boolean = False) _
As Variant 'Variant as value may be string or array


Dim objRegExp As Object
Dim objMatch As Object
Dim colMatches As Object

Dim i As Long 'counter
Dim T() As String 'container for array results

' Create a regular expression object.
Set objRegExp = CreateObject("vbscript.regexp")

'Set the pattern by using the Pattern property.
objRegExp.Pattern = Pattern

' Set Case Insensitivity.
objRegExp.IgnoreCase = Not CaseSensitive

'Set global applicability.
objRegExp.Global = True

'Set multiline
objRegExp.MultiLine = MultiLin

'Test whether the String can be compared.
If (objRegExp.Test(Str) = True) Then

'Get the matches.
Set colMatches = objRegExp.Execute(Str) ' Execute search.

On Error Resume Next 'return null string if a colmatch index is
non-existent
If IsArray(Index) Then
ReDim T(1 To UBound(Index))
For i = 1 To UBound(Index)
T(i) = colMatches(Index(i) - 1)
Next i
RegexMid = T()
Else
RegexMid = CStr(colMatches(Index - 1))
If IsEmpty(RegexMid) Then RegexMid = ""
End If
On Error GoTo 0 'reset error handler
Else
RegexMid = ""
End If
End Function
=======================================
--ron



Another consideration:

If your values are not all integers; in other words, if some of them are
include decimals, then you may want to use this formula instead, to split
the
string up at the commas:

=regexmid($A$1,"[^,]+",ROWS($1:1))+regexmid($A$2,"[^,]+",ROWS($1:1))

--ron


  #10   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 504
Default sum of text positions

Ok, this i can wrap my brain around. This works great for me. I dont need to
drag it down nessecarily these formulas will be on a template that gets
copied in so the formulas will already be there.

Thanks alot guys for your help. I learned some new stuff!!

Kevin

"Rick Rothstein" wrote:

Here is my take on a UDF solution (see notes after the code)...

Function AddCellNums(IndexNum As Long, ParamArray CellRef()) As Variant
Dim C As Variant
Dim CC As Range
If UBound(CellRef) = -1 Then
AddCellNums -CVErr(xlErrValue)
Exit Function
ElseIf Not TypeOf CellRef(LBound(CellRef)) Is Range Then
AddCellNums -CVErr(xlErrRef)
Exit Function
Else
On Error GoTo CancelFunction
For Each C In CellRef
If C.Count = 1 Then
AddCellNums = AddCellNums + CDbl(Split(C, ",")(IndexNum - 1))
Else
For Each CC In C
AddCellNums = AddCellNums + CDbl(Split(CC, ",")(IndexNum - 1))
Next
End If
Next
End If
Exit Function
CancelFunction:
AddCellNums = CVErr(xlErrNum)
End Function

This function can handle more than two cell... just put the range or cell
references in a comma delimited list after the item number in the list that
you want to find. So, if you wanted the sum of the 3 item in cells A1, A2
and A4, you would call the UDF with this formula...

=AddCellNums(3,A1:A2,A4)

However, since you will want to copy this formula down in order to get all
the individual sums, you will need to make the index number variable and the
cell references absolute. So, to get all the sum, use something like this...

=AddCellNums(ROW(A1),A$1:A$2,A$4)

and copy that down.

Note that if all the cells do not contain a list of values containing the
same number of values in each list, an error is generated.

Oh, and if you do not know how to install a UDF, follow this procedure.
Press Alt+F11 to get into the VB editor and then click Insert/Module from
its menu bar, then just copy/paste the above function into the code window
that opened up. That's it... you can now use the AddCellNums like any other
worksheet function (provided you feed it the correct arguments as outlined
above).

--
Rick (MVP - Excel)


"Kevin" wrote in message
...
Range A1= 15,10,1,8,1,9,0,1
Range A2= 15,10,1,8,1,9,0,1

I need 8 different formulas for A3-A10 to get the sum of each position.
So A3 would only get the sum of the first number in A1 and A2 (30)
A4 would get the sum of the second number in A1 and A2 (20)
A5=2
A6=16
A7=2
and so on...

Is that do-able?

Thanks,
Kevin





  #11   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5,651
Default sum of text positions

On Thu, 11 Dec 2008 12:00:05 -0800, Kevin
wrote:

Well, that works and I like it, however when I stated my example I simplified
it thinking I could change the formula prett easy. In actuallity the cells
that need to be added are
C4, E4, G4, I4, K4, M4, O4, Q4, S4, U4, W4, Y4, AA4, AC4, AE4, AG4


One way:

=SUM(RegexMid($C$4,"[^,]+",ROWS($1:1)),RegexMid($E$4,"[^,]+",ROWS($1:1))),...)

But doing it all within a UDF would probably be simpler.
--ron
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
1st and 2nd.... positions gorro Excel Discussion (Misc queries) 21 January 21st 08 06:35 AM
Lon Lat positions in chart Peter[_3_] Charts and Charting in Excel 1 August 10th 07 08:14 PM
using particular positions within an array BorisS Excel Worksheet Functions 2 November 12th 05 02:19 PM
Excel VBA Positions Available Career Capital Excel Discussion (Misc queries) 0 September 9th 05 05:30 AM
Positions of Comments AizA Excel Worksheet Functions 0 June 28th 05 12:38 AM


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