Results 1 to 7 of 7

Thread: How to convert vb .text to number format

  1. #1
    Join Date
    May 2008
    Posts
    222

    How to convert vb .text to number format

    I have the requirement to convert the word 2007 text file and need to have visual basic code for converting a selected text to a number, and a number to text? If it would be possible then please let me, i also expect answer descriptive as well.

    Thanks for those who help..

  2. #2
    Join Date
    Dec 2008
    Posts
    183

    Re: How to convert vb .text to number format

    The number to be converted to word text can be a numeric value or a string.

    • If the optional formatting keyword is omitted, a number such as 1234 will be returned as "One Thousand Two Hundred Thirty Four".
    • If the word "check" is passed, the number returned will be written as on a check. Hence, 123.45 will report back as "One Hundred Twenty Three and 45/100". If there are more than two decimal places in the "check" mode, the decimal portion will be rounded to the nearest two decimal places.

  3. #3
    Join Date
    Jan 2006
    Posts
    211

    Re: How to convert vb .text to number format

    To convert String to Number i will suggest you to use the Val function which will convert a string to a decimal number.If you need a formula to simply change numbers to text, use =concatenate([Cell#]). The Val function is not locale aware, so it will not recognize comma decimal separators in countries where this is used, such as "1,000,000".

    following is the vb code which will allows you to do the conversion.
    Dim v As Integer

    v = Val("100 Blah Blah")

    Print v ' prints 100

    v = Val("Nothing")

    Print v ' Could not convert, prints 0

  4. #4
    Join Date
    Oct 2008
    Posts
    167

    Re: How to convert vb .text to number format

    Code:
    found = InStrRev(Text1.Text, Chr(10), pos, 0)
    Text1.text is the string being searched and chr(10) is the string being searched. We take a new variable called pos, which signifies the starting postion for the search in the given string (i.e. text1.text). The SendMessage API function requires the window handle (hWnd& above) of the text box. To get the window handle of the text box, use the hWnd property of the text box. Initially pos is set as the current caret location, which is given by text1.selstart Pos is taken since when we find a new line char, its obvious that we will look for a new line char above of it.

  5. #5
    Join Date
    Feb 2009
    Posts
    96

    Re: How to convert vb .text to number format

    ' Convert a number between 0 and 999 into words.
    Private Function numbers(ByVal num As Integer) As String
    Static onetonineteen() As String = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen", "Nineteen"}
    Static tens() As String = {"Twenty -", "Thirty -", "Forty -", "Fifty -", "Sixty -", "Seventy -", "Eighty -", "Ninety -"}
    Static thousand() As String = {" Thousand "}
    Dim place As Integer
    Dim valid As Boolean = True
    Dim result As String = ""
    'http://www.vb-helper.com/howto_net_dollars_to_words.html is somewhat used here



    ' Handle the hundreds digit.
    If txtAmount.Text.Length = 0 Then
    MessageBox.Show("Please enter an amount.")
    valid = False
    End If
    If num > 99 Then
    place = CInt(num / 100)
    num = num Mod 100
    result = onetonineteen(place) & " Hundred"
    End If

    ' If num = 0, we have hundreds only.
    If num = 0 Then Return result.Trim()

    ' See if the rest is less than 20.
    If num < 20 Then
    ' Look up the correct name.
    result &= " " & onetonineteen(num)
    Else
    ' Handle the tens digit.
    place = num \ 10
    num = num Mod 10
    result &= " " & tens(place - 2)

    ' Handle the final digit.
    If num > 0 Then
    result &= " " & onetonineteen(num)
    valid = True
    End If

    If result.EndsWith(", ") Then
    result = result.Substring(0, result.Length - 2)
    End If
    If valid = True Then
    Me.Hide()
    End If
    If valid = False Then
    txtAmount.Text = ""
    End If

    End If

    Return result.Trim()
    End Function

  6. #6
    Join Date
    Feb 2009
    Posts
    96

    Re: How to convert vb .text to number format

    Private Function NumberToString(ByVal num As Double) As _
    String

    ' Remove any fractional part.
    num = Int(num)

    ' If the number is 0, return zero.
    If num = 0 Then Return "zero"

    Static groups() As String = {"", "thousand", "million", _
    "billion", "trillion", "quadrillion", "?", "??", _
    "???", "????"}
    Dim result As String = ""
    Dim valid As Boolean = True
    ' Process the groups, smallest first.
    Dim quotient As Double
    Dim remainder As Integer
    Dim group_num As Integer = 0
    Do While num > 0 And num < 9999.99
    ' Get the next group of three digits.
    quotient = Int(num / 1000)
    remainder = CInt(num - quotient * 1000)
    num = quotient

    ' Convert the group into words.
    result = numbers(remainder) & _
    " " & groups(group_num) & ", " & _
    result

    ' Get ready for the next group.
    group_num += 1
    valid = True
    Loop

    If txtAmount.Text.Length = 0 Then
    MessageBox.Show("Please enter an amount.")
    valid = False
    End If
    If num < 0 And num > 9999.99 Then
    MessageBox.Show("Amount must be greater then 0 and less then $10,000 .")
    txtAmount.Text = String.Empty
    valid = False
    End If

    If valid = True Then
    Me.Hide()
    End If




    ' Remove the trailing ", ".
    If result.EndsWith(", ") Then
    result = result.Substring(0, result.Length - 2)
    End If

    Return result.Trim()
    End Function

  7. #7
    Join Date
    Feb 2009
    Posts
    96

    Re: How to convert vb .text to number format

    View the above samples but please dont just out right copy it unless it was a share ware thingy.

Similar Threads

  1. How to convert number to text in excel
    By Jevin in forum Software Development
    Replies: 9
    Last Post: 17-05-2012, 06:42 PM
  2. How to convert math text PDF format to Kindle readable format
    By The#Gaelic in forum Portable Devices
    Replies: 7
    Last Post: 23-02-2012, 11:09 AM
  3. How to convert HTML to TEXT format ?
    By Custidio in forum Windows Software
    Replies: 5
    Last Post: 09-02-2010, 01:22 AM
  4. Convert a text document in PDF format (Mac)
    By Waffle in forum Operating Systems
    Replies: 4
    Last Post: 03-02-2010, 09:33 PM
  5. How can I convert text data into table format?
    By Ram Bharose in forum Windows Software
    Replies: 7
    Last Post: 30-12-2009, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •