如果在单元格中找到一个特定的单词,则将行复制到另一个纸张
有没有办法在单元格中查找特定单词,即使它在更多文本中 我一直在下面尝试,但只有当单元格中存在的唯一数据是单词时它才有效。
Sub Tucana()
Dim sh1 As Worksheet, sh2 As Worksheet, rng As Range, cel As Range
Dim rngCopy As Range, lastR1 As Long, lastR2 As Long
Dim strSearch1 As String, strSearch2 As String
strSearch1 = "Tucana"
strSearch2 = "Tuc"
Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")
lastR1 = sh1.Range("A" & Rows.Count).End(xlUp).Row
lastR2 = sh2.Range("A" & Rows.Count).End(xlUp).Row + 1
Set rng = sh1.Range("A1:A" & lastR1)
For Each cel In rng.Cells
If cel.Value = strSearch1 Or cel.Value = strSearch2 Then
If rngCopy Is Nothing Then
Set rngCopy = sh1.Rows(cel.Row)
Else
Set rngCopy = Union(rngCopy, sh1.Rows(cel.Row))
End If
End If
Next
If Not rngCopy Is Nothing Then
rngCopy.Copy Destination:=sh2.Cells(lastR2, 1)
End If
End Sub
is there a way to look for specific word in cells even if it's within more text
i've been trying below but it only works if the only data exsits within a cell is the word.
Sub Tucana()
Dim sh1 As Worksheet, sh2 As Worksheet, rng As Range, cel As Range
Dim rngCopy As Range, lastR1 As Long, lastR2 As Long
Dim strSearch1 As String, strSearch2 As String
strSearch1 = "Tucana"
strSearch2 = "Tuc"
Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")
lastR1 = sh1.Range("A" & Rows.Count).End(xlUp).Row
lastR2 = sh2.Range("A" & Rows.Count).End(xlUp).Row + 1
Set rng = sh1.Range("A1:A" & lastR1)
For Each cel In rng.Cells
If cel.Value = strSearch1 Or cel.Value = strSearch2 Then
If rngCopy Is Nothing Then
Set rngCopy = sh1.Rows(cel.Row)
Else
Set rngCopy = Union(rngCopy, sh1.Rows(cel.Row))
End If
End If
Next
If Not rngCopy Is Nothing Then
rngCopy.Copy Destination:=sh2.Cells(lastR2, 1)
End If
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论