热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在Excel中针对单词列表搜索列表以过滤掉

我想搜索一个列表以过滤出其中是否包含某些单词。我找到了这个VBA脚本Sub

我想搜索一个列表以过滤出其中是否包含某些单词。

我找到了这个VBA脚本

Sub GetWords()
Dim wrdLRow As Integer
Dim wrdLp As Integer
Dim CommentLrow As Integer
Dim CommentLp As Integer
Dim fndWord As Integer
Dim Sht As Worksheet
On Error Resume Next 'Suppress Errors... for when we don't find a match
'Define worksheet that has data on it....
Set Sht = Sheets("Sheet1")
'Get last row for words based on column A
wrdLRow = Sht.Cells(Rows.Count,"A").End(xlUp).Row
'Get last row for comments based on column C
CommentLrow = Sht.Cells(Rows.Count,"C").End(xlUp).Row
'Loop through lists and find matches....
For CommentLp = 2 To CommentLrow
For wrdLp = 2 To wrdLRow
'Look for word...
fndWord = Application.WorksheetFunction.Search(Sht.Cells(wrdLp,"A"),Sht.Cells(CommentLp,"C"))
'If we found the word....then
If fndWord > 0 Then
Sht.Cells(CommentLp,"D") = Sht.Cells(CommentLp,"D") & "; " & Sht.Cells(wrdLp,"A")
fndWord = 0 'Reset Variable for next loop
End If
Next wrdLp
Sht.Cells(CommentLp,"D") = Mid(Sht.Cells(CommentLp,"D"),3,Len(Sht.Cells(CommentLp,"D")) - 2)
Next CommentLp
End Sub

图片之前

如何在Excel中针对单词列表搜索列表以过滤掉

图片后

如何在Excel中针对单词列表搜索列表以过滤掉

它不仅搜索单词,还搜索单词中的字符。

如果C列中的字符串是“我如何使excel在单词列表中搜索单词?”

在A栏寻找的单词是“ how,ke,cel,abcd,xyz”

然后将返回“ how; ke; cel;”。

我只想要“如何”而不想要字符“ ke; cel;” “ make; excel”一词内。

这可以通过修改此脚本来完成,因此,如果该单词之前或之后包含空格,则它仅返回单词,因此可以将其猜测为整个单词而不是字符。


您可以使用InStr function。请记住,InStr function区分大小写。

以下仅是示例。修改和使用:

Option Explicit
Sub test()
Dim arr_For As Variant,arr_In As Variant
Dim i As Long,j As Long,Position As Long
Dim Valid As Boolean
With ThisWorkbook.Worksheets("Sheet1")
arr_For = .Range("A1:A5")
arr_In = .Range("B1:B5")
For i = LBound(arr_For) To UBound(arr_For)
For j = LBound(arr_In) To UBound(arr_In)
Position = InStr(arr_In(j,1),arr_For(i,1))
If Position > 0 Then
Valid = False
If Position = 1 And Mid(arr_In(j,Position + Len(arr_For(i,1)),1) = " " Then
Valid = True
ElseIf Position > 1 Then
If Position + Len(arr_For(i,1)) = Len(arr_In(j,1)) + 1 And Mid(arr_In(j,Position - 1,1) = " " Then
Valid = True
ElseIf Mid(arr_In(j,1) = " " And Mid(arr_In(j,1) = " " Then
Valid = True
End If
End If
If Valid = True Then
If .Cells(j,3).Value = "" Then
.Cells(j,3).Value = arr_For(i,1)
Else
.Cells(j,3).Value = .Cells(j,3).Value & " ; " & arr_For(i,1)
End If
End If
End If
Next j
Next i
End With
End Sub

,

您可以查看一下并对其进行一些编辑:
enter image description here

=IF(IFERROR(FIND(B$2,$A3,0),1,"")

您可以通过将“,1”更改为“,B $ 2”来返回字母,如果可以考虑使用非vba解决方案,则可以考虑将多个字母串联在一起。

编辑,仅显示其灵活性:
enter image description here

,

尝试以下操作:


样本数据:

enter image description here


示例代码:

Sub GetWords()
Dim lr1 As Long,lr2 As Long,x As Long
Dim arr1 As Variant,arr2 As Variant
Dim hits As String
With Sheet1 'Change according to your sheets CodeName
'Get list of words to check
lr1 = .Cells(.Rows.Count,1).End(xlUp).Row
arr1 = .Range("A2:A" & lr1).Value
'Get list of sentences to check
lr2 = .Cells(.Rows.Count,3).End(xlUp).Row
arr2 = .Range("C2:D" & lr2).Value
'Loop through all sentences in memory
For x = LBound(arr2) To UBound(arr2)
For Each itm In arr1
If IsNumeric(Application.Match(itm,Split(arr2(x," "),0)) Then
If hits <> "" Then
hits = hits & "; " & itm
Else
hits = itm
End If
End If
Next itm
arr2(x,2) = hits
hits = ""
Next x
'Load hits into column D
.Range("C2:D" & lr2).Value = arr2
End With
End Sub


样本结果:

enter image description here


它已经扩展了一些,但我只是想逐步讲解它,并对发生的事情进行一些解释。


推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
  • Tryingtosaveanew,notyetnamed,workbook(thatIaddedusinganothersub)withvariablesforth ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Commit1ced2a7433ea8937a1b260ea65d708f32ca7c95eintroduceda+Clonetraitboundtom ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • SoIhavealoopthatrunsperfectforeventsandonlyshowsfutureposts.TheissueisthatIwould ... [详细]
author-avatar
晴可倾
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有