# Macro for Table in MS word



## newtomacros (Jul 20, 2009)

Hi

I have table in a word file that contains 2 columns. What i am trying to do is search through the second column for blank cells and if found copy the text from the adjoining cell that is in the first column to the blank cell.

I found some code for searching blank cells but i need help on copy the text from the adjoining cell to the blank cell.


```
Dim oCell   As Word.Cell
     
    With Selection
   
        If .Information(wdWithInTable) Then
    
            For Each oCell In .Tables(1).Range.Cells
    
                    ' copy the text from the adjoining cell to the blank cell

                End If
            Next
        End If
    End With
```
I appreciate any help

Thanks


----------



## macropod (Apr 11, 2008)

Hi ,

Try something along the lines of:

```
Sub Demo()
Dim oTbl As Table, i As Integer, strTxt As String
For Each oTbl In ActiveDocument.Tables
  With oTbl
    For i = 1 To .Rows.Count
    strTxt = .Cell(i, 2).Range.Text
    strTxt = Left(strTxt, Len(strTxt) - 2)
    If strTxt = "" Then
      strTxt = .Cell(i, 1).Range.Text
      strTxt = Left(strTxt, Len(strTxt) - 2)
      .Cell(i, 2).Range.Text = strTxt
    End If
    Next
  End With
Next
End Sub
```
*Note:* as coded, this macro processes all tables in the document. If you only want it to act on the selected tables, change 'ActiveDocument' to 'Selection'.


----------



## macropod (Apr 11, 2008)

Hi newtomacros,

Please see:
http://www.excelguru.ca/node/7

It seems you've posted the same question, as hugh999, at: http://www.tek-tips.com/viewthread.cfm?qid=1604425&page=1, where I now find I've just provided essentially the same solution Gerry's already provided. I am not amused to find I've been wasting my time here because you didn't have the courtesy to tell anyone you'd cross-posted in other forums.


----------



## fumei (Jul 22, 2009)

Concur


----------



## newtomacros (Jul 20, 2009)

Sorry for not informing that i posted to another form but the only reason i did was that i did not receive any reply to this post in 24 hours so i asummed, it would not be answered.

Thanks


----------



## macropod (Apr 11, 2008)

Hi newtomacros,

Neither I nor, in all likelihood, most other contributors would mind if you posted in multiple forums simultaneously = provided you post a link to the others so that everyone can see what's going on. Clearly, in this case, your assumption about whether a reply would be forthcoming didn't hold true. I might have replied sooner to your original post except for the fact I'm travelling and have only sporadic internet access.

A further courtesy would be to let each forum know when you've obtained a suitable solution - and the link to it.


----------

