如果在单元格中找到一个特定的单词,则将行复制到另一个纸张

发布于 2025-01-19 07:29:26 字数 915 浏览 0 评论 0原文

有没有办法在单元格中查找特定单词,即使它在更多文本中 我一直在下面尝试,但只有当单元格中存在的唯一数据是单词时它才有效。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文