Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-07-2017, 06:32 PM
jhancock1994 jhancock1994 is offline Excel to Word data import based on UserForm entry Windows 7 64bit Excel to Word data import based on UserForm entry Office 2013
Novice
Excel to Word data import based on UserForm entry
 
Join Date: May 2017
Posts: 7
jhancock1994 is on a distinguished road
Default Excel to Word data import based on UserForm entry

Hi all,

I am quite new to VBA, and am struggling somewhat with this. Essentially I require a macro that can pull data from Excel and paste into a bookmark in Word. I need it to search Excel for a word which has been inputted by the user into a fillable form, and if there is a match copy the value of another cell (e.g. 5 columns across) and paste into the bookmark. I have come up with the below code, which needs some fixing. Any help would be very greatly appreciated.

Cheers,
James



Code:
Sub CallEx3()

    Dim objExcel As New Excel.Application
    Dim exWb As Excel.Workbook
    Dim oRng As Excel.Range
    Dim oSheet As Excel.Worksheet
    Dim LastRow As Long
    
    Set exWb = objExcel.Workbooks.Open("U:\VBA Word Automation\Excel Test Sheet.xlsx")
    
    For Each oSheet In objExcel.ActiveWorkbook.Worksheets
            LastRow = Cells(Rows.Count, 1).End(xlUp).Row
             For i = 2 To LastRow
                 ThisValue = Cells(i, 1).Value
                           If ThisValue = TextBox1.Text Then
                          Cells(i, 6).Copy
                               WordApp.Selection.PasteSpecial.FillBM Link:=False,    DataType:=wdPasteText, _
                             Placement:="bmDT", DisplayAsIcon:=False                  
                 End If
            Next i

    Next oSheet


    exWb.Close

    Set exWb = Nothing
    Set objExcel = Nothing
    Set oRng = Nothing
    
    Exit Sub
   
End Sub
Reply With Quote
  #2  
Old 05-07-2017, 08:59 PM
gmayor's Avatar
gmayor gmayor is offline Excel to Word data import based on UserForm entry Windows 10 Excel to Word data import based on UserForm entry Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Frankly I wouldn't do it that way as it is slow and cumbersome. The following functions will read the named worksheet into an array then search the first column of the array for the input value (here "The value to find") and outputs the corresponding value from column 6 to the bookmark (here "bookmarkname"). If the value is not found the bookmark is cleared.

It is assumed that the named worksheet has at least six columns, that there is a header row on the first row and there are no empty columns. The sheet name is case sensitive.

If you need to loop through several worksheets, then all the sheets searched must match the criteria above. The array function takes no prisoners.

Macro1 simply shows how to call the functions. You can call the functions from your form using the appropriate values.

Code:
Option Explicit
Const strWorkbook As String = "U:\VBA Word Automation\Excel Test Sheet.xlsx"
Const strSheet As String = "Sheet1"        'The name of the worksheet

Sub Macro1()
'look for the value in the first column and return the value in the sixth column
Dim strResult As String
    strResult = GetValue("The value to find", 6)
    FillBM "bookmarkname", strResult 'fill the named bookmark with the value
lbl_Exit:
    Exit Sub
End Sub

Private Function GetValue(strKey As String, lngColumn As Long) As String
'Graham Mayor - http://www.gmayor.com - Last updated - 08 May 2017
Dim Arr() As Variant
Dim iCols As Long
Dim strValue As String
Dim strID As String
    Arr = xlFillArray(strWorkbook, strSheet)
    For iCols = 0 To UBound(Arr, 2)
        strID = Arr(0, iCols)    'The first column
        strValue = Arr(lngColumn - 1, iCols)
        If strID = strKey Then
            GetValue = strValue
            Exit For
        End If
    Next iCols
lbl_Exit:
    Exit Function
End Function

Private Function xlFillArray(strWorkbook As String, _
                             strRange As String) As Variant
'Graham Mayor - http://www.gmayor.com - Last updated - 08 May 2017

Dim RS As Object
Dim CN As Object
Dim iRows As Long

    strRange = strRange & "$]"
    Set CN = CreateObject("ADODB.Connection")
    
    CN.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
                              "Data Source=" & strWorkbook & ";" & _
                              "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"

    Set RS = CreateObject("ADODB.Recordset")
    RS.Open "SELECT * FROM [" & strRange, CN, 2, 1

    With RS
        .MoveLast
        iRows = .RecordCount
        .MoveFirst
    End With
    xlFillArray = RS.GetRows(iRows)
    If RS.State = 1 Then RS.Close
    Set RS = Nothing
    If CN.State = 1 Then CN.Close
    Set CN = Nothing
lbl_Exit:
    Exit Function
End Function

