Results 1 to 3 of 3

Need help for a code in macro (to identify alpha numeric numbers)

Threaded View

  1. #1
    Forum Contributor
    Join Date
    04-01-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    128

    Need help for a code in macro (to identify alpha numeric numbers)

    Hi,

    I am preparing a macro which identifies and copies 9 digit alpha numeric code (alpha till 4). The code am using is "\b\w{0,9}\d{4,9}\b"

    But this code is copying alpha numeric numbers more than 9 too.....

    I need to prepare a macro which would take only 9 digit numbers (including alphabets till 5)

    My full macro code is
    Sub ExtractNumbers()
    
    Dim Filename As String
    Dim Filepath As String
    Dim Filespec As String
    Dim Match As Variant
    Dim RegExp As Object
    Dim R As Long
    Dim Rng As Range
    Dim TextFile As Integer
    Dim TextLine As String
    Dim Wks As Worksheet
    
    ' Output worksheet and starting cell.
    Set Wks = Worksheets("Sheet2")
    Set Rng = Wks.Range("A1")
    
    ' Location and name of text file.
    Filename = "Test.txt"
    Filepath = "C:\Documents and Settings\rra172\Desktop"
    
    Set RegExp = CreateObject("VBScript.RegExp")
    RegExp.Global = True
    RegExp.Pattern = "\b\w{0,9}\d{4,9}\b"
    
    ' Add backslash to the end of the file path if needed.
    If Right(Filepath, 1) <> "\" Then
    Filespec = Filepath & "\" & Filename
    Else
    Filespec = Filepath & Filename
    End If
    
    TextFile = FreeFile
    
    ' Parse the 9 digit numbers from the text file and output them to the worksheet.
    Open Filespec For Input Access Read As #TextFile
    Do While Not EOF(TextFile)
    Line Input #TextFile, TextLine
    If RegExp.test(TextLine) = True Then
    For Each Match In RegExp.Execute(TextLine)
    Rng.Offset(R, 0).Value = Match
    R = R + 1
    Next Match
    End If
    Loop
    Close #TextFile
    
    End Sub
    I am attaching the txt file from which this macro should copy the numbers. thanks
    Attached Files Attached Files
    Last edited by Paul; 04-19-2012 at 12:10 AM. Reason: Added CODE tags.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1