17

我开发了一个 Excel 2010 VBA 宏,它使用VBScript.RegExp. 我的一位用户显然正在使用 Excel Mac 2011,它支持 VBA,但不支持VBScript.RegExp.

我看过几篇文章提到可以使用AppleScript可由 VBA 宏调用的 RegEx 函数来创建自己的函数。但是,似乎必须有 Excel 文件的 Mac 版本和 Windows 版本。这不太理想。

是否有另一种方法可以在 VBA 中实现与 Windows 和 Mac 兼容的正则表达式?

4

3 回答 3

2

I hit this problem trying to use the regexp library when trying to strip html tags from a cell.

I know this wasn't noted within the question as the desired use but, in case it is or future visitors are trying to replace a regex function to remove HTML from tags within a cell within Mac Excel 2011, this user defined function will work. Apologies to the original author, i found it only but can no longer find the source i'm afraid.

Public Function StripHTML(zDataIn As String) As String

  Dim iStart As Integer
  Dim iEnd   As Integer
  Dim iLen   As Integer

  Do While InStr(zDataIn, "<") > 0
    iStart = InStr(zDataIn, "<")
    iEnd = InStr(iStart, zDataIn, ">")
    iLen = Len(zDataIn)
    If iStart = 1 Then
      zDataIn = Right(zDataIn, iLen - iEnd)
    Else
      If iLen = iEnd Then
        zDataIn = Left(zDataIn, iStart - 1)
      Else
        zDataIn = Mid(zDataIn, 1, iStart - 1) & _
                  Right(zDataIn, iLen - iEnd)
      End If
    End If
  Loop

  StripHTML = zDataIn

End Function
于 2013-10-15T13:30:03.280 回答
2

不幸的是,还没有找到解决方案——当前的解决方法是简单地用一系列Replace调用(或其他必需的操作)替换正则表达式。

于 2013-02-11T09:29:57.890 回答
0

如果您不介意支付 99 美元,可以使用 Aivosto RegExpr。显然这是一个纯 VBA 解决方案,应该在 PC 和 Mac VBA 上运行。

或者,为 PC 和 Mac 编写单独的解决方案,然后使用编译器指令来分隔在每个平台上运行的代码位

于 2013-07-24T23:16:13.947 回答