【问题标题】:Move rows containing a word from a list to a new sheet将包含单词的行从列表移动到新工作表
【发布时间】:2022-06-24 21:47:21
【问题描述】:

我期待:

  1. 在第一个工作表(右侧)之后创建一个名为“结果”的新工作表
  2. 在原始工作表搜索 B 列中,查找单元格中是否存在任何单词数组
  3. 如果找到,选择该行并将其移动到名为 results 的工作表中
  4. 从移动的行中删除空白空间

例如,在列中搜索包含“casual”且一个单元格包含“Casual Worker”的任何单元格。
我希望宏选择该行并将其剪切并粘贴到结果表中,然后返回并删除空单元格。

我认为单词的数组最适合,因为单词列表可能会发生变化。

工作表始终在同一列中包含信息,因此它的列 b 始终包含单词。

如果 b 列仅包含单词但 99% 的时间它是文本字符串的一部分,我可以编写宏。

我找到了在原始工作表之后创建新工作表的代码,然后将其命名。然后我找到了如何从 B 中选择单词并使其选择行并将其移动到另一张表。

它主要是设置数组,然后在每个单元格的一行文本中搜索特定的单词。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您可以使用InStr() 在较长的字符串中搜索字符串。然后,我将遍历您工作簿中的一个范围,以获取您要搜索的单词列表。根据您对所需内容的描述,我可能会使用类似以下的宏。

    Private Sub SearchAndMove()
        
        Dim SearchSheetName As String
        Dim NewSheetName As String
        Dim SearchRng As Range
        Dim SearchWords() As Variant
        Dim i As Long
        Dim c As Range
        
        Application.ScreenUpdating = False
        
        'Name of original sheet that will be searched
        SearchSheetName = "Search"
        'Name of new sheet for search results
        NewSheetName = "Results"
        
        'Add new sheet if it doesn't already exist
        If Not SheetExists(NewSheetName) Then
            Sheets.Add After:=Sheets(SearchSheetName)
            ActiveSheet.Name = NewSheetName
            Range("A1") = "Results"
        End If
        
        Sheets(SearchSheetName).Activate
        Set SearchRng = Range(Range("B1"), Range("B1").End(xlDown)).Cells
        SearchWords = Array("Word1", "Word2", "Word3")
        
        'Loop through each cell and move to new tab where there is a match to our lookup words
        For i = 0 To UBound(SearchWords)
            For Each c In SearchRng
                If InStr(1, c.Value2, SearchWords(i), vbTextCompare) > 0 Then 'Use vbBinaryCompare if you want the comparison to be case sensitive
                    Sheets(NewSheetName).Range("A1").End(xlDown).End(xlDown).End(xlUp).Offset(1) = c.Value2
                    c.Delete Shift:=xlUp
                End If
            Next c
        Next i
        
        Application.ScreenUpdating = True
    End Sub
    
    'Function to check if a sheet exists
    'Returns TRUE if the sheet exists in the active workbook
    Function SheetExists(SheetName As String) As Boolean
        SheetExists = False
        On Error GoTo NoSuchSheet
        If Len(Sheets(SheetName).Name) > 0 Then
            SheetExists = True
            Exit Function
        End If
    NoSuchSheet:
    End Function
    

    【讨论】:

    • 非常感谢。真的很感激。我唯一的问题是在哪里放置搜索词。我看到了“搜索词”范围,但不确定在哪里添加这些词。或者如何在列表中格式化它们。单词列表是否应该只有逗号或下划线之间的空格。抱歉,如果您的笔记已经提到这一点
    • 对不起,实际上我看到了设置单词的代码。只是仍然没有 100% 了解如何写。有空格等
    • 我在工作簿中添加了一列,用于列出我要搜索的单词,每个单元格中一个单词。我将该范围命名为“SearchWords”以使其更易于引用,然后遍历该范围以检查每个单词。
    • 这个范围的单词可以在任何一张纸上吗?或者 fors 代码只是寻找标题搜索词
    • 有没有办法在代码中定义它们,而不是在工作表上。如果没有,我将只制作一个宏来自动编写单词以保存拼写错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多