Changing number formatting in excel 2007 - excel

I have an Excel file that looks like this. How would I change the cell formatting so that it looks exactly like it does it the function bar. Instead of 3E-07 --> 0.000003
I don't want to change the decimal places because I only want it to have the number of decimal places it needs so there won't be extra zeroes at the end.
For example because the smallest numbers have 7 decimal places if I change the whole column to seven decimal places numbers like 6E-05 will have two extra zeroes (0.0000600) at the end.
I'm trying to do this on macro because there are a lot of numbers to go through. I can set up the loop myself.

To fix on all columns, follow below steps:-
Click on the symbol mentioned side of A column (That is left side of A)
When you click on that symbol, it will select whole sheet
Double Click on border line between A column & B column
This is fix all your problems on the excel sheet
This number format issue is occurred due to column size.
To fix this type of number format "0.0000600":-
select the Column header (that is A column, B column .. and soon which ever column you need to format)
Right click on the tab
Select "Format Cell"
Click on custom option under Category
under Type field, enter 0.0000000000
Then it fix your format issue.

You have tagged this question with VBA so here are two examples of VBA's .AutoFit method.
For the entire worksheet:
With ActiveSheet 'set this worksheet reference more defintively!
.Cells.EntireColumn.AutoFit
End With
If you have columns that are already the width you want you could test the first 20 rows to see if the .TEXT property is displaying the hash mark (e.g. # or Chr(35)) symbol.
With ActiveSheet 'set this worksheet reference properly!
With .Cells(1, 1).CurrentRegion
For c = 1 To .Columns.Count
For r = 1 To 20 'maybe even .Rows.Count for smaller data grids
If CBool(InStr(1, .Cells(r, c).Text, Chr(35), vbBinaryCompare)) Then
.Columns(c).EntireColumn.AutoFit
Exit For
End If
Next r
Next c
End With
End With
The latter defined the range of cells to work on using the .CurrentRegion property which expands from its origin until it meets a fully blank row and column.

Related

Ignoring specific rows with VBA Excel

I have a table in Excel like such, where the number of rows will vary each day:
Column A
Column B
Column C
Cell 1
Cell 2
Show
Cell 3
Cell 4
Show
Cell 5
Cell 6
Ignore
I am using vba to convert the range to a html table, and then email it.
I have a helper column (Column C), and I want to use a formula there to filter out certain rows.
However, that filter is not excluding hidden cells from being displayed in the html table.
I currently use this
Dim LastRow As Long LastRow = rInput.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
to find the last row of my table. This works great in projects where you want all of the table included.
I tried to change it to Find("Ignore", which gets me Object variable or With block variable not set
I tried including 'SpecialCells(xlCellTypeVisible)' in my
ConvertRangeToHTMLTable(Sheet2.Range("$A:$J").Rows("5:" & LastRow), 5)
and using a filter to hide the 'Ignore' cells. But that did not stop them showing in the emailed html table.
You probably have some sort of loop which goes over the rows right? It will not automatically skip the hidden rows just because they are filtered out, you need to specifically tell it to skip them. You can do something like:
For Each r In myRange.Rows
If Not r.EntireRow.Hidden Then
doSomething
End If
Next r
Ended up adjusting the table (and thus the range I cared about) to start at row 1 rather than row 5, and using
strBody = dsaEmailHeader & ConvertRangeToHTMLTable(Sheet2.Range("$A:$H").Rows("1:" & LastRow).SpecialCells(xlCellTypeVisible))
worked, where it didn't previously.

Excel If one cell contains a number then highlight that number of cells in a row horizontally

I'm looking for a way by VBA or whichever works, even 1 command button but 4-5 processes to be called
Call process1
Call process2
End Sub
on one click which somehow seems impossible for me as I'm not that advanced in Excel VBA, but hopefully this challenge is a piece of cake for others. The task is to highlight cells horizontally by range based on the formula referring to a cell in Column AM. So "B8" is an amount of 100 which needs to be divided by the number of partitions appearing on "AM3". So 100/6. Now "AM3" is 6 so starting from "C8" a number of 6 cells (Merged in 4's) will be highlighted horizontally. "C9" is relating to "AM4" which is having a value of 9 and will highlight 9 cells (Total of 36 cells since merged) horizontally .
For now this is what i have applied but it limits to only within that range :
Sub HighlightRangeOfCells()
Dim rng As Range
For Each rng In Range("C8:AL12")
If IsNumeric(rng.Value) Then
If rng.Value <> 0 Then
rng.Interior.Color = vbRed
End If
End If
Next rng
End Sub
Thanks for your efforts and reply whatever it may be if possible or not.
As mentioned by SSlinky, the best way to do this is using conditional formatting. I have created a simple Excel sheet for explaining you how to do such a thing: I have put the formula =COLUMN()<=$J1 in cell "A1", I have used this formula for conditional formatting, I have dragged to the right, and then I have dragged down.
This is what it looks like:
As you see, cells get highlighted when their column number in some way corresponds with the value of column "J". All you need to do is replace "J" by "AM" and describe the correspondence as you see fit.
For your information: in the formula, I'm using the reference $J1, which means that while dragging and dropping, the row number might change, but the referred column always needs to be "J" (it's a combination of absolute and relative cell references).

How can I make a macro to copy an entire column but change a single row number?

