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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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