[英]Cut rows to new sheet based on values in column
I have this list of products, and i want to:我有这个产品清单,我想:
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 行再次创建)
Cut the entire row to the matching sheet.将整行剪切到匹配的工作表。
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.这将完成工作。
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.