简体   繁体   English

根据列中的值将行剪切到新工作表

[英]Cut rows to new sheet based on values in column

I have this list of products, and i want to:我有这个产品清单,我想:

  1. Create new sheets based on the values on column C, if there's already a sheet with the same name as the cell value don't create a new sheet.根据 C 列上的值创建新工作表,如果已经有一个与单元格值同名的工作表,则不要创建新工作表。 (like "Abstract" in my example that already been created for row 2 and doesn't need to created again for row 3) (例如我的示例中的“抽象”已经为第 2 行创建,不需要为第 3 行再次创建)

  2. Cut the entire row to the matching sheet.将整行剪切到匹配的工作表。

  3. Make sure the first row is copied to all sheets.确保将第一行复制到所有工作表。

This is a before picture这是之前的图片

After Pic #1: new sheets created, nothing left on first sheet except the 1st row Pic #1 之后:创建了新的工作表,除了第一行之外,第一张工作表上没有留下任何东西

After Pic #2: the sheet contains 2 products because there were 2 "Abstract" in column C在图片 #2 之后:该工作表包含 2 个产品,因为 C 列中有 2 个“摘要”

After Pic #3: the sheet contain 1 product because there was 1 "Plain" in column C在图片 #3 之后:该工作表包含 1 个产品,因为 C 列中有 1 个“普通”

After Pic #4: the sheet contain 1 product because there was 1 "Shiny" in column C在图片 #4 之后:该表包含 1 个产品,因为 C 列中有 1 个“闪亮”

This will get the job done.这将完成工作。

  • I Named the first sheet to "Worksheet".我将第一张工作表命名为“工作表”。
  • The code is dynamic, so you need to input 2 values by yourself:代码是动态的,所以需要自己输入2个值:

Which range/names that should create the new worksheets:应该创建新工作表的范围/名称:

Set myrange = ThisWorkbook.Sheets("Worksheet").Range("C2:C5") 'Set range that should create the new worksheet list 

and how many columns you want to copy to the new sheets (it makes it more dynamic than take the whole row)以及您想要复制到新工作表的列数(这使它比整行更具动态性)

lastcol = Cells(1, "C").Column 'Set how many column that should be copied to new worksheet

VBA Code: VBA 代码:

Sub AddNewSheetFromRange2()
Dim c As Range
Dim ws As Worksheet
Dim myrange As Range
Dim lastcol As Integer
Dim lrow As Integer
Dim lrow_newsheet As Integer
Dim i As Integer

Set myrange = ThisWorkbook.Sheets("Worksheet").Range("C2:C5") 'Set range that should create the new worksheet list
lastcol = Cells(1, "C").Column 'Set how many column that should be copied to new worksheet

lrow = Cells(Rows.Count, 3).End(xlUp).Row 'find last row for range that should create the new worksheet list

i = 1 'Set first index loop to 1

For Each c In myrange.Cells
i = i + 1 'Create index for each loop, used to know which row that should be copied
    'Debug.Print c 'Print which Sheet Name that will be examine
    Set ws = Nothing
    On Error Resume Next
    Set ws = Worksheets(c.Value)
    On Error GoTo 0
        If ws Is Nothing Then

        With ThisWorkbook
            Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count)) 'Add new sheet after (not before)
            ws.Name = c.Value 'Rename the new sheet
        End With


        Dim WorksheetSheet As Worksheet 'Declare variable for Main worksheet
        Set WorksheetSheet = ActiveWorkbook.Worksheets("Worksheet") 'Name the Main sheet
        Dim NewSheet As Worksheet 'Declare variable for new worksheet
        Set NewSheet = ActiveWorkbook.Worksheets(ws.Name) 'Make all new worksheets dynamic by taking name from range

        'Copy Headers from Main sheet to New Worksheet
        Worksheets("Worksheet").Activate
        ThisWorkbook.Worksheets("Worksheet").Range(Cells(1, 1), Cells(1, 3)).Copy
        Worksheets(ws.Name).Activate
        ThisWorkbook.Worksheets(ws.Name).Range(Cells(1, 1), Cells(1, 3)).PasteSpecial

        'Copy row from Main sheet to New Worksheet
        Worksheets("Worksheet").Activate
        ThisWorkbook.Worksheets("Worksheet").Range(Cells(i, 1), Cells(i, lastcol)).Copy
        Worksheets(ws.Name).Activate
        lrow_newsheet = Cells(Rows.Count, 3).End(xlUp).Row + 1
        ThisWorkbook.Worksheets(ws.Name).Range(Cells(lrow_newsheet, 1), Cells(lrow_newsheet, lastcol)).PasteSpecial

        'Clear row in Main sheet
        Worksheets("Worksheet").Activate
        ThisWorkbook.Worksheets("Worksheet").Range(Cells(i, 1), Cells(i, lastcol)).Clear

        Else

        'If worksheet already exists, then
        'Copy row from Main sheet to existing worksheet with exactly the same name
        Worksheets("Worksheet").Activate
        ThisWorkbook.Worksheets("Worksheet").Range(Cells(i, 1), Cells(i, lastcol)).Copy
        Worksheets(ws.Name).Activate
        lrow_newsheet = Cells(Rows.Count, 3).End(xlUp).Row + 1
        ThisWorkbook.Worksheets(ws.Name).Range(Cells(lrow_newsheet, 1), Cells(lrow_newsheet, lastcol)).PasteSpecial

        'Clear row in Main sheet
        Worksheets("Worksheet").Activate
        ThisWorkbook.Worksheets("Worksheet").Range(Cells(i, 1), Cells(i, lastcol)).Clear

        End If
Next c
End Sub

Visualizing the code in excel you will have to start with this:在 excel 中可视化代码,您必须从以下开始:

在此处输入图片说明

and the final output will be this (the four rows into individual worksheets, if the name already exists, it will add to the already existing worksheet)最终输出将是这个(四行到单独的工作表中,如果名称已经存在,它将添加到已经存在的工作表中)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在2个值之间剪切行并将其粘贴到新的工作表上 - cut rows between 2 values and pasting it on a new sheet 根据列中的值为整个工作表的行着色 - Colour rows of an entire sheet based on values in a column 如何根据单元格值将行 x 次复制到另一个工作表中,并创建一个填充特定内容的新列? - How to copy rows x times based on cell values into another sheet, & create a new column filled with specific content? 使用特定条件将行剪切/粘贴到新工作表 - Cut/paste rows to new sheet with certain criteria 如果D列不为空,则剪切行,并根据C列的内容粘贴到同一张纸的特定位置 - Cut rows if column D is not null and paste in a specific place in the same sheet based on contents in column C Excel宏可根据列值将行复制到新工作表 - Excel macro to copy rows to new sheet(s) based on column value 如何使用列值删除基于Excel工作表的行 - How to delete the rows based in excel sheet using column values 根据不同工作表下一列中的多个单元格值删除行 - Delete Rows based on multiple cell values in a column under a different sheet 在新工作表中剪切复制粘贴并在新列中将状态更新为“已更新” - Cut Copy Paste in new sheet and updates status as “Updated” in new column 在Excel VBA中,根据列值在表格之间复制行 - Copying Rows from sheet to sheet in Excel VBA based upon column values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM