0

I'm trying to conver a number value to a strin ex. convert 10 to "10"

I tried

    With Range("B2:B" & NewResizeRange)
      .Value = Range("B2:B" & NewResizeRange).Value
      .NumberFormat = "0" ' Or .NumberFormat = "@"
   End With

But it us not converting correctly. Any ideas?

Jordan
  • 657
  • 3
  • 9
  • 16
  • You need to [reevaluate the cell](https://superuser.com/q/299437/52365) after setting the format. – GSerg Jul 07 '17 at 07:44
  • I tried using Range("B2:B").Formula = "=Text(B2,""0"")" but thats just writting the formula as = TEXT(B2;"0") but I want it to write =TEXT(B2;0) @GSerg – Jordan Jul 07 '17 at 07:55
  • 1
    If you want to use a formula, you don't want to set the text format. `=TEXT(B2;0)` is invalid because the second argument of `TEXT` is a format string. It is also invalid to try to make a formula that refers to its own cell (circular dependency). – GSerg Jul 07 '17 at 08:11
  • That explains why I keep getting zeros. Thanks for explaining that @GSerg – Jordan Jul 07 '17 at 08:13

4 Answers4

1
With Range("A1")
    .NumberFormat = "@"
    .Value = .Value
End With

This will make the green triangle for Number stored as text show up and mean that your cell will indeed hold a text value. However, when doing calculations using such a cell as a parameter for the formula, Excel will do some conversion by itself.

If you do not want to use NumberFormat, you could also do:

Range("A1").value = "=""" & Range("A1").value & """" - This creates a formula (If the value was 10, it'll make a formula ="10") that forces the value to evaluate to a string.

Rik Sportel
  • 2,661
  • 1
  • 14
  • 24
  • If I change Range("A1").value = "=""" & Range("A1").value & """" to Range(("B2:B" & NewResizeRange).value = "=""" & Range(("B2:B" & NewResizeRange).value & """" Anyway around that? @RikSportel – Jordan Jul 07 '17 at 08:11
  • Use an array and loop. You can't assign "bulk" values within a string (the formula) like that. – Rik Sportel Jul 07 '17 at 10:26
  • 1
    I Tried the following without succes: 'For Count_Text = 2 To NewResízeRange Range("B" & Count_Text).Value = "=""" & Range("B" & Count_Text).Value & """" Next Count_Text ' – Jordan Jul 07 '17 at 10:29
  • Yeah that'll work. Consider using an array when dealing with 1000s of cells, though. – Rik Sportel Jul 07 '17 at 10:30
  • The loop did not work though @RikSportel . Nothing is happening – Jordan Jul 07 '17 at 10:32
  • 1
    Is there a typo in NewResizeRange? That í. What error? – Rik Sportel Jul 07 '17 at 10:32
  • Thats it! I'm resetting NewResizeRange before running the function. Thank you for all the help! Cheers – Jordan Jul 07 '17 at 10:34
  • just out of curiosity, why is it better to use an array ? And how would one implement an array in this exemple? – Jordan Jul 07 '17 at 14:03
  • The short answer: Arrays are in memory and you have less I/O operations. When working against the range objects directly, you have to access each cell, which adds a lot of overhead compared to just loading the values in an array, do your thing, write the array back to the worksheet. See [Chip Pearson's article](http://www.cpearson.com/excel/ArraysAndRanges.aspx) on that. – Rik Sportel Jul 07 '17 at 14:05
  • Cheers! @RikSportel – Jordan Jul 07 '17 at 14:06
0

Try selecting your range first. So:

   With Range("B2:B" & NewResizeRange)
      .Value = Range("B2:B" & NewResizeRange).Value
      .Select
      .Selection.NumberFormat = "@"
   End With

Hope this helps.

Roosz0rd
  • 192
  • 13
0

Convert Integer into String in Excel VBA for Indian Currency in Rupees

Paste this code in macro by creating a module

Function name is ConvertIndianCurrency

   Function ConvertIndianCurrency(ByVal MyNumber)
Dim Temp
Dim Count, DecimalPlace
ReDim Place(9) As String
Place(2) = " Thousand "
Place(3) = " Lakh "
Place(4) = " Crore "
' convert MyNumber to a string, trimming extra spaces.
MyNumber = Trim(Str(MyNumber))
' Find decimal place.
DecimalPlace = InStr(MyNumber, ".")
' If we find decimal place...
If DecimalPlace > 0 Then
    ConvertIndianCurrency = "Please Remove Decimal Places in the Number..."
    Exit Function
End If
Count = 1
If MyNumber <> "" Then
    ' convert last 3 digits of MyNumber to Indian Rupees.
    Temp = ConvertHundreds(Right(MyNumber, 3))
    If Temp <> "" Then
        Rupees = Temp
    End If
    If Len(MyNumber) > 3 Then
        ' Remove last 3 digits from MyNumber.
        MyNumber = Left(MyNumber, Len(MyNumber) - 3)
    Else
        MyNumber = ""
    End If
End If
Do While MyNumber <> ""
    Count = Count + 1
    Temp = ConvertTwoDigits(Right(MyNumber, 2))
    If Temp <> "" Then
        Rupees = Temp & Place(Count) & Rupees
    End If
    If Len(MyNumber) > 2 Then
        ' Remove last 2 converted digits from MyNumber.
        MyNumber = Left(MyNumber, Len(MyNumber) - 2)
    Else
        MyNumber = ""
    End If
Loop
' Clean up Rupees.
Select Case Rupees
    Case ""
        Rupees = "Rupees Nil"
    Case "One"
        Rupees = "Rupee One Only"
    Case Else
        Rupees = "Rupees " & Rupees & " Only"
End Select
ConvertIndianCurrency = Rupees
End Function

Private Function ConvertHundreds(ByVal MyNumber)
Dim Result As String
' Exit if there is nothing to convert.
If Val(MyNumber) = 0 Then
    Exit Function
End If
' Append leading zeros to number.
MyNumber = Right("000" & MyNumber, 3)
' Do we have a hundreds place digit to convert?
If Left(MyNumber, 1) <> "0" Then
    Result = ConvertDigit(Left(MyNumber, 1)) & " Hundred "
End If
' Do we have a tens place to convert?
If Mid(MyNumber, 2) <> "0" Then
    Result = Result & ConvertTens(Mid(MyNumber, 2))
Else
    ' If not, then convert the ones place digit.
    Result = Result & ConvertDigit(Mid(MyNumber, 3))
End If
ConvertHundreds = Trim(Result)
End Function

Private Function ConvertTwoDigits(ByVal MyNumber)
Dim Result As String
' Exit if there is nothing to convert.
If Val(MyNumber) = 0 Then
    Exit Function
End If
' Append leading zeros to number.
MyNumber = Right("00" & MyNumber, 2)
' Do we have a tens place to convert?
If Left(MyNumber, 1) <> "0" Then
    Result = ConvertTens(Mid(MyNumber, 1))
Else
    Result = Result & ConvertDigit(Right(MyNumber, 1))
End If
ConvertTwoDigits = Trim(Result)
End Function

Private Function ConvertTens(ByVal MyTens)
Dim Result As String
' Is value between 10 and 19?
If Val(Left(MyTens, 1)) = 1 Then
    Select Case Val(MyTens)
        Case 10: Result = "Ten"
        Case 11: Result = "Eleven"
        Case 12: Result = "Twelve"
        Case 13: Result = "Thirteen"
        Case 14: Result = "Fourteen"
        Case 15: Result = "Fifteen"
        Case 16: Result = "Sixteen"
        Case 17: Result = "Seventeen"
        Case 18: Result = "Eighteen"
        Case 19: Result = "Nineteen"
        Case Else
    End Select
Else
    ' ... otherwise its between 20 and 99.
    Select Case Val(Left(MyTens, 1))
        Case 2: Result = "Twenty "
        Case 3: Result = "Thirty "
        Case 4: Result = "Forty "
        Case 5: Result = "Fifty "
        Case 6: Result = "Sixty "
        Case 7: Result = "Seventy "
        Case 8: Result = "Eighty "
        Case 9: Result = "Ninety "
        Case Else
    End Select
    ' convert ones place digit.
    Result = Result & ConvertDigit(Right(MyTens, 1))
End If
ConvertTens = Result
End Function
Private Function ConvertDigit(ByVal MyDigit)
Select Case Val(MyDigit)
    Case 1: ConvertDigit = "One"
    Case 2: ConvertDigit = "Two"
    Case 3: ConvertDigit = "Three"
    Case 4: ConvertDigit = "Four"
    Case 5: ConvertDigit = "Five"
    Case 6: ConvertDigit = "Six"
    Case 7: ConvertDigit = "Seven"
    Case 8: ConvertDigit = "Eight"
    Case 9: ConvertDigit = "Nine"
    Case Else: ConvertDigit = ""
End Select
End Function
0
Dim XN As Integer
Dim XS As String

XN = 10
XS = Trim(Str(XN))  'now XS = "10"
Matu
  • 1