Private Sub FillBM(strBMName As String, strValue As String)
'Graham Mayor - http://www.gmayor.com
Dim oRng As Range
    With ActiveDocument
        On Error GoTo lbl_Exit
        Set oRng = .Bookmarks(strBMName).Range
        oRng.Text = strValue
        oRng.Bookmarks.Add strBMName
    End With
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 05-08-2017, 04:57 PM
jhancock1994 jhancock1994 is offline Excel to Word data import based on UserForm entry Windows 7 64bit Excel to Word data import based on UserForm entry Office 2013
Novice
Excel to Word data import based on UserForm entry
 
Join Date: May 2017
Posts: 7
jhancock1994 is on a distinguished road
Default

Thanks very much for the reply Graham. It works great, the only issue is that "The value to find" must be text that the user will enter into a User Form when the Word document is opened.
Reply With Quote
  #4  
Old 05-09-2017, 03:06 AM
gmaxey gmaxey is offline Excel to Word data import based on UserForm entry Windows 7 32bit Excel to Word data import based on UserForm entry Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Replace:
strResult = GetValue("The value to find", 6)

With
strResult = GetValue(YourUserForm.TextBox1.Text, 6)

or however your userform and .TextBox1 might be defined.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 05-09-2017, 04:57 PM
jhancock1994 jhancock1994 is offline Excel to Word data import based on UserForm entry Windows 7 64bit Excel to Word data import based on UserForm entry Office 2013
Novice
Excel to Word data import based on UserForm entry
 
Join Date: May 2017
Posts: 7
jhancock1994 is on a distinguished road
Default

Cheers Greg.

Unfortunately when run most of the time now it gives an Invalid use of Null (Error 94), relating to the line:

strID = Arr(0, iCols)

The search value and Excel workbook meet the requirements so I'm not exactly sure where this is coming from.
Reply With Quote
  #6  
Old 05-09-2017, 08:32 PM
gmayor's Avatar
gmayor gmayor is offline Excel to Word data import based on UserForm entry Windows 10 Excel to Word data import based on UserForm entry Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It sounds like there is no value in strResult when changed to use the userform value. Greg has shown how to use a userform value. He has not included the userform, which you will need to add to your document or modify the code to use the value selected from the userform. Userforms are difficult to reproduce in the forum.

Replace the Macro1 I provided earlier with
Code:
Sub Macro1()
'look for the value in the first column and return the value in the sixth column
Dim strResult As String
Dim strValue As String
    strValue = InputBox("Enter the value to find")
    If strValue = "" Then
        MsgBox "No value entered or user cancelled"
        GoTo lbl_Exit
    End If
    strResult = GetValue(strValue, 6)
    FillBM "bookmarkname", strResult    'fill the named bookmark with the value
lbl_Exit:
    Exit Sub
End Sub
which does not need a userform.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 05-14-2017, 03:42 PM
jhancock1994 jhancock1994 is offline Excel to Word data import based on UserForm entry Windows 7 64bit Excel to Word data import based on UserForm entry Office 2013
Novice
Excel to Word data import based on UserForm entry
 
Join Date: May 2017
Posts: 7
jhancock1994 is on a distinguished road
Default

Unfortunately the same Invalid Use of Null error is still occurring Graham
Reply With Quote
  #8  
Old 05-14-2017, 08:29 PM
gmayor's Avatar
gmayor gmayor is offline Excel to Word data import based on UserForm entry Windows 10 Excel to Word data import based on UserForm entry Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You will get that error if there is a missing value in column A of your worksheet.
Post your template and worksheet to me at supportATgmayor.com with your forum username in the subject and I will investigate.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com

Last edited by gmayor; 05-14-2017 at 11:18 PM.
Reply With Quote
  #9  
Old 05-16-2017, 04:41 PM
jhancock1994 jhancock1994 is offline Excel to Word data import based on UserForm entry Windows 7 64bit Excel to Word data import based on UserForm entry Office 2013
Novice
Excel to Word data import based on UserForm entry
 
Join Date: May 2017
Posts: 7
jhancock1994 is on a distinguished road
Default

You're right Graham. One of the sheets has a header in row 2 which I think is causing the error.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Data Import from multiple word files into an Excel tale asmanokhchi Word 1 04-21-2015 06:24 AM
need VBA to Transpose the Data from excel to word based on given criteria(status) winmaxservices2 Excel Programming 1 12-19-2014 10:21 PM
Excel to Word data import based on UserForm entry Import data from Excel into Word sb003848 Word 1 11-04-2014 06:30 PM
Import excel data in to SQL Server DavidBrown Excel 0 08-08-2011 04:49 AM
Import Pics and Excel Data into PP? jawillyams PowerPoint 0 03-13-2011 01:03 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:31 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft