简体   繁体   English

如果单元格包含特定文本,则移动到新工作簿

[英]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.

相关问题 如果单元格包含某些文本,则复制文本的一部分并移至新表 - if cell contains certain text copy part of text and move to new sheet Excel - 如果单元格包含特定文本,则将整行移动到下一个工作表中 - Excel - move a whole row into the next worksheet if cell contains specific text 如果 Excel 2010 中单元格范围内的单元格包含特定文本,请将整个单元格文本移动到不同的列中 - If a cell in range of cells in Excel 2010 contains specific text, move whole cell text into a different column 参考工作簿在其中包含文件名中的特定文本? - Reference workbook where it contains specific text in filename? 如果已填充特定单元格,则将VBA工作表添加到新工作簿 - VBA sheet to new workbook if specific cell populated Excel-如果单元格包含特定文本 - Excel - If cell contains specific text 检查单元格是否包含特定文本 - Check if a cell contains specific text 协助 Excel VBA 参考单元格将工作表移动到新工作簿并保存 - Assistance with Excel VBA Reference Cell to Move Sheet to new workbook and save Excel VBA如果单元格包含整个工作簿中的文本,则删除一行 - Excel vba Deleting a row if a cell contains a text throughout whole workbook 如果单元格包含特定文本,则复制整行 - if cell contains specific text, copy whole row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM