Input data into excel to open word document with corresponding data in specified bookmarks

excelnoobz

New Member
Joined
Oct 27, 2017
Messages
2
Hi all, I could definitely use your help in addressing my code. This is the first time I've ever coded in VBA so appreciate any beginner's advice and tips. As a brief background, I have an excel sheet of my county's zip codes (col 2) and their corresponding towns (col 3). I want to have a person enter their zip code into "A2". Excel searches thru the zip code column and finds the corresponding town. Afterwards, I would like it to open up a word document template, which has two bookmarks: "zip" and "town", and pull the corresponding zip code and town to the appropriate bookmark.

Unfortunately, I keep getting an error of "Sub or Function not defined". Additionally, it highlights "Sub Worksheet_Change (ByVal Target As Range) and "cell" once I receive the error. Not sure what that means.
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">
Code:
Sub Worksheet_Change(ByVal Target As Range)
Dim myVar As String
Dim numofzips As Integer
Dim targetrow As Integer
Dim ZipCode As String, municipal As String
numofzips = Range(Range("B2"), Range("B2").End(xlDown)).Count
If Target.Address = "$A$2" Then
Target.Font.ColorIndex = 5
myVar = Target.Value
For i = 2 To numofzips
If myVar = cell(i, 2).Value Then
targetrow = i
End If
Next
End If
ZipCode = cell(targetrow, 2)
municipal = cell(targetrow, 3)
ImportToDoc ZipCode, municipal



End Sub

Sub ImportToDoc(zip As String, town As String)



Dim wdApp As Word.Application, wdDoc As Word.Document
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
Set wdDoc = wdApp.Documents.Open("C:\Users\kimd\Documents\VBA Test.docx", ReadOnly:=True)
wdApp.Visible = True
wdDoc.Bookmarks("zip").Range.Text = zip
wdDoc.Bookmarks("town").Range.Text = town
wdDoc.SaveAs Filename:=town & " Test"
wdDoc.Close
wdApp.Application.Quit


End Sub
Code:
</code>
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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