Consulting

Results 1 to 4 of 4

Thread: With Selection.Cells(ALL OF THEM)

  1. #1

    With Selection.Cells(ALL OF THEM)

    Hi guys, I'm absolutely newbie to VBA. Really sorry for this stupid question. My problem is about selecting cells. My sample code is below:


    Selection.Tables(1).Columns(2).Select
        With Selection.Cells(1)
            .LeftPadding = CentimetersToPoints(0.30)
            .WordWrap = True
            .FitText = False
        End With
    This code affects only first row. I want to change all rows on the 2nd column. Is there any solution like With Selection.Cells(ALL)

    By the way I don't know how many rows.

    Thanks for all

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Try:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long
    With Selection
      If .Information(wdWithInTable) = False Then Exit Sub
      With .Tables(1).Columns(2)
        For i = 1# To .Cells.Count
          With .Cells(i)
            .LeftPadding = CentimetersToPoints(0.3)
            .WordWrap = True
            .FitText = False
          End With
        Next
      End With
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Thanks macropod!

    It works very well on sample document..

    But my real table has over 20000 rows. And the process is very slow. The window is still freeze. My question: When I select column manually the changes are done in one step. Is there a VBA restriction? Is it impossible to do without if-while loops? Does VBA not allow?

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by TASAK View Post
    But my real table has over 20000 rows. And the process is very slow.
    IMHO a table of that length is ridiculous. Something that size should be managed in Excel.

    That said, if the table has autofitting enabled, disabling it will speed things up considerably - and that can be done in code. You might also add a DoEvents line to the loop, to give Word a chance to breathe, e.g.:
    If i Mod 500 = 0 Then DoEvents

    As for the differences between the GUI and VBA; not everything possible in the GUI can be done in VBA.
    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
  •