如何根据用户表单中填写的文本字段更改宏值?
考虑我有一个段落:
我们的团队正在开发五 (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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论