Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to develop an Excel Macro that contains regular expressions so it will work in Windows and Mac

I have developed an Excel 2010 VBA Macro that makes use of VBScript.RegExp. One of my users is apparently using Excel Mac 2011, which supports VBA, but no VBScript.RegExp.

I have seen several posts that mention it is possible to create your own RegEx functions using AppleScript that could be called by the VBA Macro. However, it seems as though there would have to be a Mac version of the Excel file and a Windows version. This is less than ideal.

Is there another way to implement regular expressions in VBA that would be compatible with both Windows and Mac?

like image 734
Brian Avatar asked Dec 21 '12 18:12

Brian


People also ask

Do regular expressions work in Excel?

Does Excel support regex? Regrettably, there are no inbuilt Regex functions in Excel. To be able to use regular expressions in your formulas, you'll have to create your own user-defined function (VBA or . NET based) or install third-party tools supporting regexes.

How do I write a macro script in Excel?

On the Developer tab, click Record Macro. Optionally, enter a name for the macro in the Macro name box, enter a shortcut key in the Shortcut key box, and a description in the Description box, and then click OK to start recording.

What language does Excel use for macros?

Excel Macros - Security. The macros that you create in Excel would be written in the programming language VBA (Visual Basic for Applications). You will learn about the Excel macro code in later chapters.


2 Answers

Unfortunately no solution for this has yet been found — the current workaround is to simply replace regular expressions with a series of Replace calls (or other required operation).

like image 77
JustinJDavies Avatar answered Sep 28 '22 06:09

JustinJDavies


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
like image 34
jimmyjamesdub Avatar answered Sep 28 '22 06:09

jimmyjamesdub