简体   繁体   English

过滤的单元格未粘贴到新工作表上

[英]Filtered cells not pasting on a new sheet

Stuck at this annoying problem, I want to filter cells from a data in M_Sheet, select the filtered region of cells that have "New Code" in Range("x6") and paste it in a new sheet.卡在这个烦人的问题上,我想从 M_Sheet 中的数据中过滤单元格,select 过滤区域中具有“新代码”的单元格区域(“x6”)并将其粘贴到新工作表中。 For some reasons the cells are not copying or pasting由于某些原因,单元格没有复制或粘贴

Set M_Sheet = Comp_Book.Worksheets("Sheet1")    'Comp_Book is a variable for a book currently open

M_Sheet.range("B6:F1835").AutoFilter Field:=6, Criteria1:="New Code"
M_Sheet.range("B6:F1835").SpecialCells(xlCellTypeVisible).Copy
'A code suggestion needed to paste the copied region in new a worksheet 
 starting from Column B

What i have tried我试过的

 Dim N_Sheet = Comp_Book.Worksheets("Sheet2)
 N_Sheet.cells(2,2).paste

AutoFilter feat.自动筛选壮举。 CurrentRegion and SpecialCells CurrentRegion 和 SpecialCells

The Code编码

Sub AutoFilterRange()
    Dim wb As Workbook: Set wb = Workbooks("Comp.xlsm")
    Dim src As Worksheet: Set src = wb.Worksheets("Sheet1")
    Dim rng As Range: Set rng = src.Range("B6").CurrentRegion
    rng.AutoFilter Field:=6, Criteria1:="New Code"
    Dim tgt As Worksheet: Set tgt = wb.Worksheets.Add
    rng.SpecialCells(xlCellTypeVisible).Copy
    tgt.Paste
    Application.CutCopyMode = False
    ' Show all data.
    'src.ShowAllData
    ' Turn off AutoFilter.
    'rng.AutoFilter
End Sub

This code assigns the range in Comp_Book.Sheet1 and sets a variable to a new worksheet.此代码分配Comp_Book.Sheet1中的范围并将变量设置为新工作表。 It will filter the range for "New Code" in Column G , and then copy visible cells in the range to the new worksheet.它将过滤Column G中“新代码”的范围,然后将该范围中的可见单元格复制到新工作表中。 Comments are provided in the code.代码中提供了注释。

Sub FilterRangeCopytoNewSheet()
    'Define your copy range and destination worksheet variables
    Dim cpyrng As Range: Set cpyrng = Comp_Book.Sheets("Sheet1").Range("B6:F1835")
    Dim destws As Worksheet: Set destws = Comp_Book.Sheets.Add(After:=Sheets(Sheets.Count))
    
        ThisWorkbook.Sheets("Sheet1").AutoFilterMode = False 'Clear any filter
            
            cpyrng.AutoFilter 6, "New Code" 'Field 6 will filter on Column G
                
            cpyrng.SpecialCells(xlCellTypeVisible).Copy destws.Cells(2, 2) 'Copy visible to new sheet
            
        ThisWorkbook.Sheets("Sheet1").AutoFilterMode = False 'Clear filter
End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM