I'm getting strange numbers when using the VBA Height function - vba

I am trying to use the Height function in VBA but I am getting strange numbers. Instead of returning the height of a range, it is returning the height of a range multiplied by 15. For example, the following simple function :
Function h(c1 As Range) As Double
h = c1.Height
End Function
would give a value of 15 if c1 was a single cell, would give 30 if c1 was two cells high, and so on. I should clarify that I am using this as part of a larger function, but it is this issue that is causing the problem. I wanted to make a function as simple as possible to see if the problem remained, and it has. I really don't understand why this is happening, I have used the height function before with no problem. Any advice would be greatly appreciated,
Thanks

You are misunderstanding what the height property is. The height is its height in points...
From here..https://msdn.microsoft.com/en-us/library/office/aa215509%28v=office.11%29.aspx?f=255&MSPPError=-2147217396
Returns the height of all the rows in the range specified, measured in points. Returns Null if the rows in the specified range aren't all the same height. Read/write Variant.
Remarks
You can use the Height property to return the total height of a range of cells.
Differences between RowHeight and Height include the following:
Height is read-only.
If you return the RowHeight property of several rows, you will either get the row height of each of the rows (if all the rows are the same height) or Null (if they're different heights). If you return the Height property of several rows, you will get the total height of all the rows.
Example
This example doubles the height of row one on Sheet1.
With Worksheets("Sheet1").Rows(1)
.RowHeight = .RowHeight * 2
End With

Related

Excel cell default measure unit

What is the default measurement unit of Excel cell size? Is it Point or Pixel or Millimeter ?
By default, excel cell Row height is 15, what is the meaning of this value? Is it 15 Pixels or 15 Points
By default, excel cell Column Width is 8.43, what is the meaning of this value? Is it 8.43 Pixels or 8.43 Points
If both row and column units are same, then Row height should be smaller than to column width. But the measurement is reverse, row height shows bigger number than column width. In Cell appearance also, row is small than column width.
I need to create box with Height 90 mm (millimeter) and Width 195 mm (millimeter). Please let me know what are values to be put in Row and Column textboxes.
Thanks in advance.
The default units for column and row are indeed different when accessed through the GUI.
The displayed column width in the GUI refers to the Range.ColumnWidth property, where One unit of column width is equal to the width of one character in the Normal style. For proportional fonts, the width of the character 0 (zero) is used (source). This means as you change the worksheet style, your column width may change too.
The height, however, displays a normal height in points.
In VBA, you can both get both this font-related unit, and the normal point unit for the width. For the height, you can only get the value in points:
Debug.Print Range("A1").ColumnWidth '8.43 characters wide by default
Debug.Print Range("A1").Width '48 points wide by default
Debug.Print Range("A1").Height '12.75 points high by default
Of course, you can calculate a conversion factor between character width and points: Range("A1").Width / Range("A1").ColumnWidth = 5.69 when using Arial, 10 pt. This means that if you want to have a size of 195mm by 90mm, you need to enter 97.0777 as column width, and 255.118 as column height if you're using Arial, 10 pt as normal style.
As per the Microsoft documentation....
You can specify a row height of 0 (zero) to 409. This value represents the height measurement in points (1 point equals approximately 1/72 inch or 0.035 cm). The default row height is 12.75 points (approximately 1/6 inch or 0.4 cm). If a row has a height of 0 (zero), the row is hidden.
Read it more here.
Please throw this link: Microsoft reference
You can change and define this unit manually where pointed in above link:
On the File tab, click Options, click the Advanced category, and under Display, select an option from the Ruler Units list.
So please visit Here Microsoft reference that exactly explain
what you asked.
Above you asked are the pixel width retranslated into character units (based on the Normal font) for display.

Epplus row height issue

I have an excel file with over 1000 rows. Each row contains some data and 2 images.
The images are attached as OfficeOpenXml.Drawing.eEditAs.OneCell
After populating the Excel I run this, to set the row height.
int prodTableStart = 3;
int prodTableEnd = 1025;
while (prodTableStart <= prodTableEnd)
{
ws.Row(prodTableStart).Height = 112d; // works, but mega slow
prodTableStart++;
}
I tried to speed up with something like this: ws.Cells["A" + prodTableStart + ":L" + prodTableEnd].Rows but that returns an int?
So how can I set the row height efficient on a selected range of rows?
When I have so many rows, it even never ends. No exception is thrown. The process just takes for ever.
ps. I am using epplus latest nuget (4.1.0) on .Net 4.6.2 in C#
Setting the row height in EPPlus can be really slow. Instead of updating the row height of multiple rows one by one, you can set the row height for all rows very fast like:
workSheet.DefaultRowHeight = 500;
If you don't want to set all row heights you now can set back the row height to the previous default value of these rows one by one. This solution is faster if the rows the height needs to be updated are more than the rows the height not needs to be updated.
To anyone looking for this 4 years later: for some reason, resizing a row that contains an image slows things significantly. For efficiency, calculate and set the rows heights before adding the images. You will likely need to add the rows with all of their text information, with string.Empty cells where the images will go, and then add them after setting heights for the rows.

Set Excel Column width in pixel via VB.NET

Is there a way to set the columnwidth in an Excel document via VB.NET in pixels?
I am using currently the following codeline:
Sheets(i).Columns("A:A").ColumnWidth = 3.14
Another problem is that I used a macro to record the width of the column and inserted it with the above statement in my code. But when I run the code the column width is 3,42 instead of 3,14.
Can anybody help me?
Based on this post, you can perform a calculation to convert the excel unit of measure to pixels.
Here is a snippet from that web site:
COLUMNWIDTH: One unit of column width is equal to the width of one character
in the Normal style.
range("A1").Columnwidth
returns 8.43 characters
WIDTH: Returns or sets an object's width, in points.
range("A1").width
returns 48 points. 72 point/inch=.6666"=64 pixels # 96 pixels/inch
So...
Sub testwidth()
Sheets("sheet3").Activate
screenres = 96 '96/inch
mypoints = Sheets("sheet3").Range("A1").Width
'> returns 48 points
mychars = Sheets("sheet3").Range("A1").ColumnWidth
'> returns 8.43 chars
mypixels = (mypoints / 72) * screenres 'pixel width of column
Debug.Print mypoints, mychars, mypixels
'> returns 48 8.43 64
End Sub
The column width is 48 points or 8.43 characters or 64 pixels.
Excel doesn't use the concept of pixels when it comes to width of the columns.
One unit of column width is equal to the width of one character in the Normal style as documented on MSDN here: [https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.namedrange.columnwidth?redirectedfrom=MSDN&view=vsto-2017#Microsoft_Office_Tools_Excel_NamedRange_ColumnWidth][1]
Hope this helped.
The other way to solve this issue is to convert pixels to above mentioned character and set it.

UITableView height of a section

I have a UITableView that has 3 sections. My individual cells vary in height (collapse-expand).
I need a way to figure out the height of each individual section, i.e. the sum of cell heights in each section. Preferably without calculating everything the tableView already has done each time I need it.
I there a way to deduce or access such a value?
Did you look at -rectForSection: (and possibly also -rectForFooterInSection:)?
There is no easy way to calculate the value, however there is a way. If your cells have a fixed height it's just the many rows multiplied by their fixed height for each section.
If you have varying height in the cells too, I'd go for an array which holds the total height for each section, just calculate it once and keep it cached there. If the section is collapsed you just need the section header height else it's the value in the array.

Print on a variable height paper using vb.net

I want to print bill on a roll paper using vb.net.
The requirements are as follows:
The width of the page is 300 pixels or 3 inches.
The height of the page is variable, depending on the number of the rows in the datagrid.
The page header will have an image.
The names of the items could be long, so they should not be chopped, rather print on the next line.
How should I go about it?
Get your Image height and xRows (dgv.rows.count) * Rowheight
So you have an Dynamic height.
Additional you could count string lengtht of your long Values and add for longer datarows the addidtional height
But that is just my idea.
Im sure there are better solutions