下面是一个示例代码,可以实现循环遍历工作簿中的每个工作表(除了当前这张),查找包含‘软件工程’的单元格,并将包含该单元格的整行复制到当前工作表:

Sub CopyRows()

    Dim ws As Worksheet
    Dim currentSheet As Worksheet
    Dim searchValue As String
    Dim searchRange As Range
    Dim foundCell As Range
    Dim copyRange As Range
    Dim lastRow As Long
    Dim i As Long
    
    ' 设置要查找的值
    searchValue = '软件工程'
    
    ' 获取当前工作表
    Set currentSheet = ActiveSheet
    
    ' 循环遍历工作簿中的每个工作表
    For Each ws In ThisWorkbook.Worksheets
        
        ' 跳过当前工作表
        If ws.Name <> currentSheet.Name Then
        
            ' 在当前工作表中查找包含搜索值的单元格
            Set searchRange = ws.Cells
            Set foundCell = searchRange.Find(searchValue, LookIn:=xlValues, LookAt:=xlPart)
            
            ' 如果找到了匹配的单元格,则复制整行到当前工作表
            If Not foundCell Is Nothing Then
                
                ' 确定要复制的范围
                lastRow = ws.Cells(ws.Rows.Count, foundCell.Column).End(xlUp).Row
                Set copyRange = ws.Range(ws.Cells(foundCell.Row, 1), ws.Cells(lastRow, ws.Columns.Count))
                
                ' 将范围复制到当前工作表
                i = currentSheet.Cells(currentSheet.Rows.Count, 1).End(xlUp).Row + 1
                copyRange.Copy currentSheet.Cells(i, 1)
                
            End If
            
        End If
        
    Next ws
    
End Sub
VBA 循环遍历工作簿查找包含‘软件工程’的单元格并复制整行

原文地址: https://www.cveoy.top/t/topic/l47j 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录