VBA - How to convert value of cell from text to number - vba

I have to columns with exported numbers, which are stored as text.
I tried to convert those data to numbers with this code.
Sub FormatColumns()
Columns(9).NumberFormat = "0"
Columns(10).NumberFormat = "0"
End Sub
It has done the job correctly (or it seems so), but I am still not able to work with those data as with numbers, but Excel tell me, that it is stored as number.
But when I verify it via formula =ISTEXT(), it always shows me TRUE
Could you help me with it, please? All tutorials and advices, which I've found on Google was via Formating cells or =VALUE(), but VALUE fucntion doesnt work for me, it shows me #VALUE.
Thank you in advance!

Another solution is:
Sub Macro1()
Range("A:A").Select 'Range of values you need converted
For Each Cell In Selection
Cell.Value = (Cell.Value * 1)
Next
End Sub
A non-vba equivalent solution is to have a "1" entered in a cell, copy that cell, select the range you would like converted to numbers, right click and press Paste Special -> Multiply.
This will leave all values in that range as numbers without changing the format. Tested with ISTEXT.
Make sure your settings also match with commas replacing decimals otherwise it may continue to have issues.

you must replace "," ~~> "."
Sub FormatColumns()
With Columns("i:j")
.Replace ",", "."
.NumberFormat = "#,###.00"
End With
End Sub

Check entire relevant region is set to Number format first.
Selection the relevant region and do a global replace of for nothing ("space" to "leave empty"). Then depending upon your locale try a global replace of , to ..
Next enter 1 in a spare cell, copy that cell, select the entire relevant region and Paste Special, Option, Multiply.
If that does not work switch . back to , and repeat the Paste Special.

Its the comma, needs to be . so 405,90 = 405.90

Converting numbers stored as text to numbers via Macro
Sub macro()
Range("F:F").Select 'specify the range which suits your purpose
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
End Sub

Hilight the numbers you with to "fix" and run:
Sub fixNumbers()
With Selection
.NumberFormat = "General"
.Value = .Value
End With
End Sub

Related

Embedded "IF" formula breaks occasionally, VBA alternative?

