Run Macro for only selected row

Suavesav

New Member
Joined
Mar 14, 2023
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
I have a code that creates certificates in Word using data from excel. All certificates are created in one go. How do I change it so that it only creates a certificate for a selected row? Thanks in advance :)

Here is the code

'Start word and a new document
Set wd = New Word.Application
'Set worksheet where table is available
Set sh = ThisWorkbook.Sheets("Shipment")
iRow = 4
Do While sh.Range("A" & iRow).Value <> ""


'opening the word template
Set wdDoc = wd.Documents.Open(ThisWorkbook.Path & "\New Freight Certificate (Macro).docx")
wd.Visible = False
wd.Selection.Goto what:=wdgotobookmark, Name:="HBL"
wd.Selection.typetext Text:=sh.Range("A" & iRow).Value

wd.Selection.Goto what:=wdgotobookmark, Name:="MBL"
wd.Selection.typetext Text:=sh.Range("G" & iRow).Value

wd.Selection.Goto what:=wdgotobookmark, Name:="Shipper"
wd.Selection.typetext Text:=sh.Range("K" & iRow).Value

wd.Selection.Goto what:=wdgotobookmark, Name:="POL"
wd.Selection.typetext Text:=sh.Range("L" & iRow).Value

wd.Selection.Goto what:=wdgotobookmark, Name:="POD"
wd.Selection.typetext Text:=sh.Range("M" & iRow).Value

wd.Selection.Goto what:=wdgotobookmark, Name:="Type"
wd.Selection.typetext Text:=sh.Range("AB" & iRow).Value

wd.Selection.Goto what:=wdgotobookmark, Name:="ExRT"
wd.Selection.typetext Text:=sh.Range("AC" & iRow).Value

wd.Selection.Goto what:=wdgotobookmark, Name:="Volume"
wd.Selection.typetext Text:=sh.Range("O" & iRow).Value

wd.Selection.Goto what:=wdgotobookmark, Name:="Terms"
wd.Selection.typetext Text:=sh.Range("AD" & iRow).Value


wdDoc.SaveAs2 (ThisWorkbook.Path & "\" & sh.Range("A" & iRow).Value)

wdDoc.Close

Set wdDoc = Nothing


iRow = iRow + 1
Loop



End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
What happens if you replace the following lines?

VBA Code:
iRow = selection.row
' Do While sh.Range("A" & iRow).Value <> ""

' iRow = iRow + 1
' Loop


Untested and based on a quick glance...
 
Upvote 0
This works great Thanks! However, now if I start with the topmost line item, all following rows are also looped in for certificate creation.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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