Set table row height of row containing the cursor - vba

I am trying to set the table row height for the Word table row that contains the cursor in one of the cells in the row.
No text is selected; I click on a cell.
Here is my C# code that loops over all rows in the selection and adjusts the row height.
foreach (Row current in sel.Rows) {
current.Height = InchesToPoints(height);
}
The problem is that the code works if the selection contains multiple rows (click in a cell and drag down to a second row), but the code skips the loop if the cursor is placed in a single cell by clicking within the cell. I checked the value of sel.Rows.Count and it is 1 for the single row case, so I expect the loop to run once. But it skips the loop as if sel.Rows.Count was zero.
The selection.Type is selectionIP (in paragraph), and the selection is in a table.
Why would the loop work for two or more rows, but skip the loop when sel.Rows.Count==1? It's like the sel.row.count of 1 is not being treated as a row.
Maybe I need to programmatically select the entire row?

The selection.Type is selectionIP (in paragraph), and of course the
selection is in a table.
This is entirely to be expected. Your selection does not contain any rows as the insertion point has been placed inside a cell. As a result the selection does not contain any table elements.
They can, however, be obtained.
If Selection.Information(wdWithInTable) returns true Selection.Information(wdEndOfRangeRowNumber) will provide the row number.
You can then set the row height using:
Selection.Tables(1).Rows(Selection.Information(wdEndOfRangeRowNumber)).Height = InchesToPoints(height)

Related

Compare Excel sheets values to update a third value

Example file So I have two sheets that each have lists of part numbers, plant where they come from and two columns on costs. What I need to do is scan them and if Sheet A and Sheet B both have a row with matching part numbers and the plant they come from, then A's two cost values are updated to match B's costs.
The next step is then to highlight all cells in Sheet A that are not on Sheet B and highlight all cells in Sheet B that were copied to Sheet A. I think this last part can be done at the same time the cell is being copied I'm just not sure how to do any of this.
This is a formula method.
Because you will not be changing all the values and I assume you want to keep those that do not have a match, then in an empty column next to the figures on sheet 1 put the following formula:
=IFERROR(INDEX(Sheet2!F$3:F$7,MATCH(1,INDEX((Sheet2!$D$3:$D$7=$A3)*(Sheet2!$B$3:$B$7=$C3),),0)),G3)
Then copy over one column and down the the end of the data.
The INDEX((Sheet2!$D$3:$D$7=$A3)*(Sheet2!$B$3:$B$7=$C3),) will create an array of 0 and 1's the same size as the data reference on sheet 2. In this instance it will create a 1 dimensional array that is 5 objects.
The position of these objects of 0 and 1 are relative to the rows. So for the first formula the return array will be {0,1,0,0,0} because only the second row of the data matches both the plant and the part number.
The MATCH(1,INDEX(...),0) then finds the first object in that array that is 1 and returns the relative position, in this case 2 as it is the second in the array.
The Outer INDEX(Sheet2!F$3:F$7,...) then returns the value in the range Sheet2!F$3:F$7 whose relative position is equal to the 2 passed from the MATCH(). So Sheet2!F4.
If no MATCH is found then the whole thing will throw a #N/A error so we capture that error with IFERROR(...,G3) and tell the formula to return the value in column G instead.
This will give you all the proper values:
Then you can copy and paste just the values back to the original spots and hide the columns with the formulas:
Sheet2 for reference:
If you want vba to do the last part of copy and past and hiding then use the macro recorder and then clean up the code.

Color Grid in Excel by vb.NET

I need some help.
What I need is a code for colorid the Grid in Excel in this way:
find the first cell with value ( 1 )
find the last cell with value ( 2 )
range (1:2)
Color the grid black
My code by now:
'Color the columns A to K and ALL the rows below, no matter if they have a value or not.
xlWorkSheet.Range(xlWorkSheet.Cells(1, 1), xlWorkSheet.Cells(xlWorkSheet.Rows.Count, 11)).Borders.ColorIndex = 0
I need to change the color of not-empty rows .
THX
Find the first cell :
You should active the first cell (A1) and use IsEmpty function.
Then you loop in the same column until you find a cell containing a value.
By the way, you need to keep the number of this row for the second point.
Find the last cell :
You have to exit your loop when it finds a new empty row. Then you'll get the range of cells using the number of first and last row.
You can check the MSDN - "Empty Cells" to get more details.

Removing a row in a table if it doesn't contain keyword

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

Excel VBA - selecting the range from first row to the last row

I have a problem with VBA code. I have a sheet with a lot of data around the cells I want. I need to select data in column F, but the position of the first and last cells between the range is to be selected is changing up and down. I created the non empty cells in row X where there is no data do the LastRow function has any refernece but now I dont know how to select the first row
thx for help
If F1 is empty, this selects the first cell with data in the F column
Worksheets("Sheet1").Range("F1").End(xlDown).Select
If F1 may be non-empty you can add a check to see whether it contains text.

VBA locate the first empty cell that occurs in a row by moving left/right

I have a Row in Excel and many cells of that row are populated by numbers, dates, etc.
Let us say this is Row 1. However once we make a couple of moves in Row 1 to the left we find 3 or 4 empty cells and then continuing our move to the left on Row 1 the cells are populated again by various values.
How can i go to the first cell which is blank and empty that occurs, when we move to the left in Row 1 every time i fire up a macro?
.____.____.___.___.___.____.____.____.____.____.
|_d__|_d__|___|___|___|__x_|_d__|_d__|_d__|_O__|
To put this schematicall if we are on O how can we find/go to x?
(Lets assume the x is the first blank cell)
How can i go here
This should move the selection there.
Selection.End(xlToLeft).Offset(0,-1).Select