#1
|
|||
|
|||
Automate the filling in of the rows of a table
Hello sorry for my english I'm French . I would like to fill an array automatically with strings defined in advance.
We start from the basic table with 1 column and 2 rows and with a macro we generate two other columns filled with the names "Zz (number of hardware) Name" in the column "Name and" Zz (number of hardware) Status "in the "State. What I manage to do is the part framed in green. But it will be necessary that I manage to copy the number of the hardwares (1,2,3, etc ...) between "Zz" and "Name" and "Zz" and "State" so that it makes "Zz2Name" and " Zz2State "if I am at hardware N ° 2. What I can't do is in the part framed in red. I would like that if the user wishes to enter X hardwares so that he himself creates X lines that with the macro the X lines created are filled with the names according to the number of the hardware. I don't know if I was clear enough in my explanations. But here is my code which makes it possible to make the framed green part, it would just be necessary to add for this macro the copy of the material number between "Zz" and "Name" and "Zz" and "State" to avoid that I write it manually at each line. Code : Code:
Sub test_Tableau() Dim tab_1 As Table Set tab_1 = ActiveDocument.Tables(1) If ActiveDocument.Tables.Count >= 1 Then tab_1.Columns.Width = InchesToPoints(1.66) Selection.InsertColumnsRight Selection.InsertColumnsRight If tab_1.Rows.Count = 2 Then With tab_1.Cell(Row:=1, Column:=2).Range .InsertAfter Text:="Nom" End With With tab_1.Cell(Row:=1, Column:=3).Range .InsertAfter Text:="Etat" With tab_1.Cell(Row:=2, Column:=2).Range .InsertAfter Text:="Zz1Etat" End With With tab_1.Cell(Row:=2, Column:=3).Range .InsertAfter Text:="Zz1Nom" End With End With End If End If End Sub Thank you in advance ! PS : I will attach an explanatory picture of what I would like to do. |
#2
|
||||
|
||||
Try this
Code:
Sub test2() Dim aTable As Table, iRow As Integer, sPrefix As String, aRow As Row Dim sC1 As String, sC2 As String, sC3 As String sC2 = "Name" sC3 = "State" sPrefix = "Zz" If ActiveDocument.Tables.Count > 0 Then Set aTable = ActiveDocument.Tables(1) aTable.Columns.Width = InchesToPoints(1.66) aTable.Columns.Add aTable.Columns.Add aTable.Cell(1, 2).Range.Text = sC2 aTable.Cell(1, 3).Range.Text = sC3 For iRow = 2 To aTable.Rows.Count Set aRow = aTable.Rows(iRow) sC1 = Split(aRow.Cells(1).Range.Text, vbCr)(0) aRow.Cells(2).Range.Text = sPrefix & sC1 & sC2 aRow.Cells(3).Range.Text = sPrefix & sC1 & sC3 Next iRow End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thank you very much ! This is exactly what I wanted to do. Now I would like all table changes to be done using a bookmark so that I can run the macro anywhere in the document. If anyone has an idea for this ^^
|
#4
|
||||
|
||||
It is not clear what you mean.
You can point at the first selected table by saying Set aTable = Selection.Range.Tables(1) You can point at the first table in the bookmark range by saying Set aTable = ActiveDocument.Bookmarks("myBookishFriend").Range. Tables(1)
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Quote:
I think, it’s exactly what I asked. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Add Rows to table that will include content controls of previous rows | bobsagat | Word VBA | 20 | 01-27-2020 08:00 AM |
Automate filling in PDF Forms from Word? | dlowrey | Word | 5 | 09-23-2014 10:12 PM |
Grouping table rows to prevent individual rows from breaking across pages | dennist77 | Word | 1 | 10-29-2013 11:39 PM |
Text Form Fields - Filling the table cell | simville02 | Word Tables | 1 | 01-31-2013 11:12 PM |
Image filling a Word table cell completely? | Elisabeth | Word Tables | 1 | 07-18-2012 07:21 AM |