将用户输入与关联的词典文本进行匹配

问题描述 投票:0回答:1

我正在尝试为现有产品列表创建零件编号生成器。

我希望用户在用户表单中输入描述、系列等,并从中生成新的(非重复的)零件号。

零件号遵循以下格式:XXXyyy-WWW
其中 XXX 是零件的三个字母缩写,yyy 是系列号(三个数字),WWW 是部门编号参考。
例如:BRC569-896,其中 BRC 指的是系列号为 569(BRC 产品系列中的下一个可用系列号)的支架,896 是部门参考/来源。

我已经检查了现有的零件号,创建了所用缩写的字典。下面是一个例子:

ASM - Assembly
BRC - Bracket
CAB - Cable
FLG - Flange
FOO - Foot Pad
GLU - Glue/Adhesive
SCR - Screw

缩写不遵循特定模式,例如使用前三个字母等。我认为创建旧列表中使用的缩写的字典将允许 VBA 拾取单词并生成缩写来创建零件号。
例如:在描述中键入“Cable”,并选择“CAB”作为新零件号的开头。

如何创建这个?

有其他方法吗?

excel vba
1个回答
1
投票

假设映射表存储在工作表上。该脚本将映射表加载到 Dict 对象中。

Sub Demo()
    Dim objDic As Object, c As Range
    Dim i As Long, sKey As String, aTxt
    Dim arrData
    Set objDic = CreateObject("scripting.dictionary")
    arrData = Range("A1").CurrentRegion.Value
    For i = LBound(arrData) To UBound(arrData)
        aTxt = Split(arrData(i, 1), " - ")
        sKey = aTxt(1)
        If Not objDic.exists(sKey) Then
            objDic(sKey) = aTxt(0)
        End If
    Next i
    ' Testing code
    For Each c In Range("C2:C3") ' Read description from cell
        sKey = c.Value
        If objDic.exists(sKey) Then
            MsgBox sKey & "'s abbreviation is " & objDic(sKey)
        Else
            MsgBox sKey & " doesn't exists"
        End If
    Next
End Sub

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.