Excel formulas are not working - not recognizing numbers - excel

I've pasted some numbers on Excel spreadsheet and wanted to do some calculations with it. The problem is that Excel isn't recognizing the numbers. I've already tried several methods to convert them into numbers and none of them works: paste/special multiplying by 1; formating each cell to the number/scientific number format. And there isn't also an error message on the top right corner of each cell like I've read on the internet indicating that there is a number written as text. If I retype each number, Excel recognizes it.
To make sure that the problem was really that the numbers were understood by Excel as text, I tried the functions ISNUMBER(), that returned FALSE and ISTEXT() that returned true.
I want to know how I can fix that problem without having to type into each cell.
Ps. the numbers are in scientific number format, i.e., 1,085859E+001 

Since the column is text the cells are formatted as text.
you use Value to convert the text into a number so the formula will work
A2 = 123
A3 = 123 Richard
Formula
=isnumber(A2) result is false
use
=isnumber(value(A2)) result is True

I was having the same problem, until I realized that the decimal separator was set as (,) instead of (.) in the default settings. Once I changed that, everything worked fine.

If your "numbers" are being detected as text, you can use VALUE() to make sure Excel understands that it is actually a number.
A1: `1.23E+10 (this is a string)
B1: =VALUE(A1)
=12300000000
C1: 1.23E+10 (this is a number)
D1: =IF(B1==C1,"It worked", "Uh Oh")
=It Worked (for me anyway)
I'm not sure what the comma in your scientific number will do so might want to have the function replace them if there not required.

See Kenneth Hobs' answer here: http://www.vbaexpress.com/forum/showthread.php?42119-Solved-Convert-exponential-format-to-a-number
Open your Excel File, Press Alt + f11 to open the VBA screen,
Go to Insert > Module, Copy and Paste Kenneth's code:
Sub Expo()
Dim cell As Range, s() As String, lng As Long, n As Integer
For Each cell In Selection
With cell
If Not VarType(.Value2) = vbString Then GoTo NextCell
s() = Split(cell.Value2, "E")
.Value2 = s(0) * 1 * (1 * 10 ^ s(1)) 'ePart(s(1))
.NumberFormat = "General"
.EntireColumn.AutoFit
End With
NextCell:
Next cell
End Sub
You can now run it as a macro to convert selected cells. Or if you want it as a function copy this code instead:
Function Expo(cell As Range)
Dim s() As String
With cell
If VarType(.Value2) = vbString Then
s() = Split(.Value2, "E")
Expo = s(0) * 1 * (1 * 10 ^ s(1)) 'ePart(s(1))
End If
End With
End Function
This way you can use it as a normal function in excel eg =Expo(A1)
As I mentioned in the comments above though, you will have already lost some degree of accuracy when the original number was converted to scientific notation. The best solution is to get the originating program to write the proper numbers into the text file if you can.

Open a new word document and try Pasting the web content in word first, the copy this content from the word document and paste special in excel, as text. This simple solution worked for me

Open a new blank Excel, then go to Data > From Text, this way you can import text and designate which format you want to convert to. On the Text Import Wizard page, select either Delimited or Fixed width (I am not sure how your original text file look like but generally it should be Delimited. On the next page, pick a Delimiter or enter one in Others. On step 3, you should see the data listed below and the data format on the upper left. Pick General for those columns that you believe should not be Text. This should fix your problem.

My case was stubborn, no response to Paste Special or CLEAN(). Finally resolved by copying the offending column of Excel data and pasting into new Notepad++ doc. This revealed a leading "?" in all the bad numbers (apparently some non-printing character). Used Search > Replace to find all "?" and replace with nothing. Edit > Select All, copy to a new Excel column, and voilà!

There may be hidden characters. Trailing/leading spaces may not visible and hence erroneously be neglected. If there is trailing/leading Space characters with numeric values, excel consider it as text.
Copy contents problematic cells to MS-Word [(Select problematic cells and copy them to MS-Word)] and check any hidden characters, Remove hidden characters with "find"/"replace" functionality.

I was having issues with numbers from PPT (e.g. ($3,000))pasted to excel. Tried multiple different ways to get the text to recognize including find replacing parens, commas, $ signs to blank and trying to format so excel could run formulas. The only option that worked was to paste to Word first then paste value to excel which worked without any additional formatting steps. Surprised I could not do it all within excel though. Maybe there's another way

Select all the cells to convert to a number.
|Data| Menu Tab > Data Tools > [Text to columns]
Delimited. [Next]
Deselect all "Delimiters". [Next]
"Column data format" > General
[Finish]
Verify by using =ISNUMBER(C16) in an spare cell, where C16 is a sample cell. Should now return TRUE.

This happened to me lately. I had forgotten that I had set formula recalculation to manual. The weird thing is that it was returing FALSE when initially created (which was correct) but given the test depended on the value of other cells that, when changed, did not trigger the change in the cell with the isnumber() formula.
Pressing F9 "fixed" my problem (and my ignorance).

Related

How to have excel recognize zero as a string?

I need to put data validation on a range of cells so that you can enter no more and no less than 9 characters in those cells. The problem is that SOMETIMES those 9 characters will be all number ... and that "number string" will start with a zero ... e.g. 012345678. Excel will remove the zero, as it recognizes that string as a number and my validation kicks in saying that I need to enter 9 characters into that field.
Any ideas?
Format the range of cells as Text. This will prevent Excel from trimming leading zeroes.
=TEXT(Cellwithnumber,"000000000")
Normally, if you format a range as text prior to typing in data (by right-clicking → format cells) or with something like
Dim c As Object
For Each c In Selection.Cells
c.NumberFormat = "#"
Next c
or
ActiveSheet.Cells.NumberFormat = "#"
Excel wont cut the leading zeroes.
If other users are using your sheet, you could protect the cell formats, so they don't accidentally change it back to numbers by, say, pasting data in with formats.
You could create a new cell and enter them as text formulas. Say the value you want to get the 0 from is in A1, enter your formula in a cell:
=TEXT(A1,"000000000")
That will show the leading 0.
If you want to use VBA:
rngTarget.NumberFormat = "#" ' rngTarget is a Range, can be e.g. ActiveCell or ActiveSheet.Cells(1, 2) or ActiveSheet.Range("B8")

Count number of numbers in a single cell

I want to count the number of numbers in a single cell.
Example:
In cell A1, I have the following addition:
=12+45+23+51+10 which totals (and shows) 141.
In cell B1, I would like the see how many numbers have been added together, which means that there should be 5 (12 is a number, 45 another one, etc... and all together, there are 5 numbers in cell A1).
I know it seems to be a ridiculous question, but I scanned all the platforms for this issue and did not find any suitable solution. Tried all the LEN and LEN SUBSTITUTE alternatives out there, but somehow it does not work.
Thank you upfront for your help. Optimal solution would be a excel formula, alternatively VBA would also work.
Excel 2013 has a new function Formulatext() which returns the, well, formula text. Using that the Len() and Substitute() approach works.
=LEN(FORMULATEXT(A1))-LEN(SUBSTITUTE(FORMULATEXT(A1),"+",""))+1
Hit Alt+F11 and Insert->Module with the following VBA:
Function NumCount(Rng As Range)
x = Split(Rng.Formula, "+")
NumCount = UBound(x) + 1
End Function
Then you can call =NumCount(A1) on any cell to get the number of numbers in the formula for that cell.
Use this VBA:
Function GetFormula(Cell as Range) as String
GetFormula = Cell.Formula
End Function
and then to get the number of numbers...
=LEN(GetFormula(D99))-LEN(SUBSTITUTE(GetFormula(D99),"+",""))
on this as my D99 contents...
=45+46+47+50+100
The one major drawback here is that I'm assuming everything is + if you have -, *,/ or other operators this gets more challenging. you might be able to use replace for each but you'd always be limited to the 4 basic operators... if someone used exponents or mod, this would be harder.
Also possible in earlier Excel versions without VBA - subject to (i) there is always at least one value in the cells, (ii) the operator is always + and (iii) the cells are in a column.
Replace = with '= in that column, apply the substitution, say:
=LEN(A1)-LEN(SUBSTITUTE(A1,"+",""))+1
in a different column and Paste Special, Value the results for that other column. Then apply Text To Columns on the original column with ' as the delimiter.
*There is no way to do this without using a User Defined Function (UDF) written in Excel VBA. Something like this should work:
Public Function numsAdded(cell1 As Range)
Dim formulaString As String
formulaString = cell1.Formula
numsAdded = Len(formulaString) - Len(Replace(formulaString, "+", "")) + 1
End Function
Once you've added that to a VBA module, you can use it as a function in any cell in your spreadsheet.
*Edit - Apparently there is a way if you have Excel 2013, as teylyn suggests. If you use 2010 or earlier like me, you'll need VBA.
Try this:
=LEN(SUBSTITUTE(F16,"+","+"))
Note: F16 is only an example name for the cell you want to do the counting on.
Example:
F16=45+65+76 # Gives 186
F17=LEN(SUBSTITUTE(F16,"+","+")) # Gives 3

How to convert the actual excel formula (not the result) to a text string

I have been searching all over for a solution and I couldn't find one.
Let's say I type into cell A1: =if(A2>1,1,0)
Excel will interpret that and either return 1 or 0.
However, what I would like to do is literally convert that formula into the string: '=if(A2>1,1,0)'
If you press Ctrl+~ into Excel, you will see all the formulas. Basically what I want to do, in conclusion, is take all those formulas and make them strings.
(The purpose, if you care, is to compare these strings with another workbook to make sure I didn't delete anything)
You can make a user defined function for this.
Put this code into a Module (not a worksheet code sheet, class, or form)
Function GetFormula(rng As range, Optional asR1C1 As Boolean = False) As String
If asR1C1 Then
getFormula = rng.FormulaR1C1
Else
getFormula = rng.Formula
End If
End Function
and call it in your worksheet like so
=GetFormula(A1)
or if you want the results as R1C1
=GetFormula(A1,TRUE)
Use the FORMULATEXT function. It's built into Excel.
what I would like to do is literally convert that formula into the
string: '=if(A2>1,1,0)'
Simply use the Formula Property of the cell, here is a code example:
Range("A1").Select
Dim strFormula As String
strFormula = ActiveCell.Formula
MsgBox (strFormula)
You can also replace the equal sign with blank using find and replace. In effect removing the equal sign and leaving only the text of the formula as a string.
Just follow this way.
Select the range (contain formula) you want to convert to text,
also you can select All range on your sheet.
Use Find and Replace command by pressing Control-F
then replace "=" with "^"
press replace All..
All done....
If you want to use it again as formula...
Just use Find and Replace, then replace "^" with "="
just look....All formula in text format converted to formula...
Just right-click on the column header (i.e. A) and select "Format Cells" then under the "Number" tab select "Text". Then at each cell press enter and the formula will be displayed instead of the result.

Excel number formatting. Treat my cell as a number but leave its formatting alone! (ie Trailing 0's)

I have (what seems like it should be) a simple problem. I need to be able to tell excel (in vba) that a cell's contents are numeric, but I don't really want to apply any formatting to it. I need my trailing zeros left how they are. Am I missing something incredibly simple?
Update:
I'm getting xml data from a query and am placing it into the spreadsheets. But lets say one of the cells has 589.950000 I need to keep those additional zeros on display for the user (don't ask me why, they just want the precision) but excel converts it to 589.95. I also need for them to be able to do spreadsheet calculations on that cell (so I can't format it as text).
Update 2:
Further Clarification. Lets say I have 4 values that I'm going to place into a column
595.56000
15.00
90.00050
1919.120000000
is there one numeric format that I can apply to that column that will display those exact numbers?
You can't do this with one custom format.
What you can do is make a macro that does the input for you and modifies each cell format as it puts the value into it. You can use the Macro Recorder to get a handle on where to start with this.
I think you can do:
Val(Range("A1").Value)
Which will evalulate the text to a number
You mean you want to use the value of the cell as a numerical one but you don't want to change the real val of the cell ?
In vba, you can use CInt(yourvar) to convert a string to number. Thus, the value you get will sure be an integer.
[edit] Btw, if you want to display or set back the value to a cell with the format you want, you can use the Format ( expression, [ format ] ) function of Excel vba
[edit 2]
As you cannot predict how many zeros you will have, i can't see any number format that would work. You'd probably better use the Text format as Lance Robert suggested. To use formula on text cells, you can use the VALUE formula to convert the cell to use it as a number. If you need it on a range, you may have to use array formulas like this : {=SUM(IF(ISTEXT(A1:A4);VALUE(A1:A4);A1:A4))}.
Please let us know if you want some help on these formulas.
Regards,
Max
If you know the number of decimal places you need, you can use something like the following:
Range("A1").NumberFormat = "0.000000"
I suppose you could even get fancy and check the number of trailing 0s in the code and adjust the formatting as required.
UPDATE:
Here's a VBA function that takes the number as a string and returns the appropriate NumberFormat string.
Private Function trailing(strNum As String) As String
'From number entered as string, returns Excel Number format to preserve trailing zeroes in decimal.
Dim decpt As Integer
Dim aftdec As Integer
Dim strTmp As String
decpt = InStr(strNum, ".")
If decpt = 0 Then
strTmp = "0"
Else
aftdec = Len(strNum) - decpt
strTmp = "0."
If aftdec <> 0 Then
For i = 1 To aftdec
strTmp = strTmp & "0"
Next
End If
End If
trailing = strTmp
End Function
Can't believe I just stumbled on this...
I know it's old but might as well give very simple answer:
Custom format and simply use # symbol. Will be treated as integer and left exactly as typed in to cell.

How to get Excel to ignore apostrophe in beginning of cell

I'm writing a tool that syncs a simple database with Excel sheets. Each item in a table in the database corresponds to one row in the worksheet. I read the Excel sheet into the tool using C# and the Excel interop com interface, then compared the items' values (i.e. one of the columns in the excel sheet) after the sync just to make sure that they are equal.
Yesterday I found a case where the comparison wasn't true:
"'<MedalTitle>' Medal - <MedalDescription>"
"<MedalTitle>' Medal - <MedalDescription>"
The second is the one I've read in from Excel, and as you can see it's skipped the first apostrophe. Is there a way to tell Excel to treat the cell as just text (no, just setting the cell's formatting doesn't help)?
I even tried to copy the value ( 'hello' ) of a cell in VBA like this:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Target.Offset(1, 0).Value = Target.Worksheet.Range("b2").Value
Target.Offset(2, 0).Value = Target.Worksheet.Range("b2").Formula
Target.Offset(3, 0).Formula = Target.Worksheet.Range("b2").Formula
Target.Offset(4, 0).Formula = Target.Worksheet.Range("b2").Value
End Sub
The result was that the value of target cell is always hello'
If there is no way, I'll have to do something ugly like
if (dbitem.value[0] == ''' )
{
// stuff
}
else
{
// regular comparison
}
I'm afraid the apostrophe ' is a special character for Excel when it appears as the first character in a cell as you've found. It tells Excel to treat the rest of the string as text, so that you can enter something like '34.2 in the cell, and it'll treat it as the string instead of the number (for formatting and so on).
I suggest doing something similar to what you've suggested, except that where you're putting it into Excel, check the first character, and add an extra ' if there's one there already.
Alternatively, you could prepend an apostrophe to all values - if you want them all as text that is. That way you don't need the extra first character check.
Look at the PrefixCharacter property of the Range object which corresponds to that cell
From the help:
If the TransitionNavigKeys property is
False, this prefix character will be '
for a text label, or blank. If the
TransitionNavigKeys property is True,
this character will be ' for a
left-justified label, " for a
right-justified label, ^ for a
centered label, \ for a repeated
label, or blank.
The TransitionNavigKeys part relates to Lotus 1-2-3 compatibility so it's more than likely going to be False
Answer based on article at:
http://excel.tips.net/Pages/T003332_Searching_for_Leading_Apostrophes.html
(warning: slightly annoying pop-up may appear)
edit: actually this probably isn't going to be any use because PrefixCharacter is read-only :(
edit2: I was right the first time. PrefixCharacter only gets populated if the value added to the cell started with ' so just read back PrefixCharacter plus Value and concatenate. As long as TransitionNavigKeys is False, that is
try targetcell.Value instead. .Formula is the formula seen in the formula bar while .Value is the evaluated value of the cell.
So, I am guessing that you would have used .Formula in your original code as well. Changing that should work.
EDIT: Ok, it did not work (embarrassed).
Excel treats the starting single quote specially.. so specially that even obscure cell / range properties do not have access. The only workaround I could find is essentially the same as what you thought initially. Here goes:
If VarType(cell) = 8 And Not cell.HasFormula Then
GetFormulaI = "'" & cell.Formula
Else
GetFormulaI = cell.Formula
End If
You might try pre-pending a single quote to your text fields ( '''' + dbField ) in your query so that for fields with embedded single quotes your query would return:
"''stuff in single quotes'"
which when placed in an Excel cell would convert to:
"'stuff in single quotes'"
for characters that weren't in quotes you would get:
"'stuff that wasn't in quotes"
which when placed in an Excel cell would convert to:
"stuff that wasn't in quotes"
Worth a shot. :-)

Resources