专栏名称: 完美Excel
Excel与VBA技术学习与实践
目录
相关文章推荐
Excel之家ExcelHome  ·  按名称批量插入图片,二分钟搞定 ·  昨天  
Excel之家ExcelHome  ·  高手私藏的几个Excel公式,解决20%办公难题 ·  昨天  
Excel之家ExcelHome  ·  即梦AI:不会PS也能做专业海报 ·  4 天前  
Excel之家ExcelHome  ·  从混合内容中提取中文、英文和数字 ·  2 天前  
Excel之家ExcelHome  ·  四个最受欢迎Excel公式已选出:谁是你的梦 ... ·  3 天前  
51好读  ›  专栏  ›  完美Excel

使用VBA用户窗体自定义模糊匹配查找

完美Excel  · 公众号  · Excel  · 2025-06-09 05:30

正文

请到「今天看啥」查看全文


Private Sub UserForm_Initialize()    Dim catDict As Object    Dim it    Dim i As Long    Set catDict = GetListofCategories()    For Each it In catDict.keys        lbCategory.AddItem it    Next it
    lbCategory.AddItem "其他"     For i = 0 To lbCategory.ListCount - 1        lbCategory.Selected(i) = True    Next i
    tbSelectedQuestion.Text = Selection.ValueEnd Sub
Function GetListofCategories()     Dim question As Range    Dim dict As Object    Dim wsQnA As Worksheet    Set dict = CreateObject("Scripting.Dictionary")    Set wsQnA = GetQnAWorksheet     For Each question In wsQnA.Range("C:C").SpecialCells(xlCellTypeConstants)        If question.Row > 1 Then            If Not dict.Exists(question.Value) Then                dict.Add question.Value, 1            End If        End If    Next question     Set GetListofCategories = dictEnd Function
Function GetQnAWorksheet() As Worksheet    Dim ws As Worksheet    For Each ws In ActiveWorkbook.Sheets        If ws.Name Like "知识库*" And ws.Visible Then            Set GetQnAWorksheet = ws            Exit Function        End If    Next ws    MsgBox "没有找到知识库工作表!", vbCritical + vbOKOnly, "错误"    Set GetQnAWorksheet = NothingEnd Function


删除不必要的词

Function RemoveStopWords(sentence As String) As Collection    If IsEmpty(stopWords) Then stopWords = Split("的;是;如何;什么;所有;?;-;,;", ";")    Dim stopR As Range    Dim r As Variant    Dim col As Collection    Set col = New Collection    Dim w    For Each w In Split(sentence, " ")         If Not (IsNumeric(Trim(w))) Then            w = Trim(w)            If Len(w) > 0 Then col.Add w        End If    Next w    Dim i    For Each r In stopWords        For i = col.Count To 1 Step -1            If UCase(col(i)) = UCase(r) Then                col.Remove i            End If        Next i    Next r    Set RemoveStopWords =






请到「今天看啥」查看全文