PDA

View Full Version : Word Macro/VBA help



kpangman
03-05-2020, 03:36 PM
Calling all VBA wizards - Please help if you can!!!:friends::bow:

I am using the below macro in word to pull data into a template from excel using mail merge. Essentially, I have a database that outlines data by ticker (which are identified by anywhere from 1 - 5 letter symbols (example Google = GOOGL and Sprint = S)). I want my macro, when run, to allow me to type in a certain ticker into an input box and the macro (along with the mail merge) will search the database for that ticker and then will populate the mail merge document with applicable data. Everything works really well for those tickers with 3 - 5 letters. The problem I am running into is when I type in tickers that have 1 or 2 letters. What happens is instead of searching the document for the exact match of the ticker that I input, it finds the first instance of that string of letters. For example, if the ticker was AM the macro would pull in data for AKAM which comes before AM alphabetically and is therefore the first instance of AM in the document. My question is: Is there a way to write a macro just like I have below (since it mostly works) that would only populate data relevant to the EXACT ticker I type in rather than just searching the document for the first instance of the letters I type in. My current macro is below:

Sub Macro()
Dim numRecord As Integer
Dim Ticker As String
Ticker = InputBox("Enter the Ticker:")
Set dsMain = ActiveDocument.MailMerge.DataSource
If dsMain.FindRecord(FindText:=Ticker, Field:="Ticker") = True Then
numRecord = dsMain.ActiveRecord
End If
End Sub

I have posted to this forum before with this same question and over 400 people viewed the thread but no one responded. If this isnt possible to do, it would be great to know that too. I would really like any insight even if it doesnt give me a perfect solution.

Thanks so much for the help ahead of time!!!!!:thumb:rofl::hi:

heedaf
03-05-2020, 04:22 PM
So would " AM " versus "AM" work? Basically you are adding the space to separate from AKAM.

gmaxey
03-06-2020, 04:30 PM
Some variation of this:


Sub Macro()
Dim numRecord As Integer
Dim Ticker As String
Dim dsMain
Ticker = InputBox("Enter the Ticker:")
'Set dsMain = ActiveDocument.MailMerge.DataSource
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "AM"
.MatchCase = True
.MatchWholeWord = True
If .Execute Then
oRng.Select
MsgBox "Do whatever"
End If
End With
'If dsMain.FindRecord(FindText:=Ticker, Field:="Ticker") = True Then
' numRecord = dsMain.ActiveRecord
'End If
End Sub

macropod
03-06-2020, 07:54 PM
I already answerd this question in: http://www.vbaexpress.com/forum/showthread.php?66882-How-to-find-exact-match-in-a-word-macro
Did you bother to read it?



I have posted to this forum before with this same question and over 400 people viewed the thread but no one responded. If this isnt possible to do, it would be great to know that too. I would really like any insight even if it doesnt give me a perfect solution.

As per http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item4, kindly don't start multiple threads on the same topic.

kpangman
03-09-2020, 09:53 AM
Sadly that didnt work - the macro wouldnt even run

[QUOTE=macropod;399855]I already answerd this question in:
Did you bother to read it?




I'm sorry, I did not see the original comment you made on the other thread. I assumed something was wrong with my first thread since I didnt see any responses. I did try yours just now and the macro wouldnt run.

macropod
03-09-2020, 02:43 PM
Thread closed. You may resume the discussion in your original thread.