简体   繁体   中英

visual basic in Excel

I need your help please
I have created a module into Microsoft Excel and I wrote this code .
I want to convert numbers to words
for example if I enter number 5 inside a cell in worksheet , I want to show this result "five syrian pounds " by using the new Function which I called ( Number_To_Words ) but the result is 0 when I used the function
What is the Wrong in this Code ???

Function TheOnes(number As Integer)
suffix1 As String
suffix2 As String
suffix1 = "one syrian pound"
suffix2 = "syrain pounds"
Select Case number
Case 1: TheOnes = suffix1
Case 2: TheOnes = " Two " + suffix2
Case 3: TheOnes = " Three " + suffix2
Case 4: TheOnes = " Four " + suffix2
Case 5: TheOnes = " Five " + suffix2
Case 6: TheOnes = " Six " + suffix2
Case 7: TheOnes = " Seven " + suffix2
Case 8: TheOnes = " Eight " + suffix2
Case 9: TheOnes = " Nine " + suffix2
End Select
End Function
Function Number_To_Words(number As Integer)
Select Case Len(number)
Case 1: Number_To_Words = TheOnes(number)
End Select
End Function

Your declarations for suffix1 and suffix2 are missing the DIM command:

Dim suffix1 As String
Dim suffix2 As String

Also, you need to convert the value in the cell to a string before testing the length in Number_To_Words with LEN .

Try changing the test to:

Select Case Len(CStr(number))

Also, check your spelling of "syrian" in suffix2.


EDIT

You could replace your TheOnes function with the following slightly neater alternative:

Function TheOnes(number As Integer)
    Dim suffix
    suffix = Array(" Zero", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine")
    TheOnes = suffix(number)
    TheOnes = TheOnes & " syrian pound"
    If number > 1 Then TheOnes = TheOnes & "s"
End Function

You are getting the length of an integer in you second select case. Length would be used for getting the size of an array for example. I believe the following will help you in in your future endeavors. I would also consider properly indenting your work and declaring variables using Dim . This can be helpful in determining a variables data type on declaration, however specifying works as well when using Option Explicit Off .

 Function TheOnes(number As Integer)
        Dim suffix1 As String
        Dim suffix2 As String
        suffix1 = "one syrian pound"
        suffix2 = "syrain pounds"
        Select Case number
            Case 1 : TheOnes = suffix1
            Case 2 : TheOnes = " Two " + suffix2
            Case 3 : TheOnes = " Three " + suffix2
            Case 4 : TheOnes = " Four " + suffix2
            Case 5 : TheOnes = " Five " + suffix2
            Case 6 : TheOnes = " Six " + suffix2
            Case 7 : TheOnes = " Seven " + suffix2
            Case 8 : TheOnes = " Eight " + suffix2
            Case 9 : TheOnes = " Nine " + suffix2
        End Select
    End Function

    Function Number_To_Words(number As Integer)
        Select Case number
            Case 1 : Number_To_Words = TheOnes(number)
        End Select
    End Function

您应该摆脱Number_To_Words并直接致电TheOnes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM