Edit VBA to scan a document and pick out the reference numeral that corresponds to a string, e.g. output "102" for "dog 102" in the document

3 min read 04-10-2024
Edit VBA to scan a document and pick out the reference numeral that corresponds to a string, e.g. output "102" for "dog 102" in the document


Extracting Reference Numerals from Text: A VBA Guide

Have you ever found yourself needing to extract specific reference numbers from a document, like pulling out "102" from the phrase "dog 102"? This is a common task that can be automated using VBA (Visual Basic for Applications) within Microsoft Word. This article will walk you through the process, providing a clear, step-by-step guide and valuable insights along the way.

Scenario: Imagine you have a document filled with text like "dog 102", "cat 201", and "bird 305", and you want to extract the reference numbers (102, 201, 305) into a separate list. VBA can automate this task, saving you time and effort.

Original VBA Code:

Sub ExtractReferenceNumbers()

    Dim doc As Document
    Dim rng As Range
    Dim strText As String
    Dim strNumber As String

    Set doc = ActiveDocument
    Set rng = doc.Content

    Do While rng.Find.Execute(FindText:="dog", Forward:=True, Wrap:=wdFindStop) = True
        strText = rng.Text
        strNumber = ExtractNumber(strText)
        If strNumber <> "" Then
            Debug.Print strNumber
        End If
    Loop

End Sub

Function ExtractNumber(strText As String) As String

    Dim i As Integer
    Dim strNumber As String

    For i = Len(strText) To 1 Step -1
        If IsNumeric(Mid(strText, i, 1)) Then
            strNumber = Mid(strText, i) & strNumber
        Else
            Exit For
        End If
    Next i

    ExtractNumber = strNumber

End Function

Analysis and Clarification:

This VBA code, although functional, can be improved in several ways for efficiency and flexibility:

  1. Hardcoded "dog": The code is currently hardcoded to find the reference numbers associated with the word "dog". To make it more versatile, we can replace "dog" with a variable to search for any target string.
  2. Single number extraction: This code only extracts the first number it finds. It would be useful to extract all numbers from the document, even if multiple numbers are present in a single line.
  3. Improved number extraction: The ExtractNumber function relies on a loop that starts from the end of the text. A regular expression approach could be more efficient for extracting multiple numbers.

Enhanced VBA Code:

Sub ExtractAllReferenceNumbers()

    Dim doc As Document
    Dim rng As Range
    Dim strTarget As String
    Dim strText As String
    Dim strNumbers() As String
    Dim i As Long

    Set doc = ActiveDocument
    Set rng = doc.Content
    strTarget = InputBox("Enter the target string:", "Target String")

    Do While rng.Find.Execute(FindText:=strTarget, Forward:=True, Wrap:=wdFindStop) = True
        strText = rng.Text
        strNumbers = Split(ExtractAllNumbers(strText), ",") ' Extract all numbers
        For i = 0 To UBound(strNumbers)
            Debug.Print Trim(strNumbers(i)) ' Print each extracted number
        Next i
    Loop

End Sub

Function ExtractAllNumbers(strText As String) As String

    Dim regex As Object
    Set regex = CreateObject("VBScript.RegExp")
    regex.Global = True
    regex.Pattern = "\d+"
    ExtractAllNumbers = regex.Execute(strText).Item(0).Value ' Return the first match
    
    ' Handle multiple matches:
    For i = 1 To regex.Execute(strText).Count - 1
        ExtractAllNumbers = ExtractAllNumbers & "," & regex.Execute(strText).Item(i).Value
    Next i
    
End Function

Key Improvements:

  • User input: The code now prompts the user for the target string, allowing it to find reference numbers associated with any word.
  • Multiple number extraction: The ExtractAllNumbers function uses regular expressions to extract all numbers from the text, even if there are multiple numbers in a single line.
  • Clearer output: The code now prints each extracted number on a separate line in the Immediate window (accessed by pressing Ctrl+G).

Additional Value:

This enhanced VBA code provides a versatile solution for extracting reference numbers from documents. You can modify the strTarget variable to search for any desired string, and the code will extract all associated reference numbers.

References & Resources:

By using these steps and the provided code, you can easily automate the process of extracting reference numbers from your documents. The enhanced code offers greater flexibility, making it a powerful tool for various document management tasks.