+ Reply to Thread
Results 1 to 3 of 3

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

Hybrid 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.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

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

    1) Not recommend "Match" for variable name as it is a reserved word for VBA
    2) If 9 digits numbers only, Pattern should be "\b\d{9}\b"
    I would write like this
    
    Sub ExtractNumbers()
    
        Dim Filename As String
        Dim myMatches As Object
        Dim RegExp As Object
        Dim i As Long
        Dim Rng As Range
        Dim txt As String
        
        ' Output starting cell.
        
        Set Rng = Sheets("Sheet2").Range("A1")
        
        ' Location and name of text file.
        Filename = CreateObject("WScript.Shell").SpecialFolders("desktop") & "\Test.txt"
        
        Set RegExp = CreateObject("VBScript.RegExp")
        RegExp.Global = True
        RegExp.Pattern = "\b\d{9}\b"
        
        ' Read entire text at one time
        txt = CreateObject("Scripting.FileSystemObject").OpenTextFile(Filename).ReadAll
        
        ' Parse the 9 digit numbers from the text file and output them to the worksheet.
        Set myMatches = RegExp.Execute(txt)
        For i = 0 To myMatches.Count - 1
            Rng.Offset(i).Value = myMatches(i)
        Next
    
    End Sub
    When you post the code, use the code tag
    e.g

    [code]
    'your code here
    [/code]
    Last edited by jindon; 04-18-2012 at 10:26 PM. Reason: typo

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

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

    Hello Raju Radhakrishnan,

    I added code tags to your post. This keeps your original formatting, making it easier to read, and easier to copy. Please do this in future posts. If you don't know how, see my signature line.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ Reply to Thread

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