简体   繁体   English

Advanced.Filter VBA

[英]Advanced.Filter VBA

I have this code so far, only does it for sheet 2, how can I alter this code to include multiple sheets into this? 到目前为止,我已经有了这段代码,仅适用于工作表2,如何更改此代码以在其中包含多个工作表? Complete newb here. 在这里完成newb。 :

Sub extractuniquevalues()

    Sheet1.Range("C:C").AdvancedFilter xlFilterCopy, , Sheet4.Range("C1"), True
    Sheet2.Range("C:C").AdvancedFilter xlFilterCopy, , Sheet4.Range("C1"), True

End Sub

You can do it like that: 您可以这样做:

Filter data in place : 过滤数据到位

Sub extractuniquevalues()
    Dim wks As Excel.Worksheet

    For Each wks In Excel.ActiveWorkbook.Worksheets
        Call wks.Range("C:C").AdvancedFilter(XlFilterAction.xlFilterInPlace, , , True)
    Next wks

End Sub

Filter data and paste them into a new worksheet: 过滤数据并将其粘贴到新的工作表中:

Sub extractuniquevalues2()
    Dim wks As Excel.Worksheet
    Dim wksSummary As Excel.Worksheet
    '----------------------------------------------------------------------------------

    On Error Resume Next
    Set wksSummary = Excel.ThisWorkbook.Worksheets("Unique data")
    On Error GoTo 0

    If wksSummary Is Nothing Then
        Set wksSummary = Excel.ThisWorkbook.Worksheets.Add
        wksSummary.Name = "Unique data"
    End If


    'Iterate through all the worksheets, but skip [Summary] worksheet.
    For Each wks In Excel.ActiveWorkbook.Worksheets

        With wksSummary

            If wks.Name <> .Name Then
                If Application.WorksheetFunction.CountA(wks.Range("C:C")) Then
                    Call wks.Range("C:C").AdvancedFilter(xlFilterCopy, , .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row + 1, 1), True)
                End If
            End If

        End With

    Next wks

End Sub

Unique data from each worksheet are printed in the first column of a new worksheet called Unique data . 每个工作表中的唯一数据都打印在称为“ 唯一数据 ”的新工作表的第一栏中。

This method filters data from each worksheet separately, so if there is for example value A in Sheet1 and value A in Sheet2, there will be two entries A in the result list. 此方法分别从每个工作表中筛选数据,因此,例如,如果在Sheet1中有值A和在Sheet2中有值A ,则结果列表中将有两个条目A


Note that first value is considered to be a header and it can be duplicated in the result list. 请注意,第一个值被视为标题,并且可以在结果列表中重复。

I feel your comments warrant me posting this as an answer so that I may be a bit more thorough. 我认为您的评论可以保证我将其发布为答案,这样我可能会更透彻。 This is meant only to add to the answer provided by mielk! 这仅是为了增加mielk提供的答案!

The object hierarchy in excel is roughly summarized by "An Excel Application owns workbooks. An Excel Workbook owns Worksheets. An Excel Worksheet owns Ranges." excel中的对象层次结构大致由“一个Excel应用程序拥有工作簿。一个Excel工作簿拥有工作表。一个Excel工作表拥有范围”。 For more info on that look here . 有关更多信息,请点击此处

When you click on an excel file to open it you are effectively doing 2 things: 当您单击一个excel文件打开时,您实际上在做两件事:

  1. Starting up an Excel "Application" 启动Excel“应用程序”
  2. Opening up a Workbook that that "Application" will "own" 打开该“应用程序”将“拥有”的工作簿

When you open up subsequent Excel files, Excel will skip step one and simply open a workbook in the Excel Application that is already running. 当您打开后续的Excel文件时,Excel将跳过第一步,仅在已经运行的Excel应用程序中打开一个工作簿。 Note this means that similar to how a Workbook can have many Worksheets a single Excel Application can have multiple Workbooks that belong to it . 请注意,这意味着类似于工作簿可以具有多个工作表的方式,单个Excel应用程序可以具有属于它的多个工作簿

There are multiple ways to access these workbooks in VBA. 在VBA中有多种访问这些工作簿的方法。 One way is to use the application's Workbooks member much like you used a Workbook 's Sheets member to access worksheets. 一种方法是使用应用程序的Workbooks成员,就像您使用Workbook Sheets成员访问工作表一样。 Often though you simply want to access the Workbook that the user is currently editing/working on. 通常,尽管您只是想访问用户当前正在编辑/工作的工作簿。 To do this you can use ActiveWorkbook which is automatically updated for you whenever the user begins work on a different workbook. 为此,您可以使用ActiveWorkbook ,当用户开始在其他工作簿上工作时, ActiveWorkbook会自动为您更新。

Another Workbook you will often want to use is the workbook that "houses" the code you are running. 您经常要使用的另一个工作簿是“容纳”您正在运行的代码的工作簿。 You can do this by using ThisWorkbook . 您可以使用ThisWorkbook做到这一点。 If you open up the VBA editor and look at the project viewer, you can even see a reference to ThisWorkbook ! 如果打开VBA编辑器并查看项目查看器,甚至可以看到对ThisWorkbook的引用! If you want your code to only update/alter the workbook that contains it then ThisWorkbook is the way to go. 如果您希望代码仅更新/更改包含它的工作簿,则可以使用ThisWorkbook

As an example: 举个例子:

Let's say you have a macro to loop through all of the open workbooks and put the number of sheets each Workbook "owns" into some Worksheet in the "master" workbook. 假设您有一个宏可以遍历所有打开的工作簿,并将每个工作簿“拥有”的工作表数放入“主”工作簿中的某些工作表中。

You could do something like this: 您可以执行以下操作:

Sub CountThem()
    Dim wb As Workbook
    Dim outputCell As Range
    Dim nextRow As Integer

    nextRow = 1
    For Each wb In Application.Workbooks
        wb.Activate
        ThisWorkbook.Sheets("MySheet").Cells(nextRow, 1).Value = ActiveWorkbook.Sheets.Count
        nextRow = nextRow + 1
    Next
End Sub

You would put this code as a module in the "Master" workbook. 您可以将此代码作为模块放入“主”工作簿中。

Let me know if this clears things up for you! 让我知道是否可以帮您解决问题! :) :)

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

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