Using If Formula within VB Code in Excel

Gordo24

New Member
Joined
Apr 19, 2011
Messages
43
Not sure if this is possible, but I'm sure somebody a lot better than I am will be able to confirm. I have created a macro in my Excel source document which opens a Word template and populates my bookmarks within Word with Excel data. One of my data cells is a drop down list. My drop down list options are:

Annual Payments
Quarterly Payments
Monthly Payments

When my bookmark is filled, I need it to fill as follows:

year if Annual Payments is selected
quarter if Quarterly Payments is selected
month if Monthly Payments is selected

Basically I'm wondering if there is a way to use an IF formula in my excel macro so if my excel cell has "Annual Payments" it populates my Word bookmark as "year." Changing my Excel drop down list is not an option. Here is my code stripped down so you get an idea of what I'm doing:

Sub createTemplate()
On Error GoTo errorHandler
Dim wdApp As Word.Application
Dim myDoc As Word.Document
Dim mywdRange As Word.Range
Dim Payout_Frequency As Excel.Range

Set wdApp = New Word.Application
With wdApp
.Visible = True
.WindowState = wdWindowStateMaximize
End With
Set myDoc = wdApp.Documents.Add(Template:="C:\Test.doc")

Set Payout_Frequency = Sheets("Sheet1").Range("B2")


With myDoc.Bookmarks
.Item("Payout_Frequency").Range.InsertAfter Payout_Frequency.Text

End With

errorHandler:
Set wdApp = Nothing
Set myDoc = Nothing
Set mywdRange = Nothing
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try using Select Case to set the different bookmarks. Here's a possible solution:
Code:
Sub createTemplate()
    On Error GoTo errorHandler
    Dim wdApp As Word.Application
    Dim myDoc As Word.Document
    Dim mywdRange As Word.Range
    Dim Payout_Frequency As Excel.Range
    
    Set wdApp = New Word.Application
    With wdApp
        .Visible = True
        .WindowState = wdWindowStateMaximize
    End With
    
    Set myDoc = wdApp.Documents.Add(Template:="C:\Test.doc")
    
    Set Payout_Frequency = Sheets("Sheet1").Range("B2")
    
    Select Case Payout_Frequency.Value
    Case "Annual Payments"
        myDoc.Bookmarks.Item("Payout_Frequency").Range.InsertAfter "year"
    Case "Quarterly Payments"
        myDoc.Bookmarks.Item("Payout_Frequency").Range.InsertAfter "quarter"
    Case "Monthly Payments"
        myDoc.Bookmarks.Item("Payout_Frequency").Range.InsertAfter "month"
    Case Else
        MsgBox "Invalid selection in Payout Frequency"
    End Select
    
errorHandler:
    Set wdApp = Nothing
    Set myDoc = Nothing
    Set mywdRange = Nothing
End Sub
 
Upvote 0
Thanks PJ in FL...worked perfectly. I removed the "case else" as there will be instances when the drop down will not be utilized and I don't want the error message poping up.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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