I'm writing a program that uses the OpenXML SDK to read OpenXML or Microsoft .docx files, and I've encountered a problem with tables. Because I know a user can create a lot of things, and Word loads, edits, and saves them, I know this is possible. Using Word 2010, I created a table with the following configuration:
This is designed to test horizontal merge, which can be accessed via the GridSpan property, and also vertical merge.
The problem I'm having is that the program can determine whether a cell has a vertical merge--but it cannot tell when that vertical merge ends. There is no RowSpan property for Wordprocessing.TableCell items that reliably lets me answer my question.
Dim objCell as Wordprocessing.TableCell
' Only operates when table cells have a vertical merge property...
If Not IsNothing(objSetup.VerticalMerge) Then
' Only operates if the vertical merge property exists...
If Not IsNothing(objSetup.VerticalMerge.Val) Then
' Sets the starting column number...
intColumn = 1
' Prepares to get the current row...
Dim trRow As Wordprocessing.TableRow
' Obtains the current row...
trRow = objCell.Parent
' Loops through table cells in the current row...
For Each tcCell As Wordprocessing.TableCell In trRow
' Counts the column...
intColumn = intColumn + 1
' Responds if the cell is identified...
If tcCell.OuterXml = objWord.OuterXml Then
' TEST: Notifies the user...
MsgBox("Matching cell found in column " & intColumn & vbCrLf & vbCrLf & objWord.OuterXml)
End If
Next
' TEST: Notifies the user...
MsgBox("Switching to Next Cell")
End If
End If
This program will consistently mistake any table cell with matching height as the current cell, even though the program is part of a loop that reads individual cells from the table, one at a time.
In the upper left corner of this example, where there are three cells with identical row heights, checking one cell will return three cells as "matches." Yes, they span two rows each, but no, the fact that there are three of them means they can't all be the same cell.
Is there any way to identify a specific cell (whether empty or not) in a specific column and a specific row while looping through a table using the OpenXML SDK?
Related
I have to proofread about 30-35 spreadsheets every week.
I need to look at column W and the columns to the right of column W. When I initially display the spreadsheet I see columns A-P. I manually scroll over to column W and begin proofreading the data.
I click on a next button which steps to the next file. My code loads the next spreadsheet:
WorkbookView1.ActiveWorkSheet = Workbook_Obj.ActiveWorksheet.
WorkbookView1.Update()
Sometimes the horizontal scroll position remains the same and I can see column W and some of the columns to the right of column W.
But sometimes the horizontal scroll position changes to the left of column W and I can't see column W anymore.
I'd to do something like this:
' 23 = Column W
Col_Obj = WorkbookView1.Cells(0, 23)
' Or - find column by header text in row = 0.
Col_Obj - WorkbookView1.FindByText("Name")
Col_Obj.HorizontalScroll = Col_Obj.Location
Thanks, Ed
You can set IWorksheetWindowInfo.ScrollColumn (note this property uses a zero-based index, i.e., where Column A is index 0, B is 1, and so on), which will set the leftmost column displayed in the window. Note there is a ScrollRow property as well if you need to set the row scroll position.
When a WorkbookView is involved, such as in your case, a number of additional factors can come into play as far as whether setting properties such as ScrollColumn will actually take effect on the WorkbookView (this has to do with the fact that you can attach the same worksheet to multiple WorkbookView controls and each WorkbookView can keep its own copy of these "window info" properties). Also, there are some behavioral differences in the way SpreadsheetGear 2012 and 2017 work in this regard. The below code should work in most cases and hopefully yours as well, though it may not in certain circumstances...for instance, if you are using SpreadsheetGear 2017 and attaching this worksheet to multiple WorkbookViews which I'm guessing you're not; but if so, I can update this answer to better accommodate your particular situation.
' Make local variable to the desired worksheet
Dim activeWorksheet = Workbook_Obj.ActiveWorksheet
' Reference cell to select and scroll to
Dim cellW1 = activeWorksheet.Cells("W1")
' Select the cell (this call alone can sometimes also automatically
' trigger the cell to be scrolled to as well, though it may not depending
' on the circumstances; and it may not make this cell the leftmost in the
' window...
cellW1.Select()
' ...to avoid any problems like mentioned above, explicitly set ScrollColumn
' to the selected cell. Set ScrollRow as well if desired.
activeWorksheet.WindowInfo.ScrollColumn = cellW1.Column
' Attach workbook *AFTER* doing the above. For a variety of reasons I
' won't go into, attaching the worksheet before setting the above "window
' info" options may not have any effect.
WorkbookView1.ActiveWorksheet = Workbook_Obj.ActiveWorksheet
I am just getting started with VBA, using my old copy of Word 2010. I want to resize two of the columns in a three column table and format the text that is in the columns. This code was generated by Word's macro recorder:
Selection.ConvertToTable Separator:=wdSeparateByCommas, NumColumns:=3, _
NumRows:=14, AutoFitBehavior:=wdAutoFitContent
With Selection.Tables(1)
.Style = "Table Grid"
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
End With
Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter
Now I want the first two columns to be 1.3 inches wide and the third column to be 5.1 inches wide. Then I want to change the formatting of the text in the third column to increase the font size. The macro recorder doesn't seem to record when I resize columns. Any suggestions as to how to edit this code?
Controlling tables is rather complicated. Word does much of it the way it sees fit, but if you use VBA you must take control. It really isn't desirable that you should make your life even more miserable by invoking the Selection object. Try and make your object the table itself, for example, like this.
Dim Tbl As Table
Set Tbl = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=14, _
NumColumns:=3, _
DefaultTableBehavior:=wdWord8TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed)
' wdWord8TableBehavior = doesn't change table size to match content
The table will be inserted following the selection. You may want to collapse your selection range before running this code. But now that you have a table object you can format it to your heart's content without selecting anything. There are dozens of properties. One of them which you will want to determine is Tbl.PreferredWidth = InchesToPoints(7.7) Word will not set this property automatically to the total width of your columns.
Your table has 3 columns which you can address as Tbl.Columns(1) to 3. You can set the width for each column, like Tbl.Columns(3).Width = InchesToPoints(5.1)
Similarly, you can address each row as Tbl.Rows(1) and up. Individual cells are addressed by Row and Column numbers, for example, Tbl.Cell(1, 3) which is the 3rd cell in the first row. Avoid merging cells because that will prevent VBA from being able to count them off.
The text in a cell is contained in its range, for example, Tbl.Cell(1, 3).Range.Text. You can both read and write this property. Bear in mind that Word keeps a paragraph-end mark at the end of each cell's range. When you write to a cell Word will add it for you, even if you thought of adding it yourself. But when you read a cell's text you need to remove the last character, for example,
Dim Rng As Range
Set Rng = Tbl.Cell(1, 3).Range
With Rng
.End = .End - 1
Fun = .Text
End With
Here Fun is the variable (As String) that contains the actual text part of the cell's contents.
You can address the paragraphs within a cell as part of the range, for example, Tbl.Cell(1, 3).Range.Paragraphs(1). I always avoid having more than 1 paragraph into any cell. However, you could address more paragraphs as (2) etc. You can apply all formatting available for paragraphs to each paragraph in each cell, and all the text in each paragraph can be subjected to all the formatting Word has available for text.
I am trying to add to a macro I have that will hide every row that has no text in a column named Authorization. Please see the code I have below, I thought this may be on the right track but it does not hide any rows.
Cells.EntireRow.Hidden = False
For Each cell In Range("Authorization").End(xlUp)
If cell = "" And cell.Offset(1, 0) = "" Then cell.EntireRow.Hidden = True
Next cell
Edited to add how to define a dynamic named range
It is the fact that you have set the whole column to the name "Authorisation" that I think makes your code freeze, because the whole column is 1 million rows (if you have 2007 or above), and the code will still check even blank rows, so its doing it 1 million times. 1 Option is to rather than set it to the whole column, you could use a "Dynamic Named Range" which will expand and grow as data is added. There are several different formulas to do this, but based on the fact your data may contain blanks, this version of the formula will expand down to the last populated row in the column. My example uses colum A as you havent specified what column you are using, so change A to suit your needs.
You need to open the Names manager, from the Formulas tab
From the dialog box, find your "Authorisation" name.
Select it and you should see its current formula at the bottom of the dialog box, replace that with the following formula:
=OFFSET(Sheet1!$A$3,0,0,MATCH("*",Sheet1!$A:$A,-1)-2,1)
In the above formula:
Sheet1 is my sheet, replace it with yours a needed
$A$3 is the starting row of the name, so based on your comments, have set this as column A row 3
0,0, Are defaults you should not need to change
$A$A$ is the column it is counting values, so change as required
-1 is a default, leave as is
-2 is subtracting 2 from the count because we are starting on row 3, so if you change the starting row, change this
the last 1, defined how many columns your named range covers, in your example it is just 1, so this should not need changing.
Once you have defined the name in this way, the code below should work a lot quicker as it will only loop through down to the last row of entered data. There is one possible issue I can see with this and that is if the very last cell in column A is blank, but the rest of the row isn't, this will miss out the last row. I could fix this by using a different column to count what constitutes the last row, but need to know whicj column would always have a value in it.
< Original answer and code>
not sure you code matches the description of what you want it to do, namely you seem to be trying to check the row beneath the current cell as well, is this what you really wanted? Anyhow your syntax is slightly wrong. I have written and tested this and it works, I have swapped your offset around so my code is checking the cell in the named range "Authorisation" and then also checking the cell to the right. Amend to suit your needs
Sub test()
Dim c As Range
For Each c In Range("Authorisation").Cells
If c.Value = "" And c.Offset(0, 1).Value = "" Then c.EntireRow.Hidden = True
Next c
End Sub
I have a SAP Report embedded in a worksheet, it is refreshed via a macro using variables defined in another worksheet. That all works fine, but i am having trouble selecting the data the report generates.
The headings of the report are in and always will fall in this range ("A17:K17"), but the results rows will vary making the total range I want to capture anywhere from ("A17:K18") to (A17:K1000").
The solutions I've already tried didn't work i think because there is almost no consistency in the result data, it's a mixture of text and numbers with empty cells all over the place, in both the rows and columns. Including the occasional completely empty row. This means the methods I have tried before reach a point where it thinks it's reached the end of the populated rows - but it hasn't.
The only factor that remains the same throughout the report is that the cells in the range I want to capture are all filled with a color as default and anything outside the range is unfilled.
To me the simplest solution would be to use VBA to select all the cells beneath and including the headers on ("A17:K17") where the color index is not 0 (blank?) regardless of their contents as I don't mind capturing empty cells. Except I don't know how to do this.
At this point I'd just like to select this range I haven't decided if I'm going to copy it into a new workbook or into an email yet, but that I can do. I've just hit a dead end selecting it.
Quite unsure exactly what it is you require but here's a solution. It's worth noting that both the ColorIndex and Color properties are not necessarily zero with no fill, so if you just change blankCell to a cell with the fill which you define to be blank you'll be good to go.
Sub test()
Set blankCell = Range("A1") ' change this to a cell that you define to be blank
blankIndex = blankCell.Interior.Color
Set cellsDesired = Range("A17:K17")
For Each cell In Range("A17:K1000")
If cell.Interior.Color <> blankIndex Then
Set cellsDesired = Application.Union(cellsDesired, Range(cell.Address))
End If
Next cell
cellsDesired.Select
End Sub
Is it possible to have the formulas that I need applied on columns be saved or applied to a column header or some kind of metadata so that as and when I add new rows to my Excel table the Formulas get applied to the columns?
Scenarion:
I am creating a template Table, which will have no rows at first.
On a separate sheet (or same sheet for that matter) once the user selects the number of rows to be generated in the table, I dynamically add rows to the table using VBA.
The idea is I may not have any rows in the table at beginning OR user may have deleted rows manually.
When I programmatically add new rows, I want the Formulas applied on the cells as well. Most of the formulas I am using are either of the three types:
Structured table reference, Excel functions like SUM, AVERAGE etc and custom function names.
Updated:
Here is what I have tried:
1> tried applying the formula to the header itself.
Result: The header it self changes with #REF! error. I think the behavior is correct. So it's a no-go option.
2> Tried creating one row and apply the formula to the row. That works, but the problem is, I do not want a dummy row to begin with.
3> Using VBA code to add row to the table using
ActiveWorkbook.Worksheets("Sheet3").ListObjects("Table2").ListRows.Add AlwaysInsert:=True
inside a for loop.
The new rows retain the visual style sheets, but does not seem to retain the formulas. Just blank cells.
Could the fomrmulas be in header cell commnets?
And then with VBA add the formula for the current row:
Sub test()
Dim headerCells As Range
Set headerCells = Range("B2:E2")
OnNewRow 3, headerCells
End Sub
Sub OnNewRow(newRow As Integer, headerCells As Range)
Dim headerCell As Range, targetCell As Range, formulaFromComment As String
For Each headerCell In headerCells
formulaFromComment = GetFormulaFromComment(headerCell)
If (formulaFromComment = "") Then _
GoTo NextHeaderCell
Set targetCell = Intersect(headerCells.Worksheet.Rows(newRow), _
headerCell.EntireColumn)
AddFormula newRow, targetCell, formulaFromComment
NextHeaderCell:
Next
End Sub
Sub AddFormula( _
newRow As Integer, _
targetCell As Range, _
formula As String)
formula = Replace(formula, "{ROW}", newRow)
targetCell.formula = formula
End Sub
Function GetFormulaFromComment(headerCells As Range) As String
' TODO
GetFormulaFromComment = "=SUM($C${ROW}:$E${ROW})"
End Function
Just use tables.
If you highlight cells and choose Insert Table from the ribbon, it doesn't just give you formatting and filters. It also, if you build them the right way, stores column formulas once per column instead of once per cell. Also, the formulas are more readable!
For formulas, you can't use cell addresses if you want it to be a single column formula unless they are absolute. (E.g. $A$1, not A1.) Instead, you use [ColumnTitle] for the entire column (where "ColumnTitle" is the actual title of that column) and [#ColumnTitle] for the column value in the same row. So if "Cost" was the title of column B, "RunningTotal" was the title of column C and your formula for C6 was therefore =B6+C5, you'd instead use a formula of =[#Cost]+OFFSET([#RunningTotal],-1,0)] which is longer but much easier to read/maintain/debug, and if you change a column title then the formulas change too! No VBA required. Given this, plus being able change columns for the entire columns at once, plus being able to refer to other columns in other tables without worrying about cell addresses (e.g. MAX(Table1[Cost])), plus being able to style the tables so easily, plus the integration with Power-Query, and VBA support. (See learn.microsoft.com.) Whether VBA or otherwise, add a row to your table and the columns with a single column formulas will automatically carry over into the new row.
Not sure about Table templates or VBA but perhaps there is another option by using =ARRAYFORMULA()
For example, say you had a header row and 3 columns and wanted your last column to be the product of the first two. In cell C2 you could enter the following:
=ARRAYFORMULA(A2:A*B2:B)
This has three benefits:
Skips the first row completely
Effectively applies the formula to every row which is useful if you later decide to insert a row (your question)
Only one location to modify the formula for every single row
Although, it may not be immediately obvious where how/where the cells are being calculated. (hint: ctrl+~ may help)