Results 1 to 6 of 6
  1. #1
    3 Star Lounger
    Join Date
    Sep 2004
    Location
    Portsmouth, Hampshire, England
    Posts
    200
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Working with a range of table cells (2002 SP3)

    In Excel I can refer to a range of cells and apply changes to them without having to select them first.

    I want to Hide all the text in a particular column apart from the header row.

    In Word I know I can use the SetRange method to select that range of cells and then apply the change and I can also cycle through the cell objects and thus apply the change without selecting them.

    But can I combine the 2 and just refer to a range of cells - something like ActiveDocument.tables(1).range(cells(3,2), cells(3,activedocument.tables(1).columns(3).rows.c ount)).

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Working with a range of table cells (2002 SP3)

    You can use SetRange to set the start and end of a range, for example:

    Dim rng As Range
    Set rng = ActiveDocument.Range
    With ActiveDocument.Tables(1)
    rng.SetRange .Cell(2, 2).Range.Start, .Cell(4, 4).Range.End
    End With

  3. #3
    3 Star Lounger
    Join Date
    Sep 2004
    Location
    Portsmouth, Hampshire, England
    Posts
    200
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Working with a range of table cells (2002 SP3)

    That's great, one thing that just caught me out is that I had expected

    rng.SetRange .Cell(2, 2).Range.Start, .Cell(4, 4).Range.End

    to be a square range of 9 cells but in fact, in a 5x5 table for example, it includes 14 cells:

    Row 2 cells 2 to 5
    Row 3 cells 1 to 5
    Row 4 cells 1 to 4

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Working with a range of table cells (2002 SP3)

    Sorry, I hadn't checked thoroughly. As a test to see what the code did, I added a line (which I didn't post)

    rng.Select

    When you run the code with this line, a square block of 3 by 3 cells within the table is selected. But the range is as you described. Thinking of it, I don't think there is a workaround. We're up against a fundamental difference between the Range and Selection objects here. A Range object in Word consists of everything between the start position and the end position. The Selection can have different shapes, and need not be contiguous.

  5. #5
    3 Star Lounger
    Join Date
    Sep 2004
    Location
    Portsmouth, Hampshire, England
    Posts
    200
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Working with a range of table cells (2002 SP3)

    Yeah, I just tried

    Set rng = ActiveDocument.Tables(1).Range

    With rng.Columns(3)
    rng.SetRange .Cells(2).Range.Start, .Cells(.Cells.Count).Range.End
    rng.Font.Hidden = True
    End With

    and the effect wasn't restricted to "Columns(3)"

    So I guess I'm gonna have to select the range after all.

    Thanks for your help, couldn't have done it without you!

  6. #6
    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: Working with a range of table cells (2002 SP3)

    If you're curious about how weird it all gets, see this old thread from March 2001 on the VBA board.

Posting Permissions

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