views:

164

answers:

2

Morning All,

Today i came up with the new assignment of Converting the Text into number automatically by VBA Codes. For Example '1233 need to converted to 1233 and (1234) need to converted to -1234 without any human interference. Some Advice is very helpful.

Regards, A Jabeer Ali

A: 

Misunderstood your question, my apologies.

This worked for me, even outside excel:

Function numConv(num As String) As Double

    If Mid(num, 1, 1) = "(" And Mid(num, (Len(num)), 1) = ")" Then
        num = Replace(num, "(", "")
        num = Replace(num, ")", "")
        numConv = val(num) * -1
    Else
        numConv = val(num)
    End If

End Function
eppdog
He's not trying to flip the sign. The parenthesis (1234) is an indicator of a negative number.
BradC
Also why to use Mid(num, 1, 1) = "(" And Mid(num, (Len(num)), 1) = ")" You should be fine with Left(num,1) = "(" And Right(num,1) = ")"
Adarsha
+2  A: 

Use the VALUE() function.

Also, from here:

Sub ConvertToNumbers() 
  Cells.SpecialCells(xlCellTypeLastCell) _
    .Offset(1, 1).Copy
  Selection.PasteSpecial Paste:=xlPasteValues, _
     Operation:=xlPasteSpecialOperationAdd
  With Selection
     .VerticalAlignment = xlTop
     .WrapText = False
  End With
  Selection.EntireColumn.AutoFit
End Sub
jeffamaphone