Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:18:52+00:00 2026-06-17T22:18:52+00:00

I am working on a vba macro which uses regular expression to search for

  • 0

I am working on a vba macro which uses regular expression to search for a string pattern in another string.

Regex pattern includes a string (APR24 in code below) which varies. I need to know how to include a variable in the pattern.Could any one please help.

My code is as below

Public Function Regexsrch(ByVal str2bsrchd As String, ByVal str2srch As String) As Boolean
Dim Regex As New VBScript_RegExp_55.RegExp
Dim matches, s
Regex.Pattern = "(\.|\s)APR24(,|\s|\()"
Regex.IgnoreCase = True
    If Regex.Test(str2bsrchd) Then
         Regexsrch = True
Else
        Regexsrch = False
End If
End Function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:18:54+00:00Added an answer on June 17, 2026 at 10:18 pm

    You can specify whatever pattern you want in str2srch and then assign that to Regex.Pattern

    For example

    Sub Sample()
        Debug.Print Regexsrch("APR24ddd", "APR24")  '<~~ Returns True
        Debug.Print Regexsrch("APR277ddd", "APR24") '<~~ Returns False
    End Sub
    
    Public Function Regexsrch(ByVal str2bsrchd As String, ByVal str2srch As String) As Boolean
        Dim Regex As New VBScript_RegExp_55.RegExp
        Dim matches, s
    
        Regex.Pattern = str2srch
        Regex.IgnoreCase = True
    
        If Regex.Test(str2bsrchd) Then
             Regexsrch = True
        Else
            Regexsrch = False
        End If
    End Function
    

    FOLLOWUP

    Even if it is dynamic you can always pass the pattern as

    Debug.Print Regexsrch("APR24ddd", "(\.|\s)" & VARIABLE & "(,|\s|\()").

    This gives you the flexibility of using whatever pattern you want to pass to the function and you are not limited to one pattern…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I put together some VBA code for Outlook 2007 which has been working predominantly
! I am working with the replace method of VBA Regex and am wondering
I'm not sure why my code is not working.. I have code in vba
I have sheet1.xls which is closed. I am working in sheet2.xls VBA as follows.
Our workbooks are server-generated SpreadsheetML , which cannot include any VBA code. Creating native
What is the best way to avoid duplicating code when working in VBA? I'm
I am working with some VBA code in Access that when a very specific
I am working on a spreadsheet with lots of VBA code in it. Most
The workbook I am working on in Excel uses graphics with 3d properties which
When I'm working with VBA I'll sometimes leave a line of code incomplete to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.