How to make a table fit the window height in Word? - vba

I am New to VBA. I have a table with 4 columns and almost 2000 rows and it is displayed in a 3 column Word layout. My problem is that the last rows of the 3 columns are not always on one height as shown in the image (Masked the personal data with the white font in cells). [Image of the Table1
I think a solution would be to set the height of the rows with 2-lines to 0.54cm and the 1-line rows to 0.27cm. I did this manually and it worked. I am looking for a macro to achieve this. The below code will provide an understanding of what I am trying to do.
Sub height ()
ActiveDocument.Tables(1).Rows.HeightRule = wdRowHeightAuto
'this automatically sets the 2-line rows to 0.53cm and the 1-line rows to 0.25cm
For Each row in Rows
If Row.RowHeight < 0.5cm Then
Row.RowHeight = 0.27cm
Else Row.RowHeight = 0.54cm
End If
Next Row
End Sub
I know that this code cant work but i think it shows you what i want to do.

As Row.RowHeight won't return a useful value unless the row has been set to an exact height you can change the line spacing for the text so that the required cell heights are achieved automatically.
The code below assumes that the table you want to adjust is the first one in the document.
Sub AdjustLineSpacingForTable()
Dim reqdLineSpacing As Single
'start with the height of a double height cell
reqdLineSpacing = CentimetersToPoints(0.54)
With ActiveDocument.Tables(1)
'table cells have padding top and bottom so we need to subtract that from the required height
reqdLineSpacing = reqdLineSpacing - (.TopPadding + .BottomPadding)
'now we can adjust the text spacing
With .Range.ParagraphFormat
'ensure there is no additional spacing on the paragraphs
.SpaceAfter = 0
.SpaceBefore = 0
'set line spacing to an exact amount
.LineSpacingRule = wdLineSpaceExactly
.LineSpacing = reqdLineSpacing / 2
End With
End With
End Sub

Related

Set row height of range to 721px

I have an Excel Sheet in PPT-Look which is filled with data from another sheet dynamically. Row height for rows 1 to 7 and from 30 to 36 are fix (header & footer of the ppt). In range from row 8 to 26, rows are hidden when empty (I entered white "X" to some of the rows to keep them shown).
I need to get a total height of 721px for the rows 2 to 36 to fit onto my PPT-Template when exporting it.
Is there a possibility to dynamically adapt row height of a "white" row (as for example row 29) to get a total of 721px for the defined range (row 2 to 36)?
you could go like follows:
Option Explicit
Sub main()
Dim extraRowHeight As Double, totalRowsHeight As Double
Dim myRowsRange As Range
Const maxRowHeight As Double = 409 '<--| maximum allowed row height
totalRowsHeight = 721# '<--| set the total rows height you need for the range you are to specifiy right below
With Worksheets("MyTableSheet") '<--| change "MyTableSheet" with your actual sheet name
extraRowHeight = totalRowsHeight - .Rows("2:36").Height '<--| calculate the extra height needed for the wanted rows range
With .Rows(29) '<--| consider the "reservoir" row
If .RowHeight + extraRowHeight <= maxRowHeight Then '<--| if needed extra height leads to an allowable resulting "reservoir" row height ...
.RowHeight = .RowHeight + extraRowHeight '<--| ... then adjust "reservoir" row height
Else
.RowHeight = maxRowHeight '<--| ... otherwise adjust "reservoir" row to maximum allowable height ...
MsgBox "you still need to resize some other rows height for " & totalRowsHeight - .Rows("2:36").Height & " pts more" '<--|... and inform how much there still is to go
End If
End With
End With
End Sub

Compares two column based on the value of a third column's value

What I want to do is create a macro to look at a column (AF) and based on that value, compare column (BI), (BJ), and/or (BK) together and if its false, highlight the compared cells in yellow. I know that's a little hard to follow but this example should help clarify:
My Sheet has the following columns:
Column AF Column BI Column BJ Column BK
PRODUCT Height Length Width
I need a macro to look at the product type and compare the dimensions for that product as follows:
- If product = A, then Length = Width, if not then highlight Length and Width Cells
- If product = B then Length > Width, if not then highlight Length and Width Cells
- If product = C then Width > Height < Length, if not highlight Length, Width, and Height cells
- If product - D then Width = Length < Height, if not highlight Width, Length, and/or Height
My Data starts on row 3 and ends at row 5002.
I have tried researching this and was only able to find solutions that compare two cells then write a third column. I could combine an IF formula and conditional formatting to achieve this but I don't want to have this run all the time as the sheet will be sorted and color coded. I plan to place this macro into a command button.
Suggest to combine Statements such as Select Case, If...Then...Else, together with Operators And, Or. See the following pages:
https://msdn.microsoft.com/en-us/library/office/gg251599.aspx
https://msdn.microsoft.com/en-us/library/office/gg278665.aspx
https://msdn.microsoft.com/EN-US/library/office/gg251356.aspx
After which you should be able to write something that resembles this:
(Code below is just a sample, it will not work)
Select Case Product
Case A
If Length <> Width Then
Rem Highlight Length And Width Cells
End If
Case B
If Length <= Width Then
Rem Insert here the code to highlight Length And Width Cells
End If
Case C
If Width <= Height And Height >= Length Then
Rem Insert here the code to highlight Length, Width, and Height cells
End If
Case D
If Width <> Length And Length >= Height Then
Rem Insert here the code to highlight Width, Length, and/or Height
End If
End Sub
In case you don’t know to highlight the Width, Length and Height Cells; I suggest to do it manually while recording a macro, this shall give a good starting point.
I suggest to work with objects, defining variables for the Data range, each row being validated, the position of the fields to validate, etc. see below code with comments
Sub Highlight_Cells_based_Comparison()
Dim rData As Range
Dim rRow As Range
Dim rCllsUnion As Range
Rem Set variables to hold Fields position within the DATA range
Dim bPosProd As Byte, bPosHght As Byte, bPosLeng As Byte, bPosWdth As Byte
Rem Set variables to hold Fields values
Rem (data type Variant as don't know type of values these fields are holding, change as appropriated)
Rem see https://msdn.microsoft.com/en-us/library/office/gg251528.aspx)
Dim sProd As String, vHght As Variant, vLeng As Variant, vWdth As Variant
Dim lRow As Long
Rem Set Range (assuming it goes from column C to BK - change as needed)
Rem Not starting from column A on porpuse
Set rData = ActiveSheet.Range("C3:BK5002")
Rem Get Fields position from Header row
Rem Suggest to use this method instead of hard coding columns
On Error Resume Next
With rData
bPosProd = WorksheetFunction.Match("PRODUCT", .Rows(1), 0)
bPosHght = WorksheetFunction.Match("Height", .Rows(1), 0)
bPosLeng = WorksheetFunction.Match("Length", .Rows(1), 0)
bPosWdth = WorksheetFunction.Match("Width", .Rows(1), 0)
End With
If Err.Number <> 0 Then Exit Sub
On Error GoTo 0
Rem Loop thru each row excluding header
For lRow = 2 To rData.Rows.Count
Set rRow = rData.Rows(lRow)
With rRow
Rem Get Row Field values
sProd = .Cells(bPosProd).Value2
vHght = .Cells(bPosHght).Value2
vLeng = .Cells(bPosLeng).Value2
vWdth = .Cells(bPosWdth).Value2
Select Case sProd
Case A 'Change value of A as required
Rem If product = A, then Length = Width, if not then highlight Length and Width Cells
Rem If Length <> Width Then Highlight Length And Width 'Cells
If vLeng <> vWdth Then
Set rCllsUnion = Union(.Cells(bPosLeng), .Cells(bPosWdth))
Rem Suggest to use a subroutine for this piece as it's a repetitive task
Rem see https://msdn.microsoft.com/en-us/library/office/gg251648.aspx
GoSub CllsUnion_Highlight
End If
Case B
Rem repeat as in Case A with required changes
Case C
'...
Case D
'...
End Select: End With: Next
Exit Sub
Rem Subroutine to highlight cells
CllsUnion_Highlight:
With rCllsUnion.Interior
.Color = 65535
.TintAndShade = 0
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.PatternTintAndShade = 0
End With
Return
End Sub

How to set row height of a datagridview based on its contents in vb.net

I want to adjust row height to display entire contents of a cell(column width is fixed). I have one column and many rows in my datagridview.
I used following codes, but I couldn't get required row height
Me.data.DefaultCellStyle.WrapMode = DataGridViewTriState.True
data.AutoResizeRows(DtaGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders)
Set property AutoSizeColumnMode of datagridview to AllCells and check it.
DataGridView.AutoSizeRowsMode Property
try this
DataGridViewRow row = dataGridView.Rows[0];
row.Height = 15;
DataGridViewRow.Height Property
You can use this instead:
For i=0 to DataGridView1.Rows.Count-1
DataGridView1.Rows(i).Height=50
Next
It will consume less time and coding lines
To change the height of the rows you must to make a change to the specific row (On by On) like this
For i = 0 To DataGridView1.Rows.Count - 1
Dim r As DataGridViewRow = DataGridView1.Rows(i)
r.Height = 50
Next

How to resize row height in C1FlexGrid?

I need to auto resize row height in a C1FlexGrid. I need to make it work using AutoSizeRow but it does not change the row height. I tested it by setting height and it works. Why is AutoSizeRow not working?
For i As Integer = 0 To fgrid.Rows.Count - 1
'Setting the height explicitly changes the row height
fgrid.Rows(i).Height = 32
'But AutoSizeRow() does not change the row height
fgrid.AutoSizeRow(i)
Next i
Please note that the AutoSizeRow method works when there is any data filled in the grid's row. If there isn't any data, AutoSizeRow would simply wont work. The same thing is happening in you snippet. Since there is no data in the row, fgrid.AutoResize(i) is useless. Try replacing your snippet with the following, and you would understand how AutoSizeRow works:
For i As Integer = 0 To fgrid.Rows.Count - 1
'Fill data in the cell
fgrid.Rows(i)(1) = "This is sample text"
'Setting the height explicitly changes the row height
fgrid.Rows(i).Height = 32
'AutoSizeRow() is changing the row height now
fgrid.AutoSizeRow(i)
Next i

Hide a gridview header column without losing position of other header columns vb.net

I would like to hide the header for two columns on a gridview. However I would like the other column headers to remain in their original position not move across to where the hidden columns would have been.
The code I'm using to hide the headers is the following:
For colCount As Integer = 0 To 1
gvProgressGrid.HeaderRow.Cells(colCount).Visible = False
Next
In the below example the headers should start at the column that contains the first set of blue cells.
Instead of toggling visibility, you can just set the column headers to empty strings and remove the border.
For colCount As Integer = 0 To 1
gvProgressGrid.HeaderRow.Cells(colCount).Text = ""
gvProgressGrid.HeaderRow.Cells(colCount).BorderStyle = BorderStyle.None
Next
If you want to retain the column header value, you could also just set the cell's foreground color equal to it's background color.
For colCount As Integer = 0 To 1
gvProgressGrid.HeaderRow.Cells(colCount).ForeColor = gvProgressGrid.HeaderRow.Cells(colCount).BackColor
gvProgressGrid.HeaderRow.Cells(colCount).BorderStyle = BorderStyle.None
Next