Written a VBA script which outputs numbers and I thought I had the correct format string for thousand-separating (4,656,565 5,343 232,434 etc) but its not working for certain magnitudes of numbers.
So far I am using Cells(x,y).NumberFormat = "#,###"
can someone provide me with the correct format string to thousand-comma-separate any number, no matter the magnitude?
This works for me. Notice, formatting the cell first before assigning the number to it
Option Explicit
Sub Sample()
With Cells(1, 1)
.NumberFormat = "#,##0"
.Value = 4.65656553432324E+16 '46565655343232400
End With
End Sub
RESULT
Cell A1 has 46,565,655,343,232,400
Related
I notice that numeric values like 123456 can be considered as numbers or non-numbers in Excel. Mixing numbers and non-numbers may result in unexpected results of = or XLOOKUP.
For instance, in the following worksheet, the formula of D3 is =ISNUMBER(C3) and the formula of D4 is =ISNUMBER(C4). Their values are not the same. Then =C3=C4 in another cell will return FALSE; =XLOOKUP(C3,C4,C4) will return #N/A.
So one solution to avoid such surprises is that I would like to convert all these numeric values from numbers to non-numbers, before applying formulas on them.
Does anyone know if it is possible to undertake this conversion by manual operations (select the range, then...)?
Does anyone know how to achieve this conversion by a subroutine in VBA (select the range, then run the VBA subroutine, then the selected range will be converted)?
If you firstly write numbers in a range, let us say "C:C", formatted as General, any such a cell will return TRUE when you try =ISNUMBER(C4).
If you preliminary format the range as Text and after that write a number, this will be seen by Excel as a String (non-numbers, as you say...) and =ISNUMBER(C4) will return False.
Now, if you will try formatting the range as Text after writing the numbers these cells will not be changed in a way to make =ISNUMBER(C4) returning FALSE. In order to do that, you can use TextToColumns, as in the next example:
Private Sub testTextToCol()
Dim sh As Worksheet, rng As Range
Set sh = ActiveSheet
Set rng = sh.Range("C:C")
rng.TextToColumns Destination:=rng, FieldInfo:=Array(1, 2)
End Sub
It will make the existing =ISNUMBER(C4), initially returning TRUE, to return FALSE...
Of course you cannot compare apples to oranges, thus strings are not comparable to integers/longs/numbers. Make sure that all you compare are apples.
In a routine this would be s.th. like
Option Explicit
Sub changeFormat():
' Declare variables
Dim Number As Variant
Dim check As Boolean
'Converts the format of cells D3 and D4 to "Text"
Range("D3:D4").NumberFormat = "#"
'Assign cell to be evaluated
Number = Range("D3")
Debug.Print Number 'Prints '123'
check = WorksheetFunction.IsText(Trim(Sheets("Tabelle1").Cells(4, 3)))
Debug.Print check 'Prints True
'Converts the format of cells D3 and D4 to "Numbers"
Range("D3:D4").NumberFormat = "0.00"
'Compare Cells
If Range("D3").NumberFormat = Range("D4").NumberFormat Then Range("D5").Value = "Same Format"
End Sub
Also see the docs
I need to round off the range("M2:M13") value to two decimal points.I wrote the below code using "NumberFormat" option but when I copy paste this value to another workbook it displays me the whole number in formula bar and the rounded off number in the selected cell.
How do I completely round off the numbers to two decimal points?
Sub RoundOff()
Worksheets("Sheets2").Activate
Range("M2").Select
Selection.NumberFormat="0.00"
ActiveCell.FormulaR1C1="RC[-3]/RC[-4]*100"
Range("M2:M13").Select
Selection.FillDown
End Sub
#Nilusha M. It s missing one "=" before "RC[-3]/" from your formula.
You can try:
Sub Test()
With ThisWorkbook.Worksheets("Sheet2")
.Range("M2").FormulaR1C1 = "=RC[-3]/RC[-4]*100"
With .Range("M2:M13")
.FillDown
.NumberFormat = "0.00"
End With
End With
End Sub
You can do by simply doing
range("A1").EntireColumn.NumberFormat = "#.00"
It will format the entire A column
I need to keep the cells formatted as text and at the same time make sure that Excel calculates the formulas within them.
Is there a way to do it?
Embed your excel-formulas in the text-function.
=Text(your function,"#")
more info:
https://support.office.com/en-us/article/text-function-20d5ac4d-7b94-49fd-bb38-93d29371225c
edit: If your formulas are not being evaluated, then there can also be other causes for that than the cell-formatting.
I'm solved it!!!
In a Sheet module:
Option Explicit
Private Busy As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Busy Then
Busy = True
If ActiveSheet.ProtectContents Then ActiveSheet.Protect UserInterfaceOnly:=True
CalculateFormula Target.Row, Target.Column
Busy = False
End If
End Sub
In a standard module:
Option Explicit
Sub CalculateFormula(Row As Long, Column As Long)
Dim Format As String
If Left(Cells(Row, Column).Value, 1) = "=" Then
Format = Cells(Row, Column).NumberFormat
Cells(Row, Column).NumberFormat = "General"
On Error Resume Next
Cells(Row, Column).FormulaLocal = Cells(Row, Column).Value
On Error GoTo 0
Cells(Row, Column).NumberFormat = Format
End If
End Sub
It is unclear if you just need the format of the cell to be text, or if your need the results of a formula in a cell to be converted to a string/text. Excel formulas do no affect cell formatting, nor or are they affected by cell formatting. a perfect example of this is dates. Dates are really an integer counting the number of days since a start point. Day 1 is 1900/01/01 on a pc (On a mac I think its 1905/01/01 but could be wrong). If you enter today's date (2018/09/17) in any default cell, Excel identifies it as a date, changes the formatting of the cell and converts the date to 43360. You will see this if you change the cell format back to general and you will note that the number is right aligned. If you change the format of the cell to text, you will still see 43360 but instead it will be left aligned. More importantly its still a number and you can test it by using ISTEXT(A1) where A1 is the cell in question. It will still return true.
In order to make the contents of a cell a string, you can simply concatenate your formula with "".
=Your_Function&""
=1+1&""
Having said that. If a cell is formatted as a string, the FORMAT of the cell will remain a string despite whatever math calculation goes on inside it. So if you format A2 as text, and then place =1+1 inside it, the result in the cell will be the number 2 and the cell will be formatted as text.
I have a simple request, to paste the data exactly as visible in Excel.
I have a list of dates in mm/yyyy format, but Excel keeps adding mm/dd/yyyy which is throwing off my analysis. It's formatted to show simply mm/yyyy but the actual cell value keeps getting set to mm/01/yyyy.
How can I simply copy/paste the value to be mm/yyyy.
I've tried Range("A1").Value = Range("A1").Value, but of course that just keeps the same info.
Yes, in my case since it's dates, I can do a kludgy function that takes the left three characters, and combines with the rightmost four. However, that really just gets the date number returned. I tried on G4 and get 4171730. Plus, I'd like to know how to do this with other types of cell values too (strings, numbers, etc.).
save the value and the format then set the cell as text and assign the formatted value:
Sub test()
Dim t As Variant
t = Range("A1").Value2
Dim x As String
x = Range("A1").NumberFormat
Range("A1").NumberFormat = "#"
Range("A1").Value = Format(t, x)
End Sub
This also works
Sub test()
Dim t As String
t = Range("A1").Text
Range("A1").NumberFormat = "#"
Range("A1").Value = t
End Sub
Range("A1").Value = Format(yourdate, "mm/yyyy")
I have an excel user form into which the user enters numbers, when those numbers are entered into the spreadsheet they appear with the notification that this is a number stored as text. =SUM(H6:H13) shows a zero result.
I have tried NumCrtn = cLng(NumCrtn) - doesn't change the cell to a number, formula still shows zero.
I have tried NumCrtn = Val(NumCrtn) - doesn't change the cell to a number, formula still shows zero.
I have tried copy and paste.special to a value and that doesn't change it to a number either.
Don't know what to do.
Help!
Try this one:
With Range("H6:H13")
.NumberFormat = "0"
.Value = .Value
End With
Edit:
Another solution. Building on Pradeep Kumar's suggestion which deals with preparing your range before you enter the data, Change your code to something like this
Private Sub UserForm_Initialize()
Dim aCell As Range
Range("H6:H13").NumberFormat = "0"
'This is to cater for any previous values if filled in
For Each aCell In Range("H6:H13")
aCell.Formula = aCell.Value
Next
End Sub
Private Sub CommandButton1_Click()
'Entering value for H6
Range("H6").Value = TextBox1.Value
End Sub
Range("H6:H13").NumberFormat = "#,##0"
It is not a VBA solution, but an ordinary Excel solution.
Do like this
Select the column
Select Data - Text to columns
Quite often the default settings will do and you can click Finish. Otherwise you
will have to make sure that the result is just a "General" column
A macro doing just this would look something like this:
Columns("A:A").TextToColumns
There's a lot of parameters to the TextToColumns method, but it should work fine with default values only (i.e. no parameters).