I have a very large embedded IF formula that appears to occasionally break for no reason. Opening and closing the page a few times eventually gets it working again. I am wondering if there is a VBA alternative for it. Here is the IF formula I am running.
=IF(ISNUMBER(SEARCH("76210",E125)),"_012_00762_10",IF(ISNUMBER(SEARCH("76220",E125)),"_012_00762_20",IF(ISNUMBER(SEARCH("76900",E125)),"_012_00769_00",IF(ISNUMBER(SEARCH("76901",E125)),"_012_00769_01",IF(ISNUMBER(SEARCH("85702",E125)),"_012_00857_02",IF(ISNUMBER(SEARCH("85710",E125)),"_012_00857_10",IF(ISNUMBER(SEARCH("100800",E125)),"_012_01008_00",IF(ISNUMBER(SEARCH("100900",E125)),"_012_01009_00",IF(ISNUMBER(SEARCH("123100",E125)),"_012_01231_00",IF(ISNUMBER(SEARCH("124600",E125)),"_012_01246_00",IF(ISNUMBER(SEARCH("124601",E125)),"_012_01246_01",IF(ISNUMBER(SEARCH("124640",E125)),"_012_01246_40",IF(ISNUMBER(SEARCH("124641",E125)),"_012_01246_41",IF(ISNUMBER(SEARCH("142301",E125)),"_012_01423_01",IF(ISNUMBER(SEARCH("158801",E125)),"_012_01588_01",IF(ISNUMBER(SEARCH("158900",E125)),"_012_01589_00",IF(ISNUMBER(SEARCH("159203",E125)),"_012_01592_03",IF(ISNUMBER(SEARCH("159303",E125)),"_012_01593_03",IF(ISNUMBER(SEARCH("159401",E125)),"_012_01594_01",IF(ISNUMBER(SEARCH("159410",E125)),"_012_01594_10",IF(ISNUMBER(SEARCH("159420",E125)),"_012_01594_20",IF(ISNUMBER(SEARCH("159501",E125)),"_012_01595_01",IF(ISNUMBER(SEARCH("169000",E125)),"_012_01690_00",IF(ISNUMBER(SEARCH("186900",E125)),"_012_01869_00",IF(ISNUMBER(SEARCH("213200",E125)),"_012_02132_00",IF(ISNUMBER(SEARCH("213300",E125)),"_012_02133_00",IF(ISNUMBER(SEARCH("215400",E125)),"_012_02154_00",IF(ISNUMBER(SEARCH("220100",E125)),"_012_02201_00",IF(ISNUMBER(SEARCH("223800",E125)),"_012_02238_00",IF(ISNUMBER(SEARCH("225600",E125)),"_012_02256_00",IF(ISNUMBER(SEARCH("230700",E125)),"_012_02307_00",IF(ISNUMBER(SEARCH("230701",E125)),"_012_02307_01",IF(ISNUMBER(SEARCH("231800",E125)),"_012_02318_00",IF(ISNUMBER(SEARCH("235000",E125)),"_012_02350_00",IF(ISNUMBER(SEARCH("235020",E125)),"_012_02350_20",IF(ISNUMBER(SEARCH("242000",E125)),"_012_02420_00",IF(ISNUMBER(SEARCH("246400",E125)),"_012_02464_00",IF(ISNUMBER(SEARCH("292900",E125)),"_012_02929_00",""))))))))))))))))))))))))))))))))))))))
Basically it is built so a serial number is scanned and it populates a cell for the users who use this sheet with its results from the search. I am already running one macro in this sheet as well. Here is that...
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Intersect(Range("A2:A500, J2:J500"), Target) ' define range of interest
If Not rng Is Nothing Then ' check it's not "nothing"
If WorksheetFunction.CountA(rng) = rng.Count Then 'check for all of its cells being not empty
On Error GoTo safe_exit 'add error control
Application.EnableEvents = False 'don't do anything until you know something has to be done
rng.Offset(, 1).Value = Date 'write Date next to all relevant changed cells
End If
End If
safe_exit:
Application.EnableEvents = True
End Sub
Maybe there is a better way to build this search using a formula that isn't using embedded IF statements, but i couldn't think of another way to do it. Thanks in advance.
This may be what you're looking for:
=IF(ISNA(MATCH(1,IF(ISERR(SEARCH($A$5:$A$42,$E$125)),0,1),0)),"",INDEX($B$5:$B$42,MATCH(1,IF(ISERR(SEARCH($A$5:$A$42,$E$125)),0,1),0)))
entered as an array formula (CTRL-SHIFT-ENTER).
Here $A$5:$A$42 contains 76210, 76220, ... , 292900 (entered as text, not numbers); and $B$5:$B$42 contains _012_00762_10, _012_00762_20, ... , _012_02929_00.
Hope that helps.
Any time you have to go more than 2 deep on an IF you may want to rethink the usage.
What you can do is build a table from your values. Then reference that table as part of your lookup. Assuming your list of value is in range D8:E45 you could use the formula =VLOOKUP(E125,$D$8:$E$45,2).
The beginning of your table would look like what's seen below. The input result cell is referencing your input value and pulling the match of the second column.
To get your table you can take your source formula and replace (Find and Replace - Ctrl+H) some characters with unique delimiting characters. Then use Text To Columns Alt+D+E and delimit and Copy>Paste special>Transpose to quickly have it close to the format you need.

Deleting a ' from a cell which is not being retrieved in .Value in VBA

I need to find out how to delete a ' which is at the very beginning of every cell in the A column in a Excel but I can't figure out how to do it.
If I perform a Replace with
Replace(ActiveWorkbook.Worksheets("MAIN").Cells(2, 1).Value, "'", "")
or just showing the cell value with a MsgBox it just shows the number itself, without the '.
Any idea about how to delete that character if I couldn't even access it?
P.S: I've tried a basic "find and replace" with same result, even saying I have no matches...
Thanks in advance.
EDIT: Pressing F2 on the "A2" cell to see the formula shows this:
You will not see ' in VBA, neither can you remove it with Excel replace dialog – this is a special symbol that forces numeric values to be treated as text.
You can just reassign formulas and this will remove ':
ActiveWorkbook.Worksheets("MAIN").Range("A1").Formula = ActiveWorkbook.Worksheets("MAIN").Range("A1").Formula
try this:
Sub main()
Dim cell As Range
For Each cell In Range("A1", Cells(Rows.Count, 1).End(xlUp))
cell.Value = Val(cell.Value)
Next
End Sub

