How can I write an If, Then, Else statement in Microsoft Word

P

Perplexed

I have a Microsoft Word table that has seven cells in the row. Some of the
cells have text in them already and some do not. I want to move the text
from the first cell to one of the empty cells. I need to write an If
statement that will test if the cell I'm moving the text into is empty before
it pastes the text in it. If the cell is empty, then paste the text into
that cell, if it's not empty, then move to the next cell to the right and
test if that cell is empty before pasting the text. It should test for an
empty cell five times before ending the If statement. Can anyone help me to
write an If statement to accomplish this?
 
G

Greg Maxey

Perplexed,

You can to this with a macro. I would use Select Case vice IF>Then>Else.

Sub MoveText()
Dim oTbl As Table
Dim txtString As String

Set oTbl = ActiveDocument.Tables(1)
txtString = oTbl.Cell(1, 1).Range.Text
Select Case 2
Case Len(oTbl.Cell(1, 2).Range.Text)
oTbl.Cell(1, 2).Range.Text = txtString
Case Len(oTbl.Cell(1, 3).Range.Text)
oTbl.Cell(1, 3).Range.Text = txtString
Case Len(oTbl.Cell(1, 4).Range.Text)
oTbl.Cell(1, 4).Range.Text = txtString
Case Len(oTbl.Cell(1, 5).Range.Text)
oTbl.Cell(1, 5).Range.Text = txtString
Case Len(oTbl.Cell(1, 6).Range.Text)
oTbl.Cell(1, 6).Range.Text = txtString
Case Len(oTbl.Cell(1, 7).Range.Text)
oTbl.Cell(1, 7).Range.Text = txtString
Case Else
MsgBox "All cells are in use."
End Select

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top