VBA: Change Excel cell width - excel

I have some VBA and batch Scripts that read the Mac Address Table out of some switches and import it into Excel to format it.
But the text is too long for the default cell width.
Is it possible to change the displayed cell width?
(When saying displayed cell width I mean: this)

Use this:
Range("A1").ColumnWidth = ...
The units for this value are as following:
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.
For example, the column width for freshly opened Excel file with default styles is 8.43 which is equal to 64 pixels.
...or this to autofit width:
Range("A1").EntireColumn.AutoFit

Another method would be:
.Columns("A").ColumnWidth = 20 (or whatever value you need)
I didn't compare it speedwise but why my guess would be that it's more efficient to just use Columns() instead of Range().
For more info on the ColumnWidth-Value -> MSDN Doc for the columnwidth-property

Related

Excel Column Widths from table

From a list of columns incl Header names with defined widths in a separate sheet, I want to be able to reset column widths in a named sheet to follow the widths defined in the table, e.g.
Column A - Firstname width 32
Column B - Lastname width 40
Column C - Telephone width 6
Column D - Email width 36 etc
The same could be done to define row heights defined in a similar manner.
Thanks for your help
Your question is too imprecise to permit a targeted answer. A variant of the code below may present a generic solution.
Worksheets("Target").Columns(12).ColumnWidth = Worksheets("Source").Columns(3).ColumnWidth
This code has two parts. On the right, the column width is read. On the left it is set. You can do either with any column that you can specify, including the column of a named range such as you have in a table. You can assign the read value to a variable and apply the width stored in a variable to any column you can specify, including columns of named ranges like in a table or ranges you specify in your code on the fly. Note that the column widths is a numeric value of Single data type.
Note that the PasteSpecial method offers an option to copy the column width, too. And, of course, you have the command AutoFit at your disposal to adjust a column's width to whatever is needed by its contents.
This is the code for what I understood you wanted.
Sub test()
Worksheets("test").Columns("D").ColumnWidth = Worksheets("Values").Range("D4").Value
Worksheets("test").Columns("E").ColumnWidth = Worksheets("Values").Range("D5").Value
Worksheets("test").Columns("F").ColumnWidth = Worksheets("Values").Range("D6").Value
Worksheets("test").Range("D7").Value = Worksheets("Values").Range("C4").Value
Worksheets("test").Range("E7").Value = Worksheets("Values").Range("C5").Value
Worksheets("test").Range("F7").Value = Worksheets("Values").Range("C6").Value
End Sub
If you have troubles putting this together with your excel file, I will gladly help you.

Returning Wingdings characters within a text with an Excel formula

I want return appropriated Wingdings character at end of a string, with a formula as shown below:
enter image description here
Which formula can I write to return the shown Wingdings characters at the end of the regular string in a cell? (Instead of "?" character): Solid circle character for return in true situation of formula and hollow circle character return in false situation, so these two characters are in green range.
A formula alone cannot change the font of part of a cell.
The best you could do is have a VBA function to make the change as needed, which you could run manually, or perhaps assign to a button, timer, event, etc.
This sub changes the last character of the currently active cell to Wingdings:
Sub LastLettWingding_Active()
'changes the font for the last character of the **active** cell
ActiveCell.Characters(Start:=Len(ActiveCell), Length:=1).Font.Name = "Wingdings"
End Sub
...and this one does the same thing, but to the currently selected cell or range of cells:
Sub LastLettWingding_Selection()
'changes the font for the last character of the all selected cells
Dim c As Range
For Each c In Application.Selection
c.Characters(Start:=Len(c), Length:=1).Font.Name = "Wingdings"
Next c
End Sub
Edit:
I didn't realize it's a formula result that you're trying to format part of.
Unfortunately, Neither a formula nor VBA can change the font of part of a formula result.
My only suggestion would be to automate a Copy + Paste Values to a different cell, change the partial-formatting of that cell with the function above, and perhaps "hide" the original (formula) cell.
Edit #2:
Oh yeah-- #TomSharpe's also made the think that Unicode symbols might be an easier solution (included in most fonts, instead of using a separate font without the alphabet).
✂ ✃ ✄ ✅ ✆ ✇ ✈ ✉ ✊ ✋ ✌ ✍ ✎ ✏ ✐ ✲ ✳ ✴ ✵ ✶ ✷ ✸ ✹ ✺ ✻ ✼ ✽ ✾ ✿ ❀ ❁ ❂ ❃ ❄ ❅ ❆ ❇ ❈ ❉ ❊ ❋ ❌ ➮ ➯ ➰ ➱ ➲ ➳ ➴ ➵ ➶ ➷ ➸ ⌚ ⌛ ❶ ❷ ❸ ❹ ❺ ● ◌ ○ ◊ ◦ □ ┘ ⌂ ⅕⃝ Ω ℗ Ө Ӂ Φ ʘ ʘ ① ② ③ ④ ⑤ etc.
Click an empty cell and then on the Insert tab, click Symbol. Make sure you're on a "regular" font (like Calibri) and scroll way down the list.
Double click the one(s) you want, then Insert.
Highlight the symbol now showing in the formula bar, copy from there, and now you can paste it into your IF formula.
(Or, just copy copy the symbols from above right into an Excel formula...)
You can get a fair approximation to it by using char(149) •
/ lower case o in one of the regular fonts
=IF(VALUE(RIGHT(C4,1))>=5,"Selected "&CHAR(149),"Selected o")

