Excel 查找、选择和复制行

Excel 查找、选择和复制行,excel,vba,Excel,Vba,如何在特定列中找到包含特定字符串的单元格,然后选择整行并使用Excel vba复制该行 我对使用ExcelVBA比较陌生,我自己花了好几天的时间来编写代码。 我正在制作一个工作表,其中不同的数据集被复制到其中。每个数据集都将在列A的某些单元格中包含字符串“page”,但行因数据集而异。我现在需要一些vba代码来首先识别包含字符串“Page”的列A的单元格,然后选择整行并将其复制到表的最后一行(其行数也不同)下面。我已经成功地编写了一些代码,将整行复制到表的底部,这样我也可以重用这些代码,主要问题

如何在特定列中找到包含特定字符串的单元格,然后选择整行并使用Excel vba复制该行

我对使用ExcelVBA比较陌生,我自己花了好几天的时间来编写代码。 我正在制作一个工作表,其中不同的数据集被复制到其中。每个数据集都将在列A的某些单元格中包含字符串“page”,但行因数据集而异。我现在需要一些vba代码来首先识别包含字符串“Page”的列A的单元格,然后选择整行并将其复制到表的最后一行(其行数也不同)下面。我已经成功地编写了一些代码,将整行复制到表的底部,这样我也可以重用这些代码,主要问题是识别包含字符串的正确行。 有人能帮我吗


提前谢谢

下面是一个代码,用于将整个行从名为Sheet1的工作表复制到另一个名为Sheet2的工作表,条件为“Page”

你可以试试:

Sub test()

    Dim strSearch As String
    Dim ColumnNo As Long, LastRow As Long
    Dim rngFound  As Range
    Dim wsDestination As Worksheet, wsSource As Worksheet
    
    'set worksheets
    With ThisWorkbook
        Set wsSource = .Worksheets("Sheet1")
        Set wsDestination = .Worksheets("Overview")
    End With
    
    'Set the value you want to search
    strSearch = "*Page*"
    
    'Set the column you want to seach
    ColumnNo = 1
    
    'Create a with statement to point Sheet1.
    With wsSource
        
        'Search for strSearch in column number ColumnNo
        Set rngFound = .Columns(ColumnNo).Find(strSearch, LookIn:=xlValues, lookat:=xlWhole)
        
        If Not rngFound Is Nothing Then
            LastRow = wsDestination.Cells(wsDestination.Rows.Count, "A").End(xlUp).Row + 1
            'Copy row.
            .Rows(rngFound.Row).EntireRow.Copy
            'Paste row
            wsDestination.Rows(LastRow).PasteSpecial Paste:=xlPasteValues
            'Delete row
            .Rows(rngFound.Row).EntireRow.Delete Shift:=xlUp
        Else
            'Msg box
            MsgBox "Value not found."
        End If
        
    End With
      
End Sub

“我已经写了一些代码”请在上面的问题()中添加您的代码,并描述您在哪里遇到了问题或错误。也要研究如何使用,并尝试使用它来查找字符串“页面”另外,一些关于工作表/数据外观的好例子(或屏幕截图)有助于理解您的问题。你的代码有几个问题:1。您应该使用
Long
数据类型而不是
Integer
,因为最后一行很容易超出
Integer
数据类型的范围。2.您获取
lastrow
值的方式,
Row.Count
将从活动工作表(可能不是工作表1)获取行数。3.您不应该使用
选择
激活
之类的功能。它们减慢了进程,没有实际用途:因此,人们不赞成它们。希望这能帮上大忙谢谢你-它现在似乎正在工作-但是现在我对粘贴部分有一个问题,这就是我在那里遇到的:`如果没有rngFound,那么就什么都不是了.Columns(rngFound.Row).EntireRow.Copy Worksheets(“Overview”).Cells(Rows.Count,1).End(xlUp).offset(1,0).paste特殊xlPasteValues.Columns(rngFound.Row).EntireRow.Select Selection.Delete Shift:=xlUp End如果`它给我一条错误消息说复制的范围和粘贴范围不同,你知道如何解释吗?提前谢谢!!顺便问一下,如何添加代码作为注释?很抱歉,但正如您所看到的,我是一个VBA和stackoverflowHi的绝对初学者,我知道需要一些代码将H2中的值从工作表(3)粘贴到包含字符串“Page”的列M和行。我试图复制和调整您已经编写的代码,但我无法将其应用到世界。如何指定要粘贴M列中的值?
Sub test()

    Dim strSearch As String
    Dim ColumnNo As Long, LastRow As Long
    Dim rngFound  As Range
    Dim wsDestination As Worksheet, wsSource As Worksheet
    
    'set worksheets
    With ThisWorkbook
        Set wsSource = .Worksheets("Sheet1")
        Set wsDestination = .Worksheets("Overview")
    End With
    
    'Set the value you want to search
    strSearch = "*Page*"
    
    'Set the column you want to seach
    ColumnNo = 1
    
    'Create a with statement to point Sheet1.
    With wsSource
        
        'Search for strSearch in column number ColumnNo
        Set rngFound = .Columns(ColumnNo).Find(strSearch, LookIn:=xlValues, lookat:=xlWhole)
        
        If Not rngFound Is Nothing Then
            LastRow = wsDestination.Cells(wsDestination.Rows.Count, "A").End(xlUp).Row + 1
            'Copy row.
            .Rows(rngFound.Row).EntireRow.Copy
            'Paste row
            wsDestination.Rows(LastRow).PasteSpecial Paste:=xlPasteValues
            'Delete row
            .Rows(rngFound.Row).EntireRow.Delete Shift:=xlUp
        Else
            'Msg box
            MsgBox "Value not found."
        End If
        
    End With
      
End Sub