[英]If cell contains specific text then move to a new workbook
I have a large dataset with the first column being the variable ClientNames.我有一个大型数据集,第一列是变量 ClientNames。 Some ClientNames have the word "Project" in and I'd like to use VBA to find and select each of the ClientNames which have the word "Project" in and move the entire row for these clients into a new workbook (one workbook with all the clients that have "project" in their ClientName. How can I do this?
一些 ClientNames 中包含“Project”一词,我想使用 VBA 来查找和 select 每个 ClientNames 中包含“Project”一词,并将这些客户的整行移动到一个新工作簿中(一个包含所有客户名称中包含“项目”的客户。我该怎么做?
You can achieve that by using the below VBA code, the below code searches for text "project" in the first columns and then shifts the entire row to new workbook called Project您可以通过使用下面的 VBA 代码来实现这一点,下面的代码在第一列中搜索文本“项目”,然后将整行移动到名为 Project 的新工作簿
Option Explicit
Sub project()
Dim lastrow As Long
Dim count As Long
Dim sht As Worksheet
Set sht = ActiveSheet
Dim dst As Workbook
Set dst = Workbooks.Add
dst.SaveAs Filename:="Project"
dst.Sheets.Add.Name = "Project"
count = 1
sht.Activate
lastrow = sht.Cells(Rows.count, 1).End(xlUp).Row
Dim i As Long
For i = 1 To lastrow
If InStr(1, sht.Cells(i, 1).Value, "Project", vbTextCompare) > 0 Then
sht.Rows(i).Cut
dst.Sheets("Project").Rows(count).Insert Shift:=xlDown
sht.Rows(i).Delete
count = count + 1
i = i - 1
End If
Next i
End Sub
You can use InStr function to know if "Project" is in your cell.您可以使用InStr function 来了解“项目”是否在您的单元格中。 Then you can use entirerow to select your row then copy it to the destination sheet and finally delete the row from source sheet.
然后您可以使用整个行到select您的行,然后将其复制到目标工作表,最后从源工作表中删除该行。
Be careful of your line index which will be need to be updated after deleting your row请注意您的行索引,删除行后需要更新该索引
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.