+ Reply to Thread
Results 1 to 26 of 26

Extract unique numbers from Word and search for them in Excel

Hybrid View

Reen Extract unique numbers from... 07-04-2014, 02:36 AM
watersev Re: Extract unique numbers... 07-04-2014, 04:21 AM
Reen Re: Extract unique numbers... 07-04-2014, 09:06 AM
watersev Re: Extract unique numbers... 07-04-2014, 09:58 AM
watersev Re: Extract unique numbers... 07-04-2014, 10:23 AM
Reen Re: Extract unique numbers... 07-07-2014, 02:58 AM
Reen Re: Extract unique numbers... 07-07-2014, 04:26 AM
jindon Re: Extract unique numbers... 07-07-2014, 05:17 AM
Reen Re: Extract unique numbers... 07-07-2014, 06:23 AM
jindon Re: Extract unique numbers... 07-07-2014, 06:48 AM
Reen Re: Extract unique numbers... 07-07-2014, 06:55 AM
jindon Re: Extract unique numbers... 07-07-2014, 07:10 AM
Reen Re: Extract unique numbers... 07-07-2014, 07:30 AM
jindon Re: Extract unique numbers... 07-07-2014, 07:33 AM
Reen Re: Extract unique numbers... 07-07-2014, 07:35 AM
jindon Re: Extract unique numbers... 07-07-2014, 07:37 AM
Reen Re: Extract unique numbers... 07-07-2014, 07:44 AM
jindon Re: Extract unique numbers... 07-07-2014, 07:47 AM
Reen Re: Extract unique numbers... 07-07-2014, 08:15 AM
jindon Re: Extract unique numbers... 07-07-2014, 08:19 AM
Reen Re: Extract unique numbers... 07-07-2014, 08:29 AM
jindon Re: Extract unique numbers... 07-07-2014, 08:38 AM
Reen Re: Extract unique numbers... 07-07-2014, 09:05 AM
jindon Re: Extract unique numbers... 07-07-2014, 10:36 AM
Reen Re: Extract unique numbers... 07-08-2014, 03:48 AM
jindon Re: Extract unique numbers... 07-08-2014, 04:28 AM
  1. #1
    Registered User
    Join Date
    07-04-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    13

    Question Extract unique numbers from Word and search for them in Excel

    Hello,

    I have two documents. One worddocument with different product id:s, and a excelfile with hopefully the same numbers

    I want to build a macro that does following:

    1. Search for all numbers after the term "PRODID:" in the whole word document.
    2. See if these words can be found in the excelfile.
    3. Display the numbers who are in the worddocument but not in the exceldocument.


    I now have a short macro that can search the excel document but it requires me to manually copy all the ID-numbers from word to a excel sheet.

    I appreciate all help i can get!!

  2. #2
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Extract unique numbers from Word and search for them in Excel

    hi Reen, welcome to ExcelForum, can you provide Excel and Word file in question?

  3. #3
    Registered User
    Join Date
    07-04-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    13

    Re: Extract unique numbers from Word and search for them in Excel

    Hello,

    Thank you!

    Here is testfiles, the entire document is over 2000 pages and is confidential so i cant share it.

    Something worth knowing is that all documents are different in design. But the ID is always on the form

    "REQID XXXXXX/" or "REQID:XXXXXX/".
    Where XXXXXX is the ID-number with 4-7 digits.

    And i dont want to extract number who isnt in that form.

    In the exceldocument the Id is just in the form "XXXXXX"

    I want to search for the words in all of the sheets in the exceldokument, and every cell.
    Tell me if im confusing you!


    Thanks!
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Extract unique numbers from Word and search for them in Excel

    please check attachment, press Run button or run code "test"
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Extract unique numbers from Word and search for them in Excel

    oops...I've noticed a mistake in the code, rectified, please check attachment
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    07-04-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    13

    Re: Extract unique numbers from Word and search for them in Excel

    Quote Originally Posted by watersev View Post
    oops...I've noticed a mistake in the code, rectified, please check attachment

    THANK YOU SO MUCH! This is perfect, i really appreciate it!

    Is there a way to select the excelfile in the same way as the wordfile?

    Because the IDnumber in the exelfile can be wrapped around text etc. so i really cant extract them every time.
    Last edited by Reen; 07-07-2014 at 03:02 AM.

  7. #7
    Registered User
    Join Date
    07-04-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    13

    Re: Extract unique numbers from Word and search for them in Excel

    Quote Originally Posted by watersev View Post
    oops...I've noticed a mistake in the code, rectified, please check attachment
    Ive discovered one error cause of my misstake. I said that the word before the number war REQID but in fact it is REQPROD. When i changed REQID to REQPROD in the code i get "subscript out of range", do you know why?

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

    Re: Extract unique numbers from Word and search for them in Excel

    This should do
    Sub test()
        Dim fn As String, txt As String, e, m As Object
        Dim dic As Object, a(), n As Long
        fn = Application.GetOpenFilename("Word,*.doc*")
        If fn = "False" Then Exit Sub
        Set dic = CreateObject("Scripting.Dictionary")
        For Each e In Range("a3", Range("a" & Rows.Count).End(xlUp)).Value
            If e <> "" Then dic(e) = Empty
        Next
        With CreateObject("Word.application")
            txt = .documents.Open(fn).Content
            .Quit
        End With
        With CreateObject("VBScript.RegExp")
            .Global = True
            .Pattern = "REQPROD[ :]*(\d{4,7})"
            For Each m In .Execute(txt)
                If Not dic.exists(m.submatches(0)) Then
                    n = n + 1
                    ReDim Preserve a(1 To n)
                    a(n) = m.submatches(0)
                    dic(m.submatches(0)) = Empty
                End If
            Next
        End With
        If n < 1 Then
            MsgBox "No new ID"
        Else
            MsgBox n & " new id found"
            Range("a" & Rows.Count).End(xlUp)(2).Resize(n).Value = _
            Application.Transpose(a)
        End If
    End Sub

  9. #9
    Registered User
    Join Date
    07-04-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    13

    Re: Extract unique numbers from Word and search for them in Excel

    Quote Originally Posted by jindon View Post
    This should do
    Sub test()
        Dim fn As String, txt As String, e, m As Object
        Dim dic As Object, a(), n As Long
        fn = Application.GetOpenFilename("Word,*.doc*")
        If fn = "False" Then Exit Sub
        Set dic = CreateObject("Scripting.Dictionary")
        For Each e In Range("a3", Range("a" & Rows.Count).End(xlUp)).Value
            If e <> "" Then dic(e) = Empty
        Next
        With CreateObject("Word.application")
            txt = .documents.Open(fn).Content
            .Quit
        End With
        With CreateObject("VBScript.RegExp")
            .Global = True
            .Pattern = "REQPROD[ :]*(\d{4,7})"
            For Each m In .Execute(txt)
                If Not dic.exists(m.submatches(0)) Then
                    n = n + 1
                    ReDim Preserve a(1 To n)
                    a(n) = m.submatches(0)
                    dic(m.submatches(0)) = Empty
                End If
            Next
        End With
        If n < 1 Then
            MsgBox "No new ID"
        Else
            MsgBox n & " new id found"
            Range("a" & Rows.Count).End(xlUp)(2).Resize(n).Value = _
            Application.Transpose(a)
        End If
    End Sub
    Thank you very much!!! Works perfect
    Is there a way to display the REQPROD that are NOT in the excel, rather than the ones who are?

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

    Re: Extract unique numbers from Word and search for them in Excel

    I don't know how you want it.

    Just used msgbox.
    Sub test()
        Dim fn As String, txt As String, e, m As Object
        Dim dic As Object, a(), n As Long, msg As String
        fn = Application.GetOpenFilename("Word,*.doc*")
        If fn = "False" Then Exit Sub
        Set dic = CreateObject("Scripting.Dictionary")
        For Each e In Range("a3", Range("a" & Rows.Count).End(xlUp)).Value
            If e <> "" Then dic(CStr(e)) = Empty
        Next
        With CreateObject("Word.application")
            txt = .documents.Open(fn).Content
            .Quit
        End With
        With CreateObject("VBScript.RegExp")
            .Global = True
            .Pattern = "REQPROD[ :]*(\d{4,7})"
            For Each m In .Execute(txt)
                If Not dic.exists(m.submatches(0)) Then
                    n = n + 1: msg = msg & vbLf & m.submatches(0)
                    ReDim Preserve a(1 To n)
                    a(n) = m.submatches(0)
                    dic(m.submatches(0)) = Empty
                End If
            Next
        End With
        If msg = "" Then
            MsgBox "No new ID"
        Else
            MsgBox n & " new id found" & msg
            Range("a" & Rows.Count).End(xlUp)(2).Resize(n).Value = _
            Application.Transpose(a)
        End If
    End Sub

  11. #11
    Registered User
    Join Date
    07-04-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    13

    Re: Extract unique numbers from Word and search for them in Excel

    Quote Originally Posted by jindon View Post
    I don't know how you want it.

    Just used msgbox.
    Sub test()
        Dim fn As String, txt As String, e, m As Object
        ... 
            Application.Transpose(a)
        End If
    End Sub
    My bad, i missunderstood what the program did at first!

    When i extract all REQPROD with your code, i want to compare them with the exceldocument with the following code:

    Private Sub CommandButton1_Click()
    
        Dim CompareRange As Variant, x As Variant, y As Variant
        Dim fName As String
        Dim shName As String
        
        Set CompareRange = Workbooks("FileName").Worksheets("SheetName").Range("A:A")
    
        For Each x In Selection
            For Each y In CompareRange
                If x = y Then x.Offset(0, 1) = x
            Next y
        Next x
        
        MsgBox "Done"
    End Sub
    And it works sometimes but most it doesnt!

    I would like to change:
    Set CompareRange = Workbooks("FileName").Worksheets("SheetName").Range("A:A")

    so that the user can insert the excelfile and search the whole document including all sheets and all cells.
    How do i do that?

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

    Re: Extract unique numbers from Word and search for them in Excel

    So, you want to compare the result with the data in another workbook.
    Something like. (not tested)
    Private Sub CommandButton1_Click()
        Dim ws As WorkSheet, a, e, dic as object, r As Range
        Set dic = CreateObject("Sripting.Dictionary")
        With WorkBooks("FileName")
            For Each ws In .Worksheets
                a = ws.UsedRange.Value
                If IsArray(a) Then
                    For Each e In a
                        If e <> "" Then dic(e) = Empty
                    Next
                End if
            Next
        End With
        For Each r In Selection
            If dic.Exists(r.Value) Then r(,2).Value = r.Value
        Next
    End Sub

  13. #13
    Registered User
    Join Date
    07-04-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    13

    Re: Extract unique numbers from Word and search for them in Excel

    Excactly! Your code does not create any error but it doenst work. Nothing happends when i run it. Do you know what the problem may be?

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

    Re: Extract unique numbers from Word and search for them in Excel

    Did you select the result range before you run the code?

  15. #15
    Registered User
    Join Date
    07-04-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    13

    Re: Extract unique numbers from Word and search for them in Excel

    Yes! I added a MsgBox in the end and i can tell it runs very quick.
    In the exceldocument i want to search in the REQPROD-number isnt lined up as in te result range, the numbers can be wrapped in text etc.

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

    Re: Extract unique numbers from Word and search for them in Excel

    I need to see the file...

  17. #17
    Registered User
    Join Date
    07-04-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    13

    Re: Extract unique numbers from Word and search for them in Excel

    Does it help if i say that the first row is a drpo-down list? and row 2 and -> is content in the list.

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

    Re: Extract unique numbers from Word and search for them in Excel

    No, I need to see that data to be compared to the result....

  19. #19
    Registered User
    Join Date
    07-04-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    13

    Re: Extract unique numbers from Word and search for them in Excel

    Can i give u the content of one cell? I cant share the document and its to big to change the content of the whole file..

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

    Re: Extract unique numbers from Word and search for them in Excel

    All your result consists of numeric value...

    What are the data to be compared?

  21. #21
    Registered User
    Join Date
    07-04-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    13

    Re: Extract unique numbers from Word and search for them in Excel

    Quote Originally Posted by jindon View Post
    All your result consists of numeric value...

    What are the data to be compared?

    For example, this is the content in ONE cell. And one cell can contain many REQPROD.

    ID: 73819
    Object Text: 
    
    ......
    * 
    
    Table: Requirements ..
    
    
    Notes in the table ....
    
    1
    ********* 
    Refer to section 
    
    
    
    c. Whenever the has responded:
    
    ********** 
    session transition regardless if it is to the same or another session.
    
    
    3
    **********
    14
    
    ID: 156789

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

    Re: Extract unique numbers from Word and search for them in Excel

    How about
    Private Sub CommandButton1_Click()
        Dim ws As WorkSheet, a, e, dic as object, r As Range,m As Object
        Set dic = CreateObject("Sripting.Dictionary")
        With WorkBooks("FileName")
            For Each ws In .Worksheets
                a = ws.UsedRange.Value
                With CreateObject("VBScript.RegExp")
                    .Global = True
                    .Pattern = "ID: *(\d+)"
                    If IsArray(a) Then
                        For Each e In a
                            For Each m In .Execute(e)
                                dic(val(m.submatches(0))) = Empty
                            Next
                        Next
                    Else
                        For Each m In .Execute(a)
                            dic(val(m.submatches(0))) = Empty
                        Next
                    End if
                End With
            Next
        End With
        For Each r In Selection
            If dic.Exists(r.Value) Then r(,2).Value = r.Value
        Next
    End Sub

  23. #23
    Registered User
    Join Date
    07-04-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    13

    Re: Extract unique numbers from Word and search for them in Excel

    Quote Originally Posted by jindon View Post
    How about
    Private Sub CommandButton1_Click()
        Dim ws As WorkSheet, a, e, dic as object, r As Range,m As Object
        Set dic = CreateObject("Sripting.Dictionary")
        With WorkBooks("FileName")
            For Each ws In .Worksheets
                a = ws.UsedRange.Value
                With CreateObject("VBScript.RegExp")
                    .Global = True
                    .Pattern = "ID: *(\d+)"
                    If IsArray(a) Then
                        For Each e In a
                            For Each m In .Execute(e)
                                dic(val(m.submatches(0))) = Empty
                            Next
                        Next
                    Else
                        For Each m In .Execute(a)
                            dic(val(m.submatches(0))) = Empty
                        Next
                    End if
                End With
            Next
        End With
        For Each r In Selection
            If dic.Exists(r.Value) Then r(,2).Value = r.Value
        Next
    End Sub
    Works just fine i think!! Thank you so much, you have been a great help for me!

    If its not too much trouble, is it possible to select the excelfile on the same way as the wordfile. Instead of manually enter the filename in the code. Because i will use the code on different word and excel document.
    Last edited by Reen; 07-07-2014 at 09:19 AM.

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

    Re: Extract unique numbers from Word and search for them in Excel

    OK
    Private Sub CommandButton1_Click()
        Dim ws As WorkSheet, a, e, dic as object, r As Range,m As Object
        Dim fn As String
        fn = Application.GetOpenFilename("Excel,*.xls*")
        If fn = "False" Then Exit Sub
        Set dic = CreateObject("Sripting.Dictionary")
        With WorkBooks(fn)
            For Each ws In .Worksheets
                a = ws.UsedRange.Value
                With CreateObject("VBScript.RegExp")
                    .Global = True
                    .Pattern = "ID: *(\d+)"
                    If IsArray(a) Then
                        For Each e In a
                            For Each m In .Execute(e)
                                dic(val(m.submatches(0))) = Empty
                            Next
                        Next
                    Else
                        For Each m In .Execute(a)
                            dic(val(m.submatches(0))) = Empty
                        Next
                    End if
                End With
            Next
        End With
        For Each r In Selection
            If dic.Exists(r.Value) Then r(,2).Value = r.Value
        Next
    End Sub

  25. #25
    Registered User
    Join Date
    07-04-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    13

    Re: Extract unique numbers from Word and search for them in Excel

    Thank you!

    I get an subscript out of range error, debugger says this is the problem

    With Workbooks(fn)

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

    Re: Extract unique numbers from Word and search for them in Excel

    Of course...
        With WorkBooks.Open(fn)
            For Each ws In .Worksheets
                a = ws.UsedRange.Value
                With CreateObject("VBScript.RegExp")
                    .Global = True
                    .Pattern = "ID: *(\d+)"
                    If IsArray(a) Then
                        For Each e In a
                            For Each m In .Execute(e)
                                dic(val(m.submatches(0))) = Empty
                            Next
                        Next
                    Else
                        For Each m In .Execute(a)
                            dic(val(m.submatches(0))) = Empty
                        Next
                    End if
                End With
            Next
            .Close False
        End With

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] search for all unique numbers down a column and print a list of those numbers in another
    By JJGF in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-05-2014, 11:08 PM
  2. Search for a word in a column and extract the word
    By Abishekar in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 12-17-2012, 09:30 AM
  3. [SOLVED] vba to search word and extract following 3 words
    By sausis in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-30-2012, 03:39 AM
  4. Search for and extract word from a cell
    By hmatharo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-30-2007, 10:21 PM
  5. [SOLVED] how to extract unique numbers once from a list of repeated numbers?
    By stuart.kwok@gmail.com in forum Excel General
    Replies: 2
    Last Post: 05-02-2006, 11:20 AM

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