View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Rick Rothstein Rick Rothstein is offline
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