Excel 2010: Conditional Format Class Schedule based on Subject name

Excel 2010, creating a class schedule. Colour coded per class name.
I have a table, broken in to 10 min amounts.
7 wide, 30 down.
I want to conditionally format all cells that contain the letters ENG in green, all SCI in yellow & so on.
Currently my table colours end up with the top priority cell, rather than end up with a multi-coloured table.
In conditional formatting:
=$R$12="ENG"
=$R$11:$V$20,$R$25:$V$29,$R$34:$V$38 colour = Green
=$R$17="SCI"
=$R$11:$V$20,$R$25:$V$29,$R$34:$V$38 colour = Purple
I either get 'green' or 'purple', I don't get a table with both colours in their respective cells.
R12 = Green
R17 = Purple
U12 = Cyan (eventually) etc etc
I know I'm missing something, I don't know what though.
Thank you
Use relative references in your conditional formulas:
If the highlighted cell in your currently selected range is R12, edit the formula (with mouse and Backspace/Delete buttons, do not press arrow keys when editing the formula) to:
=R12="ENG"
...
(see also Excel vocabulary to find solutions faster for a little more background)
Also you don't have to use a formula for this use case, just choose an option Format only cells that contain > change bettween drop box to equal to > write ENG to the text box.

Create progress line using VBA excel

I try to create progress line by using VBA Excel command
I have all of data to define what progress is but
I don't know how to use VBA for creating progress line
for an example I tried to use connector line but It's seem not work
ActiveSheet.Shapes.AddConnector(msoConnectorStraight, 60, 405,800, 477).Select
Range("Z20").Select
ActiveSheet.Shapes.Range(Array("Straight Connector 11671")).Select
I would not use a shape for a progress bar, instead use the data bars conditional formatting on a cell where the cell value is updated by the code
Create a named range on one cell on the worksheet as, for example, "ProgressBar"
Adjust the width to suit your needs
Format the cell as a Percentage
Choose Conditional Formatting -> New Rule -> Format all cells based on their values
In the "Format Style" drop down, choose "Data Bar"
Select the "Show Bar Only" checkbox
In the "Type" drop downs, for Minimum and Maximum, choose "Number"
In the "Value" text boxes, enter 0 for Minimum, 1 for Maximum
Set the formatting as you wish
In the VB Editor, enter the code below
Sub UpdateProgressBar()
Dim Rows As Integer
Dim RowItem As Integer
Rows = 1000
For RowItem = 0 To Rows
Sheet1.Range("ProgressBar").Value = RowItem / Rows
Next RowItem
MsgBox "Done!"
Sheet1.Range("ProgressBar").Value = 0
End Sub
This will then update the value in the cell and the data bar will grow as the value increases.
You will need to adapt to your own code, but this should be a starting point.
some ways of doing a progress bar:
use the application.statusbar
or, use a userform, in wich you change the width property of any rectangle type (i use a button with nice picture font)
use a shape, from wich you change the size by altering its width property.
the hard part is more making it fast enough so it doesn't affect performance, and sometimes
when adding application.screenupdating=false in conjonction to doevents, it might blink... (or not)
if you need some code i can provide you a sample, and you can change it according to your needs

Writing Dates Using POI Tutorial

I was going through the following tutorial to write dates using poi API in an excel sheet
http://poi.apache.org/spreadsheet/quick-guide.html#CreateDateCells
but when I tried to run the code the date filed shows "###" and not the actual date!!
Here is the code snippet
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
Cell cell= row.createCell(4);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
Excel displays a cell full of "#" signs (e.g. "########") when a cell is formatted in such a way that the resulting text is too long to display all of it in the cell. When you open the spreadsheet in Excel, widen the column long enough and it will display the formatted date.
To get the spreadsheet to be created by POI in such a way that the column will be wide enough from the start, then you need to set the column width yourself.
sheet.setColumnWidth(columnIndex, width)
Multiply your width by 256 before passing it in, because the width units used by this method are 1/256ths of a character. Here's the Javadoc: Sheet

Resources