Fill-down for table cells (Word2000)

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15591
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Fill-down for table cells (Word2000)

Post by ChrisGreaves »

I already know that there will be a more elegant solution, but still ...
I build VBA code in Word tables and am tired of selecting a cell, duplicating it, selecting those two to make 4, 8, 16 and so on and then having to delete superfluous rows at the end of the table.
hence:

Code: Select all

Sub FillDownFromCurrentCell()
    ''' Comments:   Fill to the end of the column from the first selected cell
    ''' Arguments:  None
    ''' Returns:    None
    Dim strCellText As String
    strCellText = Selection.Cells(1).Range.Text
    strCellText = Left(strCellText, Len(strCellText) - 2) ' trim cell markers/vbcrlf
    While Not Selection.Cells(1).Row.IsLast
        Selection.Tables(1).Rows(Selection.Cells(1).Row.Index + 1).Cells(Selection.Cells(1).Column.Index).Range.Select
        Selection.Text = strCellText
    Wend
End Sub
There's nothing heavier than an empty water bottle

User avatar
HansV
Administrator
Posts: 78440
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Fill-down for table cells (Word2000)

Post by HansV »

This version is a bit shorter, and it not only copies the plain text, but also the formatting:

Code: Select all

Sub FillDownFromCurrentCell()
  Selection.Cells(1).Range.Copy
  Do While Not Selection.Cells(1).Row.IsLast
    Selection.MoveDown
    Selection.Paste
  Loop
End Sub
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15591
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Fill-down for table cells (Word2000)

Post by ChrisGreaves »

HansV wrote:This version is a bit shorter, and it not only copies the plain text, but also the formatting:
Hans, thanks for the improvement.
See? I knew what I was talking about when I said "I already know that there will be a more elegant solution,", I just didn't know what it was!
There are so many things in VBA still to be discovered. ".IsLast" took me by surprise.
There's nothing heavier than an empty water bottle

User avatar
HansV
Administrator
Posts: 78440
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Fill-down for table cells (Word2000)

Post by HansV »

One caveat: the macro will not work correctly and probably cause an error if the table contains merged cells.
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15591
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Fill-down for table cells (Word2000)

Post by ChrisGreaves »

HansV wrote:... merged cells.
(shudder!).
Thanks for the caveat. In my mind there is a clear distinction between a quick macro that serves an internal purpose, such as this macro, and a public application macro. This fill-down macro serves a specific purpose (for me), that of speeding up the process of generating VBA code; the original macro was written to do just that, and nothing more. It is a part of my code-generation toolkit.

Of course late last night I was working on a different sort of table on a different sort of project and realized that the fill-down macro would be a dandy adjunct here too, so I trotted it out with great success.

One more use out of it and I'll have to include the good old If Selection.Information(wdWithinTable) stuff, not to mention something about .Cells.Count .....
There's nothing heavier than an empty water bottle

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15591
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Fill-down for table cells (Word2000)

Post by ChrisGreaves »

HansV wrote:One caveat: the macro will not work correctly and probably cause an error if the table contains merged cells.
This version
(1) Stops when it reaches an already-filled cell
(2) Returns to the original table cell

Code: Select all

Sub FillDownFromCurrentCell()
    Dim rng As Range
    Set rng = Selection.Range
    Selection.Cells(1).Range.Copy
    Do While Not Selection.Cells(1).Row.IsLast
        Selection.MoveDown
        If Len(Selection.Cells(1).Range.Text) > 2 Then  ' (1) Stops when it reaches an already-filled cell
            Exit Do
        Else
            Selection.Paste
        End If
    Loop
    rng.Select ' (2) Returns to the original table cell
End Sub
There's nothing heavier than an empty water bottle

User avatar
HansV
Administrator
Posts: 78440
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Fill-down for table cells (Word2000)

Post by HansV »

The following macro is not intended as a replacement for yours, it serves a different purpose.
It's more or less the equivalent of Ctrl+Enter in Excel: if you select a range of cells in Excel, type something and press Ctrl+Enter, the value you typed will be entered in all cells of the selection.

Code: Select all

Sub FillCells()
  If Selection.Information(wdWithInTable) Then
    Selection.Cells(1).Range.Copy
    Selection.Paste
  End If
End Sub
x92.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15591
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Fill-down for table cells (Word2000)

Post by ChrisGreaves »

HansV wrote:The following macro is not intended as a replacement for yours, ...
Oh Hans, I wouldn't say that.
It's a much better version of anything that's come before.
It ought to be a great deal faster than my looping version.
I tried it out on a long column, and it seems faster - I haven't run concrete timings though.
5.JPG
This is the sort of problem I run into with automatically-generated code.
I was at 610 macros when this happened; splitting the code into 2x300 seemed to solve the problem.
You do not have the required permissions to view the files attached to this post.
There's nothing heavier than an empty water bottle