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 6809635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:06:34+00:00 2026-05-26T20:06:34+00:00

I am trying to write a function for excel 2010 that will take a

  • 0

I am trying to write a function for excel 2010 that will take a cell of unstructured text, look for something called an sdi value and, if found, return that number. The sdi value will appear as sdi ####. What I want is to return sdi and the sepecific numbers that follow it, so if the cell contains “some text sdi 1234 some more text” the function will return sdi 1234.

This is my function:

Function SdiTest(LookIn As String) As String
  Dim temp As String
  Dim STA As Object
  temp = ""

  Set SDI = CreateObject("VBScript.RegExp")
  SDI.IgnoreCase = True
  SDI.Pattern = "sdi [1-9]*"
  SDI.Global = True

  If SDI.Test(LookIn) Then
    temp = SDI.Execute(LookIn)
  End If

  SdiTest = temp
End Function

If there is no sdi number it never enters the if statement and dutifully returns the empty string. If there is an sdi number I get #VALUE!

What am I missing?

Yes, VBScript is enabled. Additionally, I am finding it frustrating to use regex in VBA, and hard to find useful info online. Links to good online resources would be appreciated.

Thank you

  • 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-05-26T20:06:34+00:00Added an answer on May 26, 2026 at 8:06 pm

    You need to access the matches in order to get at the SDI number. Here is a function that will do it (assuming there is only 1 SDI number per cell).

    For the regex, I used “sdi followed by a space and one or more numbers”. You had “sdi followed by a space and zero or more numbers”. You can simply change the + to * in my pattern to go back to what you had.

    Function ExtractSDI(ByVal text As String) As String
    
    Dim result As String
    Dim allMatches As Object
    Dim RE As Object
    Set RE = CreateObject("vbscript.regexp")
    
    RE.pattern = "(sdi \d+)"
    RE.Global = True
    RE.IgnoreCase = True
    Set allMatches = RE.Execute(text)
    
    If allMatches.count <> 0 Then
        result = allMatches.Item(0).submatches.Item(0)
    End If
    
    ExtractSDI = result
    
    End Function
    

    If a cell may have more than one SDI number you want to extract, here is my RegexExtract function. You can pass in a third paramter to seperate each match (like comma-seperate them), and you manually enter the pattern in the actual function call:

    Ex) =RegexExtract(A1, "(sdi \d+)", ", ")
    

    Here is:

    Function RegexExtract(ByVal text As String, _
                          ByVal extract_what As String, _
                          Optional seperator As String = "") As String
    
    Dim i As Long, j As Long
    Dim result As String
    Dim allMatches As Object
    Dim RE As Object
    Set RE = CreateObject("vbscript.regexp")
    
    RE.pattern = extract_what
    RE.Global = True
    Set allMatches = RE.Execute(text)
    
    For i = 0 To allMatches.count - 1
        For j = 0 To allMatches.Item(i).submatches.count - 1
            result = result & seperator & allMatches.Item(i).submatches.Item(j)
        Next
    Next
    
    If Len(result) <> 0 Then
        result = Right(result, Len(result) - Len(seperator))
    End If
    
    RegexExtract = result
    
    End Function
    

    *Please note that I have taken “RE.IgnoreCase = True” out of my RegexExtract, but you could add it back in, or even add it as an optional 4th parameter if you like.

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

Sidebar

Related Questions

I am trying to write a short function that will take a cell of
I'm trying to write a custom function that will let me retrieve a cell
I am trying to write a function that will pull the name of a
i am trying to write a function that will make DataRow[column] return nullable typed
I'm trying to write a function that will let me red-shift or blue-shift a
I am trying to write a function that will output every other attachment caption
I am trying to write a function that will trim all the letters off
I'm trying to write an Excel Function that grabs the data out of a
I'm trying to create an export Excel/CSV function that will iterate through a custom
I'm trying to write function similar to http://whatismyudid.com/ that, then approved, will return the

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.