简体   繁体   English

将过滤的数据复制到特定的工作表中

[英]Copy filtered data into specific sheet

So i have the following code which uses the advancedfilter function in the excel to filter for me few criteria and then copy this into new workbook wit the name of the criteria. 因此,我有以下代码,该代码使用excel中的advancedfilter函数为我过滤了一些条件,然后将其复制到新工作簿中,并带有条件名称。 What I would like it to do now is to, hmm, lets say filter criteria1, copy it, and instead of creating new workbook and paste it there, I want it to paste it into the current workbook with the same name, but the trick here is that I do not want it to overwrite the current data that i have but to find the last row (i know how to do it) and paste it there. 我现在想做的是,嗯,让我们说说过滤条件1,将其复制,而不是创建新的工作簿并将其粘贴到那里,而是希望它使用相同的名称将其粘贴到当前工作簿中,但是诀窍是这是我不希望它覆盖我拥有的当前数据,而是要找到最后一行(我知道该怎么做)并将其粘贴到那里。

Dim cell As Range
Dim curPat As String

curpath = ActiveWorkbook.Path & "\"

Application.ScreenUpdating = False
Application.DisplayAlerts = False

For Each cell In Range("fbtlist")
    [valsalesman] = cell.Value
    Range("myFBT").AdvancedFilter Action:=xlFilterCopy, _
        criteriarange:=Range("criteria"), copytorange:=("extract"), unique:=False
    Range(Range("extract"), Range("extract").End(xlDown)).Copy
    Workbooks.Add
    ActiveSheet.Paste
    ActiveWorkbook.SaveAs Filename:=curpath & cell.Value & Format(Now, "ddmmyyyy - hhmmss") & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWindow.Close
    Range(Range("extract"), Range("extract").End(xlDown)).ClearContents
Next cell

End Sub

any help or guidance would be appreciated. 任何帮助或指导,将不胜感激。

Hope the below code will match your expectations 希望下面的代码符合您的期望

Dim cell As Range
Dim curPat As String

curpath = ActiveWorkbook.Path & "\"

Application.ScreenUpdating = False
Application.DisplayAlerts = False

For Each cell In Range("fbtlist")
     [valsalesman] = cell.Value
     Range("myFBT").AdvancedFilter Action:=xlFilterCopy, _
    criteriarange:=Range("criteria"), copytorange:=("extract"), unique:=False
Range(Range("extract"), Range("extract").End(xlDown)).Copy
Workbooks.Add  'Instead of creating use the Workbook.open and perform as below
'You may insert this code to find the last used row
a = 2
do while cells(a, 2) <>""
a = a+1
loop
cells(a,1).select
Activesheet.paste
ActiveWorkbook.SaveAs Filename:=curpath & cell.Value & Format(Now, "ddmmyyyy - hhmmss") & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
Range(Range("extract"), Range("extract").End(xlDown)).ClearContents
Next cell

End Sub

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

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