I have one Chr(10) in the cell
cell.WrapText = False
cell.EntireRow.AutoFit ' AutoFit works
' ------------ but:
cell.WrapText = True
cell.EntireRow.AutoFit ' AutoFit works only if the cell has less then five lines.
If I add some characters (one line more) - AutoFit doesn't work. Text is cutted on the first and last line.
ver - famous excel 2010
It depends on how big the data is and what is the width of the column. Reason being the max height a row can expand is to 409.5 (546 pixels). If you manually increase the height of the row, you will notice that after a particular height, you will not be able to increase the height of the row. Check what is that height :) Same is the concept with the width. This is applicable to both rows and columns. In case of columns the max width is 254.86 (1789 pixels)
I used this code for demonstration purpose.
Option Explicit
Sub Sample()
ActiveCell.WrapText = True
ActiveCell.EntireRow.AutoFit
MsgBox ActiveCell.RowHeight
End Sub
See this screenshot
This works
This doesn't
The row has reach it's maximum. It cannot go beyond that.
The best way to handle this is to increase the width of the column.
FOLLOWUP
The reason why it was not working is because you had multiple columns with data in that row and you were trying to just set the WrapText of 1 cell and hence it was not working. When you have multiple columns of data you have to use cell.EntireRow.WrapText = True instead of ActiveCell.WrapText = True
A related solution in case it helps someone. I have data with 'Wrap Text' on and line breaks in cells that come from copying data from formulas that used 'Char(10)'. I need the line breaks.
For example, data in a cell should show like this:
City: New York (1950)
Country: USA
But it showed something like this:
City: New
York (1950)
Country:
USA
Selecting the column and clicking to autofit resulted in slight improvement:
City: New York
(1950)
Country: USA
Repeating manual auto-fit a 2nd time brings text to desired format.
If there is more space-separated text in a line, it takes that many more auto-fit attempts, and eventually format is fully corrected.
So in my VBA code, I replicated this and repeated auto-fit code the number of times I knew would fix my issue:
'Adjust column Width
wsSched2.Columns("F:CZ").EntireColumn.AutoFit
DoEvents
wsSched2.Columns("F:CZ").EntireColumn.AutoFit
DoEvents
wsSched2.Columns("F:CZ").EntireColumn.AutoFit
(It may work without 'DoEvents'. I haven't tried)
Related
I wrote a macro which copies table from excel to word. In excel all columns have different width. In Word I want to fit my table to one page - margins in Word are 1.5 cm from left and right. Number of rows in my table changes, number of columns is stable (this is 14). How can I set column width to be equal? I wonder if it's possible to set the same column width regardless of the amount of text in headlines. I create swdth variable which I then divide by 14 (all my columns) and I have my table on one page...
This code doesn't work properly. I have all rows in one page, but columns have different width.
Table.Rows.SetHeight RowHeight:=InchesToPoints(0.17), HeightRule:=wdRowHeightExactly
Table.Rows(1).SetHeight RowHeight:=InchesToPoints(0.59), HeightRule:=wdRowHeightExactly
sWdth = InchesToPoints(6.22)
WordTable.PreferredWidthType = wdPreferredWidthPoints
WordTable.PreferredWidth = sWdth
sWdth = sWdth / 14
Giving all columns the same with is as simple as:
Table.Columns.DistributeWidth
but it's not apparent what this has to do with keeping the table on one page.
I've seen similar questions recently. As always...
#1) Turn on the Macro Recorder
#2) Click through the steps you need to perform
#3) Turn off the Macro Recorder
Hit Alt+F11 and you should see all the code you need to do whatever you want to do. Remember, the Macro Recorder is your friend!
I want to change the font of all even rows in a large table in Microsoft Word (most versions, I use 2014) to red
I tried a simple loop :
For ii=2 to ActiveDocument.Tables(1).Rows.Count step 2
ActiveDocument.Tables(1).Rows(ii).Select
Selection.Font.ColorIndex = wdRed
Next
This sometimes hangs, sometimes it works, but takes hours (my table has 14000 rows...)
Then I had the idea : Manually, I can select a row by left-clicking on its left, then add additional rows to the selection by Ctrl-left-click on their left.
And I can then modify the font of all rows selected at once.
So let's see if doing the same programmatically is faster ! I tried something like
ActiveDocument.Tables(1).Rows(2).Select
For ii=4 to ActiveDocument.Tables(1).Rows.Count step 2
Selection.Add (ActiveDocument.Tables(1).Rows(ii))
Next
Selection.Font.ColorIndex = wdRed
but Add is not accepted as a valid Selection object member
Can someone help there ?
define a new style and apply it to the table ... no vba needed
this is a macro recording of an example style change ... if you wish to use vba
Selection.Tables(1).Style = "Grid Table 5 Dark - Accent 2"
also, record a macro of doing a new style definition .... lots of good stuff in it
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 am trying to figure out why a row keeps on blowing up during printing. Here is an example: http://imgur.com/OQD9VEh #2 is where it is getting extended. As you can see it happens when the data continues on to the next page. It is because of the columns on the right where the values are "Family" and "Mom". If I remove those two columns, the report prints out fine. Once I add them back in, the last row, no matter the row, expands dramatically if it is the leading row on the next page.
Things that I have already tried which have not worked:
- Disabling the option to have the cells increase or decrease in height.
- Tried increasing the height and width of the tablix
- Altered the data type and size to only 6 char for both columns on the left
Any ideas?
Add a column to the right of your outermost column and set its width to very small with no borders and background color(invisible). This may do the trick.
have you try to have the same row height same as the above.
try to set the row "Can Shrink" to false and "Can Grow" to false
I am writting to an excel file from my vb code. The code goes as below
xlsheet3 = xlBook.Sheets.Add(After:=xlSheet)
With xlsheet3
.Columns(5).NumberFormat = "#"
.Cells(j + 1, 5) = someStringValue 'Here "j" is a row counter and this line is in a "for loop"
end with
After writing to excel, most of the cells in excel are correct. But some of the cell's text comes as ####### however if I click on the cell, formula bar shows the correct result. I have tried giving single code before adding the text still that did not help.
Please help me in resolving this.
Thank you
There is not any issue with your code. You need to increase the width of the column or have to use word wrap. In excel if your value is not fully visible it shows it is "######".
If widening and wrapping text doesn't work and the format is set to text which allows display of only 255 characters, try changing the format to general.
This just indicates that the cell is too small for showing the result: make it wider.
See https://superuser.com/questions/65556/excel-displays-for-long-text-whats-wrong for some common reasons why Excel displays "######" in cells.
Either the cell is too narrow to display the contents or the contents are over 256 characters.
Check what you're writing to the cell. If it's not too long then all you need to do is resize the column to fit the new contents.
This is simply what Excel does when the data in a column is too wide to be displayed in the current column width. Make the column slightly wider and you will see all your data.
To autosize the column so it is wide enough to display all its data, double click the column divider at the right edge of the column, in the header bar.