I have inherited a poorly designed workbook, and I am trying to make it work a bit better without starting from scratch.
The last problem I have is that I have a formula in a column that I need to copy to the next column, but change the row number referenced in the formula. The easiest thing to do would be to change the format of the workbook but that will cause an uprising by the users.
=IF((CommaSeparatedListContains(RTM!$I$8,ROW()-2))=TRUE,"X","")
The code above is what I need to copy, but I need to change it so that it looks at I9 instead of I8. RTM is the name of the sheet that the cell is on, and CommaSeparatedListContains is a macro that will return true if the referenced cell has a value (ROW()-2) in the comma delimited list.
Basically I need a macro to add a new column to the worksheet that works like the others, so that the end users who don't know how to use Excel can just click a button and add a column.
For example, that code is in cell A1, and I need to move it to B1 keeping the I the same but increment the row number. If I remove Both $ signs it would change it to J8, if I have $I8 it stays I8, and if I have $I$8 it stays I8.
Thanks for re-affirming my understanding. I'll give this a shot with providing some code, based on your string:
Dim LC as Long, i as Integer
Columns(9).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Cells(1,10).Value="" 'Add your header
LC = Cells(1, Columns.Count).End(xlToLeft).Column
i = LC-1 'See description below code
Range("J2:J100").Formula="=IF((CommaSeparatedListContains(RTM!$I$" & i & ",ROW()-2))=TRUE,""X"","""")"
For i, you want to ensure that you substract the correct number... given the example of I8 being the cell you want to reference, and assuming that Column I is the last column of your sheet, then the 9th column, 8th row, is the cell you want to reference. So, the variable i = last column - 1, in this example.
In this case, the column is always added to the right of column I, the assumed last column in the sheet.
One other assumption is that you're using rows 2 to 100 for the range that you have the formula... So, Range("J2:J100").Formula will be affected by your actual range for the formula.

Autofit Method of Range Class Failed (Run Time Error 1004)

This is just part of my code. the value from the textbox here already gets copied to the specific cell in the Bank Certification worksheet. I need to make sure that cell C5 is specifically fitted regardless of the length of the text i inputted in the textbox. I tried interchanging range with cells to no avail. This problem seems so simple but I don't know why it doesn't work...
Dim counterparty As String
counterparty = Sheet1.txt1.Text
Range("C5").Value = counterparty
Sheets("Bank Certification").Select
Range("C5").Select
Selection.AutoFit
Try
Dim counterparty As String
counterparty = Sheet1.txt1.Text
Range("C5").Value = counterparty
Sheets("Bank Certification").Select
Columns("C:C").Autofit
Other answers correctly state that AutoFit must be used with a column, not just a cell. However, there are some nuances to using AutoFit that I didn't understand until I started experimenting.
Either of the first two statements below will use all values in column C to AutoFit the width of the column. That means if there is a value in some other cell in column C (for example C10) that is wider than the value in C5, it will fit the column to the widest cell in column C (for example C10).
Range("C5").EntireColumn.AutoFit ' Will fit to widest cell in column
Range("C:C").AutoFit ' Will fit to widest cell in column
If you want to just fit the column on 1 cell (or a certain range of cells, but not the whole column or columns), use a statement like this:
Range("C5").Columns.AutoFit ' Will fit column C to width of cell C5
And of course, it's always better form to write code like this when you can:
Fully qualify the range unless you're absolutely sure you'll only be working with one worksheet
Use Named Ranges or Range objects.
For example:
Workbooks("MyWorkbook.xlsm").Sheets("Sheet1").Range("MyData").Columns.AutoFit
' or
Set AutoFitRange = Workbooks("MyWorkbook.xlsm").Sheets("Sheet1").Range("C5")
AutoFitRange.Columns.AutoFit

formatting excel cell

I am in trouble. I have thousands of customer data having name, address, zip. I have data in excel and the problem is in zip code. The Zip should be 5 character long and if it is not, we have to add zero at front to make equal to five character. Since there are thousands of data and it is not feasible to change it one by one, can somebody suggest me to format the column of zip so that it could be 5 character long.
You can use the custom format and use the type : 00000
Write the following formula in front of that zip column:
=IF(LEN(A1)>5,"Invalid",RIGHT("00000" & A1,5))
Just replace the cell reference name A1 to the first cell of zip code column and then drag the cell down. Now all those zip codes that are less than five characters will be filled with leading zeros. Also It will show Invalid in cells which have a zip code of more than 5 characters length.
Apply this formula to the cell values, i.e. for A1 in this example:
=TEXT(A1,"0000#")
Copy/paste the formatted values (as text) into the desired column if you need them in a specific column.
Simply mark the whole column.
Than right click to get into the "format cells" settings.
There you go to "special format" and there you should mark "ZipCode".
Here is a picture of the options. It is in german, so I hope you will find it anyway ;)
Quick macro to convert the range to Text format, and append leading zeros if the cell is less than 5 characters.
Sub AddLeadingZeros()
Dim rng As Range: Set rng = Range("A1:A10") '<modify as needed, the cells containing ZIP'
Dim cl As Range
rng.NumberFormat = "#"
For Each cl In rng
If Len(cl.Value) < 5 Then
Do
cl.Value = "0" & cl.Value
Loop While Len(cl.Value) < 5
End If
Next
End Sub
select your column
Format-> cell
Special format-> zip code
make sure you save the changes... it shouldn't remove zero...
or try a "user defined" format... but I never tried it.

Resources