View Full Version : With Selection.Cells(ALL OF THEM)
TASAK
04-16-2017, 01:48 AM
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
macropod
04-16-2017, 02:35 AM
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
TASAK
04-16-2017, 04:21 AM
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?
macropod
04-16-2017, 05:49 AM
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.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.