Excel data into Word based on criteria

pregen

New Member
Joined
Oct 20, 2008
Messages
2
I'm looking for a way to insert rows into Word from an Excel spreadsheet. The catch is the rows will be based on specific criteria that is located on the Word document. I can figure out a way to bookmark the Word data in a field. I can't figure out the VBA to go do a lookup into the Excel ole linked file and pull in only the rows I need (or range - can set up ranges if need be). I know enough VBA to be extremely dangerous - but since my model is to search Google and "borrow" code ideas, I can't find one that does this particular request.

I can probably have a table in Word - populate the first column with the product. What I then need is to have the ole pull in only the rows associated with that product. I don't want to have to create a Word document per product. There are hundreds. Excel is the product pricelist - Word is the Quote. Can't use Excel as the vehicle for the Quote because of another application that is calling Word - but can't call Excel.

It's Word and Excel 2003. Thanks in Advance.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Make sure that your files are backed up before experimenting ...
Code:
'=============================================================================
'- WORD MACRO : LOOK UP WORD DATA IN AN EXCEL TABLE
'=============================================================================
'- This macro opens and closes Excel (Hidden) for each batch.
'- The search here is limited to Excel column A. Gets 2 items.
'- Problems to overcome are that Word ranges count end of line characters
'- (character 13) as words, and ranges expand if additions are made.
'-----------------------------------------------------------------------------
'- *** NB. Bear in mind that stopping the macro before it finishes
'- *** will leave a hidden Excel instance in computer memory.
'-----------------------------------------------------------------------------
'- Method : select a set of typed lookup values in a document (separate lines)
'- then run the macro to add the extra data.
'- Might be easier to use an InputBox method.
'-----------------------------------------------------------------------------
'- Brian Baulsom October 2008
'=============================================================================
Option Base 1
Dim MyLookupValue As Variant    ' lookup value from Word
'- Excel
Dim ExcelApp As Object
Dim ExcelWorkbook As String
Dim ExcelWorksheet As String
Dim FindRange As String          ' Excel range to search
Dim FoundCell As Object
Dim FoundRow As Long
'- return values from Excel
Dim ReturnValue1 As Variant
Dim ReturnValue2 As Variant
'- WORD
Dim MyRange As Range
Dim LookupList()        ' Array of words to look up
Dim MyWords As Integer  ' number of words in the array (including end of line)
'=============================================================================
'- MAIN ROUTINE TO GET LOOKUP VALUES FROM WORD
'=============================================================================
Sub GET_LOOKUPS()
    '-------------------------------------------------------------------------
    '- define variables
    ExcelWorkbook = "F:\TEST\EXCELDATA2.XLS"
    ExcelWorksheet = "Sheet1"
    '- FindRange   =  is set to column A below (change as necessary)    ***
    '------------------------------------------------------------------------
    '- open Excel
    Set ExcelApp = CreateObject("Excel.Application")
    With ExcelApp
        .Visible = False
        .workbooks.Open ExcelWorkbook
        .Worksheets(ExcelWorksheet).Activate
        With .Worksheets(ExcelWorksheet)
            FindRange = "A2:A" & CStr(.Range("A65536").End(xlUp).Row)   '***
            '================================================================
            '- GET WORD LOOKUP VALUES
            '- Could use InputBox etc. instead here
            '================================================================
            Set MyRange = Selection.Range
            MyWords = MyRange.Words.Count - 1
            ReDim LookupList(MyWords)
            For w = 1 To MyWords
                LookupList(w) = MyRange.Words(w)
            Next
            Set MyRange = Nothing
            '------------------------------------------------------------------
            '- MOVE WORD SELECTION POINT TO END OF FIRST LINE
            Selection.Collapse direction:=wdCollapseStart
            Selection.EndKey Unit:=wdLine
            '------------------------------------------------------------------
            '- LOOK UP WORDS IN THE ARRAY & ADD TO THE END OF LINES IN WORD
            For w = 1 To MyWords
                MyLookupValue = LookupList(w)
                If Asc(MyLookupValue) > 31 Then     ' not EOL character 13
                    EXCEL_LOOKUP MyLookupValue      ' call subroutine
                    '------------------------------------------------------
                    '- ADD NEW DATA (WITH TABS SEPARATING THEM)
                    Selection.TypeText _
                        Text:=vbTab & ReturnValue1 & vbTab & vbTab & ReturnValue2
                    '------------------------------------------------------
                    '- MOVE SELECTION POINT DOWN 1 LINE
                    Selection.MoveDown Unit:=wdLine, Count:=1
                    '------------------------------------------------------
                End If
            Next
            '================================================================
        End With
        '--------------------------------------------------------------------
        '- CLOSE EXCEL APP
        .ActiveWorkbook.Close savechanges:=False
        .Quit
    End With
    '-------------------------------------------------------------------------
     Beep
    Set ExcelApp = Nothing
End Sub
'======== end of main routine ===============================================
'============================================================================
'- SUBROUTINE : LOOK UP IN EXCEL & GET REQUIRED DATA
'============================================================================
Private Sub EXCEL_LOOKUP(LookupValue)
    With ExcelApp.Worksheets(ExcelWorksheet)
            '----------------------------------------------------------------
            '- FIND DATA
            Set FoundCell = _
                .Range(FindRange).Find(What:=LookupValue, _
                    After:=.Range(FindRange).Cells(1, 1), _
                    LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, MatchCase:=False)
            '----------------------------------------------------------------
            If FoundCell Is Nothing Then
                ReturnValue1 = "Not found"
                ReturnValue2 = "------------"
            Else
                FoundRow = FoundCell.Row
                ReturnValue1 = .Cells(FoundRow, 2).Value
                ReturnValue2 = .Cells(FoundRow, 3).Value
            End If
        '--------------------------------------------------------------------
    End With
End Sub
'============================================================================
 
Upvote 0
Thanks and sorry for the tardy reply. I've been traveling overseas without much access to email. I'll give this a try this week. I appreciate you sharing the code. Cool indeed.:)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top