• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 844
  • Last Modified:

set a decimal tab using VBA

Dear Experts:

I would like to insert a decimal tab at 1.7 cm in all cells of Column 2 and 3 (starting from row 2) of the currently selected table using VBA. I would like to get this code snippet integrated into an already existing macro.

That is what the macro recorder came up with:
'Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(1.7) _
'        , Alignment:=wdAlignTabDecimal, Leader:=wdTabLeaderSpaces



Help is very much appreciated. Thank you very much in advance.

Regards,  Andreas


Sub Format_Columns_Add_Decimal_tabs_selected_table()

'to be expanded by inserting a decimal tab at 1.7 cm in all cells starting from row 2 of Column 2 and 3


Dim mytable As Table

Set mytable = Selection.Tables(1)
    With mytable
       .PreferredWidth = 97.5
       .PreferredWidthType = wdPreferredWidthPercent
       .Columns(2).Width = CentimetersToPoints(3.1)
       .Columns(3).Width = CentimetersToPoints(3.1)
       .PreferredWidth = 97.5
       .PreferredWidthType = wdPreferredWidthPercent
    End With
End Sub

Open in new window

0
AndreasHermle
Asked:
AndreasHermle
  • 2
1 Solution
 
Rgonzo1971Commented:
Hi,

pls try

Sub Format_Columns_Add_Decimal_tabs_selected_table()

'to be expanded by inserting a decimal tab at 1.7 cm in all cells starting from row 2 of Column 2 and 3


Dim mytable As Table
Set d = ActiveDocument
Set mytable = Selection.Tables(1)
    With mytable
        For IdxRow = 2 To .Rows.Count
            For IdxCol = 2 To 3
                Set c = .Cell(IdxRow, IdxCol)
                c.Range.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(1.7) _
                    , Alignment:=wdAlignTabDecimal, Leader:=wdTabLeaderSpaces
            Next
        Next
       .PreferredWidth = 97.5
       .PreferredWidthType = wdPreferredWidthPercent
       .Columns(2).Width = CentimetersToPoints(3.1)
       .Columns(3).Width = CentimetersToPoints(3.1)
       .PreferredWidth = 97.5
       .PreferredWidthType = wdPreferredWidthPercent
    End With
End Sub

Open in new window

EDIT used the with mytable

Regards
0
 
AndreasHermleAuthor Commented:
Hi Rgonzo,

works like a charm. Thank you very much for your swift and professional help.

I deleted 'stop' on line 15. I am sure you placed this just for testing, am I right?

I will post a similar question soon.

Regards, Andreas
0
 
AndreasHermleAuthor Commented:
Rgonzo, as always, very professional and superb support. Thank you very much for it.

Regards, Andreas
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now