Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 27 of 27

Thread: Solved: Loop through cells in a column (Word table)

  1. #21
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I am really not following what it is you actually need.

    "tab stop changes based on the longest number in the column "

    From looking at the image you supplied in post #3, ummm, why not simply make it right aligned?

    I still do not see why you are messing around with tabs.

  2. #22
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    Hi Macropod,

    Your sample code is taking the position from the first row instead of the row with the longest number. I tested this with a number like "$34" in row 1 and the tabstop lines up with that position instead of the one with the longest number.

    I've tried going through the code to see if I can tweak it but it's a bit beyond me. Is it an easy fix?

    Thanks again guys, this is a good learning experience for me. I rarely code in Word.
    Office 2010, Windows 7
    goal: to learn the most efficient way

  3. #23
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    Fumei, I'm not ignoring you. I'm just trying to word it in as clear a manner as I possibly can so I don't make you more frustrated.

    It might take me a while. I think a better (new) screenshot will help.
    Office 2010, Windows 7
    goal: to learn the most efficient way

  4. #24
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi TrippyTom,

    Oops! Instead of:
        If Len(.Range.Text) > j Then
          j = Len(.Range.Text)
    I should have used:
        If Len(.Cell(i, 2).Range.Text) > j Then
          j = Len(.Cell(i, 2).Range.Text)
    Note that the macro this is from is only coded to process column 2 in the 1st table. I'll leave it to you to add the necessary code to process multiple tables & columns - the code you've already got gives enough hints on that.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #25
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    I almost hate to ask this, but...

    How easy would it be to get the right most location as well so I can set the decimal tab too? I'm centering Column 2 first, then running this code that I tried, based on yours...
    [VBA] With .Columns(2).Cells(k)
    .Select
    Selection.HomeKey Unit:=wdLine
    LeftPos = Selection.Information(wdHorizontalPositionRelativeToTextBoundary)
    Selection.EndKey Unit:=wdLine
    RightPos = Selection.Information(wdHorizontalPositionRelativeToTextBoundary)
    End With[/VBA]


    and tried using those variables here:
    [VBA]For i = 1 To .Rows.Count
    If i = .Rows.Count Then 'last row
    .Cell(i, 2).Range.Paragraphs(1).TabStops.Add LeftPos, wdAlignTabLeft
    .Cell(i, 2).Range.Paragraphs(1).TabStops.Add RightPos, wdAlignTabRight
    Else
    .Cell(i, 2).Range.Paragraphs(1).TabStops.Add LeftPos, wdAlignTabLeft
    .Cell(i, 2).Range.Paragraphs(1).TabStops.Add RightPos, wdAlignTabDecimal
    End If
    Next[/VBA]

    But it's shifting both tabs to the right too far. This whole range thing is really difficult to understand for me.
    Office 2010, Windows 7
    goal: to learn the most efficient way

  6. #26
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    Wow, I think I figured it out. Is this how you would have done it?
    [vba] With ActiveDocument.Tables(1)
    For i = 1 To .Rows.Count
    If Len(.Cell(i, 2).Range.Text) > j Then
    j = Len(.Cell(i, 2).Range.Text)
    k = i
    End If
    Next
    Set TabRng = .Cell(k, 2).Range.Characters(1)
    TabRng.MoveEnd wdCharacter, -1
    LeftPos = TabRng.Information(wdHorizontalPositionRelativeToTextBoundary)

    myCharLength = .Cell(k, 2).Range.Characters.Count
    Set TabRightRng = .Cell(k, 2).Range.Characters(myCharLength)
    TabRightRng.MoveEnd wdCharacter, -1
    RightPos = TabRightRng.Information(wdHorizontalPositionRelativeToTextBoundary)

    For i = 1 To .Rows.Count
    .Cell(i, 2).Range.Paragraphs(1).TabStops.Add LeftPos, wdAlignTabLeft
    .Cell(i, 2).Range.Paragraphs(1).TabStops.Add RightPos, wdAlignTabDecimal
    Next
    End With[/vba]
    Office 2010, Windows 7
    goal: to learn the most efficient way

  7. #27
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi TrippyTom,

    If I were doing this, I think I'd be adding the decimal tabstops *before* tring to do anything with the left tabstops. As for the left tabs and tabstops , I suspect you probably only need one of each per column - for the row on which the underlining is to be applied.

    Also, you probably should have your code delete any existing left tabstops in the columns concerned before calculating & adding the new one(s). Otherwise, there's the prospect of having too many tabstops if you need to re-run the code. Similarly, you could test for the presence of any decimal tabstops so that you don't add them unnecessarily - or, perhaps you could delete them (as per the left tabstops) if there's the prospect of having to add new ones at a different position.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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