I am trying to create a vba that skips cells with zero in it while creating certificates in word. The cells have zero because I have to create cells that feed on a student roster. I have a standard 133 cells on the roster because schools across the country have different sizes that don't exceed 133. As a result, the certificate roster feeds from the main roster, making some of the cells zero. How do I ckip the zero while leaving the range for the whole 133 slots.
Here is the code:
Here is the code:
Code:
Sub WordMailMerge()
'Step 1: Declare your variables
Dim wd As Word.Application
Dim wdDoc As Word.Document
Dim MyRange As Excel.Range
Dim MyCell As Excel.Range
Dim txtRank As String
Dim txtLastName As String
Dim txtFirstName As String
'Step 2: Start Word and add a new document
Set wd = New Word.Application
Set wdDoc = wd.Documents.Add
wd.Visible = True
wdDoc.PageSetup.Orientation = wdOrientLandscape
'Step 3: Set the range of your contact list
Set MyRange = Sheets("Certificates").Range("A2:A121")
'Step 4: Start the loop through each cell
For Each MyCell In MyRange.Cells
'Step 5: Assign values to each component of the letter
txtRank = MyCell.Value
txtFirstName = MyCell.Offset(, 2).Value
txtLastName = MyCell.Offset(, 1).Value
'Step 6:Insert the structure of template document
wd.Selection.InsertFile _
ThisWorkbook.Path & "" & "certificate of graduation JLS.docx"
'Step 7: Fill each relevant bookmark with respective value
wd.Selection.Goto What:=wdGoToBookmark, Name:="Rank"
wd.Selection.TypeText Text:=txtRank
wd.Selection.Goto What:=wdGoToBookmark, Name:="FirstName"
wd.Selection.TypeText Text:=txtFirstName
wd.Selection.Goto What:=wdGoToBookmark, Name:="LastName"
wd.Selection.TypeText Text:=txtLastName
'Step 8: Clear any remaining bookmarks
On Error Resume Next
wdDoc.Bookmarks("Rank").Delete
wdDoc.Bookmarks("FirstName").Delete
wdDoc.Bookmarks("LastName").Delete
'Step 9: Go to the end, insert new page, and start with the next cell
wd.Selection.EndKey Unit:=wdStory
wd.Selection.InsertBreak Type:=wdPageBreak
Next MyCell
'Step 10: Set cursor to beginning and clean up memory
wd.Selection.HomeKey Unit:=wdStory
wd.Activate
Set wd = Nothing
Set wdDoc = Nothing
End Sub
Last edited by a moderator: