如何根据用户表单中填写的文本字段更改宏值?

发布于 2025-01-12 22:43:16 字数 3209 浏览 1 评论 0原文

考虑我有一个段落:
我们的团队正在开发 (5) 个项目,

其中 是用户表单中的填充字段。

我们如何在旁边的括号中显示五的数字形式?

我尝试创建一个宏来将数字的单词形式转换为数字形式。

Option Explicit
Sub get_number(mstr)

ones = Array("one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
ones_num = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
    
teens = Array("eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen")
teens_num = Array(11, 12, 13, 14, 15, 16, 17, 18, 19)
        
tys = Array("twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety")
tys_num = Array(20, 30, 40, 50, 60, 70, 80, 90)

arr = Split(mstr, " ")
    
get_number = -1 word_index = UBound(arr) 'start from last word

parsed_value = -1
    
'check first word in ONES
For i = 0 To UBound(ones) 'search through ones
    If ones(i) = arr(word_index) Then '1st word found and it is ONES, for example it is FIVE
        parsed_value = ones_num(i)
        If UBound(arr) = 0 Then
            'there is no second word
            GoTo end_of_code
        Else
            'go and read second word
            GoTo second_word
        End If
    End If
Next i
         
'check first word in TEENS
For i = 0 To UBound(teens)
    If teens(i) = arr(word_index) Then '1st word found and it is TEENS, for example it is FIVETEEN
        parsed_value = teens_num(i)
           
        If UBound(arr) = 0 Then
            'there is no second word
            GoTo end_of_code
        Else
            'go and read second word
            GoTo second_word
        End If
    End If
Next i
    
'check first word in TYS
For i = 0 To UBound(tys)
    If tys(i) = arr(word_index) Then '1st word found and it is TYS, for example it is FIFTY
        parsed_value = tys_num(i)
        If UBound(arr) = 0 Then
            'there is no second word
            GoTo end_of_code
        Else
            'go and read second word
            GoTo second_word
        End If
    End If
Next i
    
'check if first word is HUNDRED
If arr(word_index) = "hundred" Then
    parsed_value = 100
    If UBound(arr) = 0 Then
        'there is no second word
        GoTo end_of_code
    Else
        'go and read second word
        GoTo second_word
    End If

    'if first word not found, then jump to end of code without parsing second word, no point
    If parsed_value < 0 Then GoTo end_of_code

second_word: 'start analysing second word
    word_index = word_index - 1
    second_found = False
    If parsed_value < 10 Then
        '1st word was in ONES
        'check first word in TYS
        For i = 0 To UBound(tys)
            If tys(i) = arr(word_index) Then '1st word found and it is TYS, for example it is FIFTY
                parsed_value = tys_num(i) + parsed_value
                second_found = True
                If UBound(arr) = 1 Then
                    GoTo end_of_code 
                End If
            End If
        Next i
    End If
    
end_of_code:
    'end of code finish things here
        
    If parsed_value > 0 Then get_number = parsed_value
      
End Sub  

填充字段(mstr;例如:五)位于另一个宏中。

我如何连接这两件事?

Consider I have a paragraph:
Our team is working on five (5) projects

Where five is a fill field in a userform.

How do we show the number form of five in brackets beside it?

I tried by creating a macro for the conversion word form of a number to number form.

Option Explicit
Sub get_number(mstr)

ones = Array("one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
ones_num = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
    
teens = Array("eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen")
teens_num = Array(11, 12, 13, 14, 15, 16, 17, 18, 19)
        
tys = Array("twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety")
tys_num = Array(20, 30, 40, 50, 60, 70, 80, 90)

arr = Split(mstr, " ")
    
get_number = -1 word_index = UBound(arr) 'start from last word

parsed_value = -1
    
'check first word in ONES
For i = 0 To UBound(ones) 'search through ones
    If ones(i) = arr(word_index) Then '1st word found and it is ONES, for example it is FIVE
        parsed_value = ones_num(i)
        If UBound(arr) = 0 Then
            'there is no second word
            GoTo end_of_code
        Else
            'go and read second word
            GoTo second_word
        End If
    End If
Next i
         
'check first word in TEENS
For i = 0 To UBound(teens)
    If teens(i) = arr(word_index) Then '1st word found and it is TEENS, for example it is FIVETEEN
        parsed_value = teens_num(i)
           
        If UBound(arr) = 0 Then
            'there is no second word
            GoTo end_of_code
        Else
            'go and read second word
            GoTo second_word
        End If
    End If
Next i
    
'check first word in TYS
For i = 0 To UBound(tys)
    If tys(i) = arr(word_index) Then '1st word found and it is TYS, for example it is FIFTY
        parsed_value = tys_num(i)
        If UBound(arr) = 0 Then
            'there is no second word
            GoTo end_of_code
        Else
            'go and read second word
            GoTo second_word
        End If
    End If
Next i
    
'check if first word is HUNDRED
If arr(word_index) = "hundred" Then
    parsed_value = 100
    If UBound(arr) = 0 Then
        'there is no second word
        GoTo end_of_code
    Else
        'go and read second word
        GoTo second_word
    End If

    'if first word not found, then jump to end of code without parsing second word, no point
    If parsed_value < 0 Then GoTo end_of_code

second_word: 'start analysing second word
    word_index = word_index - 1
    second_found = False
    If parsed_value < 10 Then
        '1st word was in ONES
        'check first word in TYS
        For i = 0 To UBound(tys)
            If tys(i) = arr(word_index) Then '1st word found and it is TYS, for example it is FIFTY
                parsed_value = tys_num(i) + parsed_value
                second_found = True
                If UBound(arr) = 1 Then
                    GoTo end_of_code 
                End If
            End If
        Next i
    End If
    
end_of_code:
    'end of code finish things here
        
    If parsed_value > 0 Then get_number = parsed_value
      
End Sub  

The fill field (mstr; eg:five) is in another macro.

How do I connect these two things?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文