How to check if a command is possible before executing it in vba?

I want to use a macro to change a long long column of cells. The cells are either numbers or text. The numbers, though, are stored as text and I want to convert them. I'd like to like
`Range("A1").Value = Int(Range("A1").Value)`
, and this works fine when converting the numbers that are stored as text to number, but obviously it won't work for the values that are text and have no numbers. How do I set the command to see, if this conversion is possible then do it?
Do them all at once with the Range.TextToColumns method. VBA's overhead will take care of error control.
with worksheets("Sheet1")
with intersect(.usedrange, .columns(1))
.numberformat = "General"
.TextToColumns DataType:=xlFixedWidth, FieldInfo:=Array(0, 1)
end with
end with
use IsNumeric. To avoid getting blanks converted to Zero, check empty too.
if IsNumeric(Range("A1").Value2) and Not IsEmpty(Range("A1")) then
Range("A1").Value = Int(Range("A1").Value)
end if
I think this should do it:
With Range("A:A") ' column A
.NumberFormat = "General"
.Value = .Value ' Excel should auto convert the strings to numbers
End With

VBA Vlookup formula, value does not appear in cell

My problem is that when i put this code in VBA:
Sub formula_vlookup()
'MONTHS
ActiveSheet.Cells(ActiveCell.Row, 1).Select
ActiveCell.Offset(0, 16).Select
With ActiveCell
.formula = "=IF(ISNA(VLOOKUP(" & .Offset(0, -16).Address(0, 1) & ",'CZ support'!$A:$AA,2,0)), _
"""",(VLOOKUP(" & .Offset(0, -16).Address(0, 1) & ",'CZ support'!$A:$AA,2,0)))"
End With
End Sub
It works perfectly ( i mean , it gives me the value of the cell that is "vlooked up" but the problem is that it does not display that value in the cell, so the only way to know if its a value there is making a SUM of the cells that contain this formula.
how can i manage this problem?
Thank you in advance for your help
Things to check:
Name [CZ Support] (with a BLANK in the middle) may be invalid ... I can't create that in XLS2010
check that the name exists (Name manager or just try to select it from the drop down left of formula bar)
Try entering the same formula (with .Offset(...) expanded to actual cell addresses) directly in worksheet cell(s)
Try replacing the "" in the FALSE part of the =IF(...) by some text (e.g. "not found") to see if you're caught by an exception
Try replacing the last VLOOKUP argument (0) by TRUE or FALSE as per the documentation
Try assigning the formula to a string variable first and examine it in the debugger window (or do a Debug.Print thereof)
pasting your code "as is" gave me an error - my VBA doesn't like the line splitting underscore in the formula generation

Change Value in Excel from String to Number

I wrote a script in VBA where numbers from a text file will be inserted into a worksheet. After inserting the values, Excel advises me to convert the values from strings to numbers. Inside the cell, a green triangle appears. After selecting the cell, there are different options you can select to format the value inside the cell.
I also tried to convert the values to double in VBA with CDbl(string) but nothing happens.
Changing the NumberFormat also doesn't work. I tried different values in VBA and also in Excel.
I also tried to record a macro while selecting the method but there was nothing saved inside the makro.
Anybody know how to do this?
Try this. I have taken cell A1 as an example. Also if there are decimal values then instead of Long use a Double and change the NumberFormat accordingly.
Sub Sample()
Dim clVal As Long
With Sheet1.[a1]
clVal = Val(.Value)
.NumberFormat = "0"
.ClearContents
.Formula = clVal
End With
End Sub
It might seem a bit silly, but have you tried Range("A1").Value = Range("A1").Value?
More generally, you can loop through myrange, assuming it is the range you want to fix:
For Each c in myrange
c.Value = c.Value
Next