DEV Community

Cover image for Excel Eats: Bob's Burgers Macros with a Side of Regex
Bala Madhusoodhanan
Bala Madhusoodhanan

Posted on

Excel Eats: Bob's Burgers Macros with a Side of Regex

Intro:
In the ever-evolving landscape of business operations, Excel remains a steadfast companion, serving a myriad of functions from IT endeavors to casual company gatherings. The essence of business lies in the seamless exchange of data and operations, aiming to unveil trends and meticulously organize information into meaningful categories. However, a challenge arises when these operational insights are disseminated in multiple copies across diverse functions, risking data consistency and cleanliness. To tackle this challenge and ensure data integrity, we embark on a journey through the realm of Bob's Burgers Macros – a delightful fusion of Excel prowess enriched with the potency of Regex. Brace yourself for an exploration into the realms of automation and precision, where every byte carries the flavor of efficiency and accuracy.

Algorithm:
Lets look into the special on the tech menu – a VB function that's seasoned with the secret sauce of Regular Expressions (Regex), a concoction we affectionately call "Bob's Burgers Macros." Just like Bob meticulously crafts his burgers, this VB function is a virtuoso in its own right, adding a dash of precision to your Excel kitchen. Picture it as Bob's special ingredient, ensuring your data experience is nothing short of a well-orchestrated masterpiece.

Image description

Code:

Public Function RegExpMatch(input_range As Range, pattern As String, Optional match_case As Boolean = True) As Variant

  Dim arRes() As Variant 'array to store the results
  Dim iInputCurRow, iInputCurCol, cntInputRows, cntInputCols As Long 'index of the current row in the source range, index of the current column in the source range, count of rows, count of columns

    On Error GoTo ErrHandl
    RegExpMatch = arRes

    Set regex = CreateObject("VBScript.RegExp")

    regex.pattern = pattern
    regex.Global = True
    regex.MultiLine = True

    If True = match_case Then
        regex.ignorecase = False
    Else
        regex.ignorecase = True
    End If


    cntInputRows = input_range.Rows.Count
    cntInputCols = input_range.Columns.Count
    ReDim arRes(1 To cntInputRows, 1 To cntInputCols)

    For iInputCurRow = 1 To cntInputRows
        For iInputCurCol = 1 To cntInputCols
            arRes(iInputCurRow, iInputCurCol) = regex.Test(input_range.Cells(iInputCurRow, iInputCurCol).Value)
        Next
    Next

  RegExpMatch = arRes
  Exit Function

ErrHandl:
    RegExpMatch = CVErr(xlErrValue)
End Function
Enter fullscreen mode Exit fullscreen mode

Image description

Macrodemo

Promise:
In the grand banquet of data, ensuring the quality and taste of every byte is a meticulous task. Think of our custom Regex match module as the sommelier of your Excel spreadsheets, ensuring the finest data flavors are presented on your palate. From the Tech menu to your operational feast, this module elevates data validation to an art form. As you indulge in the rich symphony of precise Regex checks, rest assured that the data shared across different realms of your organization's spreadsheets is of the highest standards – a true delicacy in the world of tech gastronomy. Bon appétit!

Top comments (0)