Excel宏查找文本并将结果存储在新工作表中

标签 excel vba

我真的是 VBA 的新手,所以我的专业知识很少。我有三列A,B,C

我的目标:
是遍历整个列 C 并查找特定字符串。
如果它相等,那么我想取一个相应的行,包括(列A,B)并将其逐行存储在(Sheet2)中:

这是我正在尝试的,请帮助。

 Sub FindString()
    Dim cell As Range
    Dim DataCount As Integer
    With Worksheets("Sheet1")
        DataCount = Range("L" & Rows.Count).End(xlUp).Row
        For Each cell In Range("C1:C" & DataCount)
            If InStr(cell.Value, "cmt") > 0 Then
                  MsgBox "The String is Found"
            End If
        Next cell
    End With
End Sub

最佳答案

缺少一些信息,但这会让您接近。由于您已经知道何时找到该字符串,因此它将复制整行并将其粘贴到 Sheet2 中。 i添加以帮助遍历找到的行,因此它们不会相互粘贴,但可以更改。

Sub FindString()
Dim cell As Range
Dim DataCount As Integer
Dim i As Integer

i = 1
With Worksheets("Sheet1")
    'Changed to column C instead
    DataCount = Range("C" & Rows.Count).End(xlUp).Row

    For Each cell In Range("C1:C" & DataCount)
        If InStr(cell.Value, "cmt") > 0 Then
              MsgBox "The String is Found"
              Rows(cell.Row).Copy Sheets("Sheet2").Cells(i, 1)
              i = i + 1
        End If
    Next cell
End With
End Sub

关于Excel宏查找文本并将结果存储在新工作表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57563161/

相关文章:

excel - 如何从一个 Excel 工作簿调用另一个 Excel 工作簿中特定于工作表的宏?

excel - JExcelApiExporter 和 JRXlsExporter 之间的区别

ms-access - Access 报告 "Can Grow"属性需要影响相邻控件

java - LibreOffice UNO : Setting Styles (anwer can be provided in Java, VB、Python、C++,任何使用 UNO API 的语言)

excel - 在 VBA 中使用 Excel 的内置 "My Table has Headers"功能

excel - 我可以让这个宏更快吗?

vba - 当用户窗体打开时单击单元格时,用单元格引用填充文本框

excel - 从自定义应用程序获取数据到 EXcel

excel - 计算具有相同背景颜色的单元格列表

excel - VBA Excel 中的弹出图表