2017-09-06 62 views
1

在Windows上,我使用以下函數從VLOOKUP公式中提取lookup_value。但是,我最近切換到macOS,現在這個功能是不兼容的。從MacOS上的VLOOKUP公式提取lookup_value

Mac上的Excel中的VBA不支持正則表達式。有沒有其他功能可以使用,或者我運氣不好?

Function extractFirstInt(strValue As String) As Integer 
    Dim regex As New regexp 
    regex.Pattern = "(\d+)" 
    extractFirstInt = regex.Execute(strValue)(0).SubMatches(0) 
End Function 
+0

[看一看這個(https://stackoverflow.com/questions/27344932/regex-with-vba-excel-at-mac) – Tom

+0

[RegEx與Mac上的VBA Excel]可能重複(https://stackoverflow.com/questions/27344932/regex-with-vba-excel-at-mac) – Tom

回答

2

我沒有在Mac這裏測試,但我認爲這應該工作:

Function extractFirstInt(strValue As String) As Integer 
    Dim codes() As Byte 
    Dim n As Long 
    Dim pos As Long 
    Dim length As Long 
    If strValue Like "*#*" Then 
     codes = strValue 
     length = 1 
     For n = LBound(codes) To UBound(codes) Step 2 
      Select Case codes(n) 
       Case 48 To 57 
        If pos = 0 Then pos = n \ 2 + 1 Else length = length + 1 
       Case Else 
        If pos <> 0 Then Exit For 
      End Select 
     Next 
     extractFirstInt = CInt(Mid$(strValue, pos, length)) 
    End If 
End Function