Results 1 to 7 of 7
  1. #1
    5 Star Lounger
    Join Date
    Jan 2002
    Location
    Midlothian, Virginia, USA
    Posts
    874
    Thanks
    0
    Thanked 2 Times in 1 Post

    Step thru Table Cells (Office 2000)

    I am trying to create a VBA macro that will step down through the cells in a particular column in a table, highlight the entire contents of each cell, and Paste into that cell. The contents to be pasted have been manually Copied before this macro is run. I'm familiar with using VBA in Access but I've never really understood the objects that are used in Word and I'm not having any success with this macro. <img src=/S/sad.gif border=0 alt=sad width=15 height=15> Can someone create that code loop for me?

    The starting condition is that the cursor is in the top cell in that column and all the contents of that cell have been manually selected and Copied to the clipboard. Of course, the purpose of the macro is to replace the current contents of all the other cells in that column with the contents of the top cell.

    Thanks,
    Bill

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Step thru Table Cells (Office 2000)

    This should get you started...
    <pre>Public Sub FillTable()
    Dim tblTemp As Table
    Dim rowTemp As Row
    Dim iCol As Integer
    If Not Selection.Information(wdWithInTable) Then
    MsgBox "Selection is not in a table", vbOKOnly
    Exit Sub
    End If
    iCol = Selection.Information(wdStartOfRangeColumnNumber)
    For Each rowTemp In Selection.Tables(1).Rows
    If rowTemp.Cells.Count >= iCol Then
    rowTemp.Cells(iCol).Range.Delete
    rowTemp.Cells(iCol).Range.Paste
    End If
    Next rowTemp
    End Sub
    </pre>

    StuartR


    Edited because I hadn't noticed that you only wanted the cells in one column. I have assumed that you want, for example, "The third cell in each row" and that if a row has fewer cells you don't want anything done.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Step thru Table Cells (Office 2000)

    Stuart's method is faster, but if you find that you want to see it running, this method, which emulates how you would do it interactively, is more visual:
    <pre>Sub TableColumnFill()
    Dim intLastRow As Integer, intColumns As Integer
    'I assume you are in the column you want to edit
    With Selection
    intLastRow = .Tables(1).Rows.Count
    intColumns = .Tables(1).Columns.Count
    Do
    .Paste
    If .Information(wdEndOfRangeRowNumber) >= intLastRow Then
    Exit Do
    Else
    .MoveRight unit:=wdCell, Count:=intColumns, Extend:=wdMove
    End If
    Loop
    End With
    End Sub</pre>

    (It's more visual if you have a slow processor or you step through it with the de######, I should say. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>)

    Note: if your rows do not all have the same number of columns, this will cause unpredictable but incorrect results.

  4. #4
    5 Star Lounger
    Join Date
    Jan 2002
    Location
    Midlothian, Virginia, USA
    Posts
    874
    Thanks
    0
    Thanked 2 Times in 1 Post

    Re: Step thru Table Cells (Office 2000)

    Stuart,

    Thanks for the great information. I discovered the problem with the first version and came back a while ago to ask about it and you had updated your message to fix it! The final version works great. There are sometimes two columns and sometimes three columns in the table so the flexible version that you supplied was needed.

    Thanks,
    Bill

  5. #5
    5 Star Lounger
    Join Date
    Jan 2002
    Location
    Midlothian, Virginia, USA
    Posts
    874
    Thanks
    0
    Thanked 2 Times in 1 Post

    Re: Step thru Table Cells (Office 2000)

    I'm going ahead and using the faster version but I really like reading your code. It tells me a lot about the properties that can be used in Word VBA. I'll save it in my archives for future reference.

    Thanks,
    Bill

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Step thru Table Cells (Office 2000)

    My code will not run correctly if your table has a merged cell that contains multiple rows and columns. I would need to think long and hard about how to do this correctly.

    So if you
    <table border=1><td>Normal</td><td>Normal</td><td>Normal</td><td>Normal</td><td>Normal</td><td><font color=red>Merge </font color=red></td><td><font color=red>these</font color=red></td><td>Normal</td><td>Normal</td><td><font color=red>four</font color=red></td><td><font color=red>cells</font color=red></td><td>Normal</td><td>Normal</td><td>Normal</td><td>Normal</td><td>Normal</td></table>
    And then run the Macro it will die horribly.

    StuartR

  7. #7
    5 Star Lounger
    Join Date
    Jan 2002
    Location
    Midlothian, Virginia, USA
    Posts
    874
    Thanks
    0
    Thanked 2 Times in 1 Post

    Re: Step thru Table Cells (Office 2000)

    Stuart,

    Yes, I understand that and that is not an issue in my situation. In this particular situation we are working on a "Handout" document that has been created by PowerPoint. PP creates a table with 3 columns: Slide No., Slide image, Notes area. Sometimes we want to keep the Slide No. column and sometimes we want to delete it, so sometimes the images are in the second column and sometimes they are in the first column.

    Thanks,
    Bill

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •