Is There a More Efficient Way of Determining Whether a Table Row Spills Over Into the Footer Than Looping Through All Cells? - vba

I'm currently using Microsoft Word 2016 VBA to programmatically set the AllowBreakAcrossPages setting so that if a row's contents overlaps the footer contents, I toggle the setting so that the row can break across pages. To solve this issue, I'm thinking about looping through all the cells of each table in the document and get the wdVerticalPositionRelativeToPage.Information of the last characters in each cell and comparing it to the footer's height relative to the page. If the last character's height is greater than the footer's, I set the row to break across pages. You can imagine that this is taking a while in a document that has many tables and cells. Is there a more efficient way to know if the row contents overlap the footer contents than looping through each cell in the row to see if a cell is causing the row to overlap the footer?
I tried using the row height property but discovered that if the HeightRule property of the specified row is wdRowHeightAuto then that means I can't access the row height via VBA.
I discovered that it's possible to use wdVerticalPositionRelativeToPage.Information with a row by row comparison of the prior and next rows like described in another posting - https://stackoverflow.com/a/14753231/4372244.
The only problem with that solution is that it doesn't work for the last row in a table or if it's the last row on a page.
Therefore, I'm examining a cell by cell solution.

Related

How to set the cell value for displayed cells only?

I have some data I want to display in a StringGrid. This data contains many rows with some pretty static columns but also two columns that need web-API lookups and take some time to gather. SO when I try to set these cells in the OnShow of the StringGrid (or any other "Init") I get a long delay until the cell values are set.
What I would like to do is only look up the values for those cells that are currently displayed (and not all of them at once).
How Do I find out which rows are currently displayed? Is there an event I can use?

How do I check to which row area field a cell in a pivottable belongs to?

I have a PivotTable in compact form layout (default layout) with multiple row area fields. This results in multiple grouping levels with buttons to collapse or expand rows.
I want to modify a cell depending on the grouping level the row is in.
How can I check in which row the cell is in?
The only idea I had so far was to check the value of the leftmost cell in the corresponding row and compare it with each item from each rowField.
But I guess there is a smarter way?
It took me a while. But at the end I just needed PivotCell.RowItems.Count
Depending on the Count result I can new manipulate the cell.

How to avoid adding a new row to the first row of subtotaled ranges to protect Subtotal formula

I have a Excel sheet, which includes many groups and subgroups. Each subgroup has their own subtotal. And as long as I can't know how many lines users will require for the subgroups, I only left 3 rows blank for each subgroup and created a button which triggers Automatically Copy and Insert Copied Row macro.
Now I need to avoid users to trigger the button while they are on the First and the Last row of any subgroup. (If you copy and insert first row, subtotal formula doesn't recognise the new added line because it will be out of the range and Last row has another specific formulas which I don't want users to copy above)
I've been searching SO and the other Excel blogs for a long time but because of my list is very dynamic and have many different subtotals none of them was suitable to my case.
How can I define all of the first and last rows of my different subtotal ranges (and maybe their Title's rows) to avoid them being copied and then inserted above?
I can imagine 2 ways to do this. When the "New Row" button is pressed check if the current row is a headline/subheadline whatever …
by checking its format (eg specific background color etc).
If Not Worksheets("Sheet1").Range("A" & iRow).Interior.Color = 15004911 Then
'copy that row
End If
or by using an extra helper column that specifies the rows as header rows or non copyable rows. If you don't want the helper column to be seen you can hide it.
If Not Worksheets("Sheet1").Range("X" & iRow).Value = "Header" Then
'copy that row
End If
'where X is the helper column
And if it is a headline row then deny the copy process.
One way that doesn't involve programming have your sum range extend into rows that won't change. So you could start the subtotal in the header row (assuming there is no content in the relevant cell in that row). Another way is to have a hidden row at the top and bottom of each sum range and that is included in the sum range. So you would sum rows 10 to 14, but 10 and 14 would be hidden (and empty) and the user would just get shown rows 11, 12 and 13. Adding a new row would push the hidden rows down and extend the subtotal.
Another way is to use the indirect function.
Say your formula was
=SUBTOTAL(9,H7:H10)
If you use indirect for the lower bound, it will always refer to the cell immediately above, regardless of how many rows are added in between.
=SUBTOTAL(9,H7:INDIRECT("H"&ROW()-1))
And taking it one step further, use the upper title row as the anchor to always add up the gap between the title and the subtotal.
=SUBTOTAL(9,INDIRECT("H"&ROW(H6)+1):INDIRECT("H"&ROW()-1))

Apache POI sheet page break

If I have cell coordinate, can I somehow get sheets next page break coordinate for first cell? I need this for printing purposes, because I'm making rather complex report and I want to copy in last page explanations. Row sizes varies, so I can't depend on fixed row count.

Is it possible to set vb.net datagridview cell alignment without modifying whole column?

I am trying to set the alignment of a specific cell in a row/column to be right aligned. However I do not want to set the whole row, or whole column to right aligned. Is it possible to set the cell only? From what I've seen online I'm starting to think it isn't possible.
You would need to hook up to the RowDataBound event. This fires as each row is databound. Check that the row is a data row (as opposed to a header or footer). Then check the value in the column you are interested in. If the value meets your criteria for right justification then apply that to the column in question.
Note if you are using AlternateItemTemplate then check both Item rows and AlternateItem rows.
I've used this method to say change the backround colour of values that fall outside a range.