开发者

Output array from VBA function to Excel sheet using formula

开发者 https://www.devze.com 2023-03-17 14:31 出处:网络
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 cita

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
D开发者_C百科im 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消