vba 根据列表的多个类别名称筛选复制内容到指定工作表
以下是一个示例代码,可以根据多个类别名称筛选复制内容到指定工作表:
Sub FilterAndCopy()
Dim wsSource As Worksheet Dim wsDest As Worksheet Dim lastRow As Long Dim i As Long Dim category1 As String Dim category2 As String
'设置原始和目标工作表 Set wsSource = ThisWorkbook.Sheets("原始数据") Set wsDest = ThisWorkbook.Sheets("目标数据")
'设置类别名称 category1 = "类别1" category2 = "类别2"
'获取原始工作表的最后一行 lastRow = wsSource.Cells(Rows.Count, "A").End(xlUp).Row
'循环遍历原始工作表 For i = 2 To lastRow '假设第一行为标题行 '如果行的“类别”列包含所需的类别名称,则将其复制到目标工作表 If InStr(1, wsSource.Cells(i, "B").Value, category1, vbTextCompare) > 0 Or _ InStr(1, wsSource.Cells(i, "B").Value, category2, vbTextCompare) > 0 Then '将该行数据复制到目标工作表 wsSource.Rows(i).Copy wsDest.Cells(Rows.Count, "A").End(xlUp).Offset(1) End If Next i
End Sub
在此示例中,我们假设原始数据工作表的第一列是序号,第二列是类别名称,第三列及以后是其他数据。我们希望筛选出包含“类别1”或“类别2”的行,并将其复制到目标数据工作表。您可以根据自己的需求修改代码。
原文地址: https://www.cveoy.top/t/topic/xbL 著作权归作者所有。请勿转载和采集!