我已经设置了一个包含很长的键和相关项列表的字典。用户通过输入框输入一串关键字,vba将每个单词拆分成一个数组,找到该键并将该项添加到输出字符串。例如:
用户类型"晚餐Applebee's Restaurant",代码将字符串分成三个单词,然后创建一个数字代码,其中每个单词由两位数字代表" 10 08 70" (添加空格以强调数字模式)。
这就是问题," Applebee's Restaurant"英语的流动性比#Apple; Restaurant Applebee"所以用户很可能会按照这个顺序输入单词,但这个数字应该真的反映出#Apple; Restaurant Applebee's。"喜欢它应该是类别然后放置而不是放置然后类别。数字代码实际应该是" 10 70 08。"
有没有办法在不制作单独字典的情况下对字典定义进行分组?或者评估每个单词并将它们排序成特定的顺序?因为前两个数字总是大于10,后两个数字也将大于10或将是00.第三个两个数字将始终小于10.所以只要我知道什么单词少于10 10,这个数字可以是最后一个。
按照正确的顺序键入单词很容易,但我担心外行人几乎不会采用简单的路径,并且有很多人会使用这个,每个人都在他们自己可疑的计算机级别-literacy。
请求了一个代码示例,所以这里是:
Dim dict As New Scripting.Dictionary
Dim InStr As String ' Input string
Dim Split() As String ' Array to hold words
Dim SegNum As String ' Output string
Dim I As Integer
' Adding searchable items to dictionary
' First two digits
dict.Add Key:="Dinner", Item:=10
dict.Add Key:="Lunch", Item:=15
dict.Add Key:="Breakfast", Item:=20
dict.Add Key:="Snack", Item:=50
' Second two digits
dict.Add Key:="Restaurant", Item:=70
dict.Add Key:="Home Cooked", Item:=80
' Third two digits
dict.Add Key:="Home", Item:=Format(0, "00)
dict.Add Key:="McDonald's", Item:=Format(1, "00")
dict.Add Key:="Burger King", Item:=Format(2,"00")
dict.Add Key:="Wendy's", Item:=Format(3, "00")
..
dict.Add Key:="Applebee's", Item:=Format(8, "00")
SplitStr = Split(InputBox("Please use some keywords to detail what you ate"), " ")
For I = LBound(SplitStr) To UBound(SplitStr)
SegNum = SegNum & dict(SplitStr(I))
Next
MsgBox SegNum
答案 0 :(得分:0)
这是一种方法。您需要添加来自同一类别的两个条目的检查。
你有“熟食”作为价值 - 这将被Split()
Sub Tester()
Dim dict As New Scripting.Dictionary
Dim InStr As String ' Input string
Dim SplitStr() As String ' Array to hold words
Dim SegNum As String ' Output string
Dim I As Long, srt As Long, wd
dict.CompareMode = TextCompare
srt = 1
dict.Add Key:="Dinner", Item:=Array(10, srt)
dict.Add Key:="Lunch", Item:=Array(15, srt)
dict.Add Key:="Breakfast", Item:=Array(20, srt)
dict.Add Key:="Snack", Item:=Array(50, srt)
' Second two digits
srt = 2
dict.Add Key:="Restaurant", Item:=Array(70, srt)
dict.Add Key:="HomeCooked", Item:=Array(80, srt)
' Third two digits
srt = 3
dict.Add Key:="Home", Item:=Array(0, srt)
dict.Add Key:="McDonald's", Item:=Array(1, srt)
dict.Add Key:="Burger King", Item:=Array(2, srt)
dict.Add Key:="Wendy's", Item:=Array(3, srt)
dict.Add Key:="Applebee's", Item:=Array(8, srt)
SegNum = String(6, " ")
SplitStr = Split("dinner home homecooked")
'SplitStr = Split(InputBox("Please use some keywords to detail what you ate"), " ")
For I = LBound(SplitStr) To UBound(SplitStr)
wd = Trim(SplitStr(I))
If dict.Exists(wd) Then
srt = dict(wd)(1)
Mid(SegNum, 1 + ((srt - 1) * 2), 2) = Format(dict(wd)(0), "00")
End If
Next
MsgBox SegNum
End Sub