简体   繁体   English

Excel中的Visual Basic

[英]visual basic in Excel

I need your help please 我需要你的帮助
I have created a module into Microsoft Excel and I wrote this code . 我已经在Microsoft Excel中创建了一个模块,并编写了此代码。
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 例如,如果我在工作表的一个单元格中输入数字5,我想通过使用我称为(Number_To_Words)的新函数来显示此结果“ 5叙利亚镑”,但是当我使用该函数时结果为0
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: 您对后缀1和后缀2的声明缺少DIM命令:

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 . 另外,您需要先使用LEN测试Number_To_Words中的长度,然后才能将单元格中的值转换为字符串。

Try changing the test to: 尝试将测试更改为:

Select Case Len(CStr(number))

Also, check your spelling of "syrian" in suffix2. 另外,请检查后缀2中“叙利亚”的拼写。


EDIT 编辑

You could replace your TheOnes function with the following slightly neater alternative: 您可以使用以下更简洁的替代方法替换TheOnes函数:

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 . 我还将考虑使用Dim适当缩进您的工作并声明变量。 This can be helpful in determining a variables data type on declaration, however specifying works as well when using Option Explicit Off . 这有助于确定声明时的变量数据类型,但是在使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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