nested table in WordI have a template which is essentially a 4-row column in Work. Each cell marked with a bookmark. I've copied a table from Excel to Word into one of these bookmarks (inside on of the cells). Now I'm trying to format this 'nested table' to make it fit according to my desired column widths (for certain columns) but I'm really struggling with the syntax. Additionally, in the table, the first row contains some merged cells (Some are merged to the cell in the adjacent column and some in the row below).
The code I was trying:
With wd.Tables(2)
.Columns(2).Width = 20
End With
But I keep getting "Run-time error '5941': The requested member of the collection does not exist." Does this mean I am not indexing it properly?
Tabels(2) is meant to refer to the 'nested table' within the larger single column of 4 rows cells.
How do index it properly/find its index?
And how would I change the widths when I have merged cells?
Would I need to:
divide them first>adjust width>re-merge?
Also, I'm doing this in VBA Word, if I reference 'Microsoft Word xx.0 Object Library' in excel VBA can I do this in excel?
I have recreated a nested table like the one in the screen shot; i.e. 1 column with 4 rows, then a nested 14 column/10 row table in row 3.
The following code works just fine for me:
Sub AccessNestedTable()
Dim tbl As Table, tbl2 As Table
Dim wd As Document
Set wd = ActiveDocument
Set tbl = wd.Tables(1)
Set tbl2 = tbl.Tables(1)
With tbl2
.Columns.Width = 20
End With
End Sub
Related
I have excel worksheets with a usedrange that consists of two dynamic tables/blocks.
On the right-hand side, there is table 1 with raw data (beginning in cell A1). Then, there is an empty column E:E (for example), followed by some formulas in a table beginning at F1 (for example).
I need to only clear the content of table 1. Table 2 needs to remain where it is and I was not yet able to figure out how.
When I do
Set StartCell1 = Range("A1") ' select first cell of table 1
StartCell1.CurrentRegion.Select ' attempt to select whole table 1
Selection.ClearContents ' attempt to clear table 1
Both table 1 and table 2 are deleted.
How to select only the subset of a usedrange that is continuous (i.e. no empty columns/rows) in excel-VBA?
Note that the dimensions of both tables as well as the number of blank columns in between varies between sheets.
I have a worksheet here with multiple columns full of data, ranging from cell AA to CT currently. The first column contains rows with headings and is frozen. Data is added to the end column of this range weekly. & Then all columns between the first and the last four are deleted (to show a month's worth of data but keep the headings intact).
I'm very new to VBA and I've been trying to write some code to automate this, so far I've managed to select the 5th column in from the end. But I can't get it to select the columns in between the 4th column from the end and Cell AA:
Sub DeleteColumns()
Dim i
i = Range("KFIData").End(xlToRight).Offset(0,-4).EntireColumn.Select
Columns("AA:ColumnFive").Delete
End Sub
Am I going about this completely the wrong way?
Many thanks
Well if you managed to catch column 5 from the end, use this statement:
Range(Columns("AA"), Columns(ColumnFiveFromEnd)).Delete
ColumnFiveFromEnd can be a number as well as a text identifier.
I have a table object in the sheet which is used by many formulas in my workbook.
The table object is created in a VBA routine.
The issue is any formulas refering to the table get broken if the table is re-created in the VBA routine. Is it possible to avoid this.
For example
=SUMIFS(output_dump[Value],output_dump[assetClass],"ML")
gets broken when the table is deleted and recreated with the same name during the VBA routine
=SUMIFS(#REF!,#REF!,"ML")
Is there a way of locking the formulas in the sheet or preventing them from updating during a VBA routine?
just insert rows in your table and any references of it will be updated to encompass the new data rows range
Option Explicit
Sub main()
Dim tbl As ListObject
Set tbl = Worksheets("table").ListObjects("output_dump") '<--| change "table" to your actual worksheet name
With tbl.DataBodyRange.Rows(2) '<-- reference data 2nd row
.Insert '<-- insert a row -> you'll have a new empty row between data row 1 and 2
.Offset(-1).Cells(1, 1) = "ML" '<-- fill inserted row row index (.Offset(-1)) first column (.Cells(1, 1))
.Offset(-1).Cells(1, 2) = 100 '<-- fill inserted row row index (.Offset(-1)) second column (.Cells(1, 2))
End With
End Sub
Right now I have a really long table in a Word doc which I populated from an Excel worksheet. It has 6 columns and I'm trying to code something in Word VBA that will go through all the rows in the table and delete the entire row if the cell in the first column DOES NOT start with an equal sign ("=").
For example, I'm only trying to keep the rows that has texts like,
"=1+S -03F7", "=1+M -06M1", etc. etc.
How would I code this? I can't give the code anything specific to look for since the parts after the equal sign will be different for every row.
So this wouldn't work, right?:
If Not ActiveDocument.Tables(83).Columns(1).Range.Text = "=" Then
EntireRow.Select
Selection.Delete
I guess I should reference to cells in column 1, not the column itself... Also, it doesn't work because it's only looking for things with just the equal sign... And I don't know how I can get it to select the row if it find the cell without the equal sign. I don't know how to match by case in the cell of the first column.
You can loop through the rows in the table using the Rows property. You can then find the first cell in that Row using the Cells property. You can then check just the first character of the Range:
Sub DeleteUnwantedRows()
Dim t As Table
Dim r As Row
Set t = ActiveDocument.Tables(1)
For Each r In t.Rows
If r.Cells(1).Range.Characters(1) <> "=" Then r.Delete
Next r
End Sub
I have a table (table of listobject type). I sorted it, filtered it and viewed the sheet. All is well. Now I attempt to enumerate...
Where
oWs_ma is the worksheet that contains the table
oLO_maTable is the table on that worksheet
oRg are range objects
all vars are strings
In the loop below, the temporary range object returns the correct row number. This row number is the number the sheet shows. For example, the 1st data row in the table lies on sheet row 5. In all but one single row, out of many, worksheet and table objects return identical values. In the single instance where they do not, the worksheet is correct. Just one error in 30 or 40 rows. This is not an end point error. It happens midway in the table. There appears to be nothing unique before the error. In fact, the row value reported by oRg_tmp.row changes and points to the correct row!
Is this construct correct? There is additional code include for testing purposes, just to show that what I am getting is indeed "right"
For Each oRg_tmp In oLO_maTable.DataBodyRange.Rows.SpecialCells(xlCellTypeVisible).Rows
Set oRg_maOwer = oLO_maTable.Range.Cells(oRg_tmp.row, colndx_ma_it_owner)
Set oRg_maGLDept = oLO_maTable.Range.Cells(oRg_tmp.row, colndx_ma_gl_dept)
ma_owner = oRg_maOwer.Value2
ma_gl_dept = oRg_maGLDept.Value2
ma_owner_ws = oWs_ma.Cells(oRg_tmp.row, colndx_ma_it_owner).Value2
ma_gl_dept_ws = oWs_ma.Cells(oRg_tmp.row, colndx_ma_gl_dept).Value2
...
Next
If the construct is correct, this is solved. I will use the worksheet.
Thanks.