View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Ron Rosenfeld Ron Rosenfeld is offline
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