![]() |
|
|
|
#1
|
|||
|
|||
|
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
|
|
#2
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
|||
|
|||
|
Replace:
strResult = GetValue("The value to find", 6) With strResult = GetValue(YourUserForm.TextBox1.Text, 6) or however your userform and .TextBox1 might be defined. |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
||||
|
||||
|
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
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#7
|
|||
|
|||
|
Unfortunately the same Invalid Use of Null error is still occurring Graham
|
|
#8
|
||||
|
||||
|
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. |
|
#9
|
|||
|
|||
|
You're right Graham. One of the sheets has a header in row 2 which I think is causing the error.
|
|
|
|
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 |
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 |