简体   繁体   English

过滤 excel 列并将粘贴复制到另一个新工作表,工作表名称应为过滤器值

[英]Filter a excel column and copy paste to another new sheet and the sheet name should be filter value

I am applying filter to column1 and pasting the data to the new sheet I am trying to add new sheets with the filter names,eg:column 1 have a,b,c and the new sheets should be a,b,c.我正在对 column1 应用过滤器并将数据粘贴到新工作表我正在尝试添加具有过滤器名称的新工作表,例如:第 1 列有 a、b、c,新工作表应该是 a、b、c。 attaching the code I tried.can you please get me out out this problem.附上我试过的代码。你能帮我解决这个问题吗? Thanks in advance.提前致谢。

Sub filter()`enter code here`
    Application.ScreenUpdating = False
    Dim x As Range
    Dim rng As Range
    Dim last As Long
    Dim sht As String

    'specify sheet name in which the data is stored
    sht = "DATA Sheet"

    'change filter column in the following code
    last = Sheets(sht).Cells(Rows.Count, "F").End(xlUp).Row
    Set rng = Sheets(sht).Range("A1:F" & last)

    Sheets(sht).Range("F1:F" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AA1"), Unique:=True

    For Each x In Range([AA2], Cells(Rows.Count, "AA").End(xlUp))
    With rng
    .AutoFilter
    .AutoFilter field:=6, Criteria1:=x.Value
    .SpecialCells(xlCellTypeVisible).Copy
    i = x
    For i = 2 To last
    Sheets.Add(after:=Sheets(Sheets.Count)).Name = Range("a" & x).Value
    'Sheets.Add.Name = Range("a3").Value
    Next i
    ActiveSheet.Paste

    End With
    Next x

    ' Turn off filter
    Sheets(sht).AutoFilterMode = False

    With Application
    .CutCopyMode = False
    .ScreenUpdating = True
    End With

End Sub

Put the filter on Column A if that is where the names are.如果 A 列是名称所在的位置,则将过滤器放在 A 列上。

Option Explicit

Sub FilterToSheet()

    Dim rng As Range, rngCopy As Range, x, arNames
    Dim last As Long, sht As String, n As Long

    'specify sheet name in which the data is stored
    sht = "DATA Sheet"

    ' filter on column A
    With Sheets(sht)
        .AutoFilterMode = False
        
        last = .Cells(Rows.Count, "A").End(xlUp).Row
        Set rng = .Range("A1:F" & last)
        .Columns("AA:AA").Clear
        .Range("A1:A" & last).AdvancedFilter Action:=xlFilterCopy, _
           CopyToRange:=.Range("AA1"), Unique:=True
        last = .Cells(Rows.Count, "AA").End(xlUp).Row
        
        ' list of filter names
        arNames = .Range("AA2:AA" & last).Value
        .Columns("AA:AA").Clear
    End With
    
    Application.ScreenUpdating = False
    
    ' aply filter for each name
    For Each x In arNames
        
        With rng
            .AutoFilter
            .AutoFilter field:=1, Criteria1:=x
            Set rngCopy = .SpecialCells(xlCellTypeVisible)
        End With
        
        Sheets.Add(after:=Sheets(Sheets.Count)).Name = x
        rngCopy.Copy
        ActiveSheet.Paste
        ActiveSheet.Range("A1").Select
        n = n + 1
        
    Next x

    ' Turn off filter
    Sheets(sht).AutoFilterMode = False
    Sheets(sht).Activate

    With Application
        .CutCopyMode = False
        .ScreenUpdating = True
    End With
    MsgBox n & " sheets created", vbInformation
End Sub

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

相关问题 VBA 用于过滤的宏,从列中复制指定值并创建然后粘贴到具有该列名称的新工作表中 - VBA Macro to filter, copy the specified value from a column and create then paste in a new sheet with that column name 过滤每个值,将每个表复制并粘贴到新工作表中 - Filter each value, copy and paste each table into new sheet 复制并粘贴到另一个工作表Excel vba中的新列 - Copy and paste to new column in another sheet Excel vba 如何筛选表格并将值粘贴到Excel VBA中的另一个工作表中 - How to filter the table and paste the value in another sheet in Excel VBA Excel VBA:过滤、剪切并粘贴到另一个工作表 - Excel VBA: Filter, cut, and paste to another sheet Excel:从另一个工作表中的值筛选工作表 - Excel: Filter a sheet from value in another sheet 在清除VBA中另一个工作表中的过滤器后,将数据复制并粘贴到新工作表中 - Copy and paste data in a new sheet after clearing the filter in another sheet in VBA Excel 将数据从 1 个工作表复制剪切粘贴到另一个工作表中,状态在工作表 2 新列中更新 - Excel copy cut paste data from 1 sheet to another with a status in updated in sheet 2 new column Excel筛选到新工作表 - Excel filter to new sheet EXCEL VBA处理数量数据,如何更快地过滤数据并将粘贴复制到另一张纸上? - EXCEL VBA to deal amount data , how to filter data and copy paste to another sheet faster?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM