0

I found a nifty RegEx function that I'm using (see below). The function outputs an array. This is fine if I only ever need the first element of the array. But I'm trying to extract authors from citation data, so I need to pull multiple items from this output.

I know about arrays in Excel sheets. So I tried ={ReFind(A3,"[^()]+")}, selected an area and pressed ctrl+shift+enter, but it returns an error and {=ReFind(A3,"[^()]+")} just duplicates the formula across the cells.

Is there a way to have the function output the array to multiple cells using a formula? Can I get away without having to write some more vba?

Function ReFind(FindIn, FindWhat As String, _
    Optional IgnoreCase As Boolean = False)
Dim i As Long
Dim matchCount As Integer
Dim RE As Object, allMatches As Object, aMatch As Object
Set RE = CreateObject("vbscript.regexp")
RE.Pattern = FindWhat
RE.IgnoreCase = IgnoreCase
RE.Global = True
Set allMatches = RE.Execute(FindIn)
matchCount = allMatches.Count
If matchCount >= 1 Then
    ReDim rslt(0 To allMatches.Count - 1)
    For i = 0 To allMatches.Count - 1
        rslt(i) = allMatches(i).Value
    Next i
    ReFind = rslt
Else
    ReFind = ""
End If
End Function
1
  • My hacked solution would be to add a new optional variable to the function to pull the i'th item from the array. But I'd like it if I could use the formula array functionality built in to Excel. Commented Jul 6, 2011 at 17:57

1 Answer 1

2

You need to return a 2-dimensional array that matches the range of calling cells (so that you are entering it as a multicell array formula (do not enter the { } just select the cells, enter the formula and press Control-shift-enter)) You are currently returning a 1-dimensional array

Sign up to request clarification or add additional context in comments.

1 Comment

Actually, without any changes to the function, and just removing the braces {} from the formula, it worked perfectly. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.