简体   繁体   English

Excel VBA 根据单元格值过滤表格行

[英]Excel VBA Filter out table rows depending on cell value

I'm trying to exclude values from a table depending on values.我试图根据值从表中排除值。

I've got the following table which has columns with names, customer ID, customer city and total spent:我有下表,其中包含名称、客户 ID、客户城市和总支出的列:

初始表

And I would like to filter out the two following specific values, which are customer ID's.我想过滤掉以下两个特定值,它们是客户 ID。

要过滤掉的值

So that the end table would look like this, notice it no longer has Liam Gallagher or Tom Johnson.为了让茶几看起来像这样,请注意它不再有 Liam Gallagher 或 Tom Johnson。

茶几

I know how to filter these out manually but I was wondering how I can do it on VBA?我知道如何手动过滤掉这些,但我想知道如何在 VBA 上进行过滤?

I've scoured the inte.net and I haven't been able to find any solutions.我已经搜索了 inte.net,但未能找到任何解决方案。 Also the two tables will have different arrays as the list of customers can change as well as the customer IDs, so if it's possible to make it dynamic that would be great.此外,这两个表将具有不同的 arrays,因为客户列表和客户 ID 都可以更改,因此如果可以使其动态化,那就太好了。

Thanks谢谢

Sub advanced_filter()
    Dim rgData As Range, rgCriteria As Range, rgOutput As Range
    
    With ThisWorkbook.Worksheets("Sheet 1")
        Set rgData = .Range("A1").CurrentRegion
        Set rgCriteria = .Range("F1").CurrentRegion
        Set rgOutput = .Range("I1")
    
        .Range("I1:L7").ClearContents
    End With
    rgData.AdvancedFilter xlFilterCopy, rgCriteria, rgOutput

End Sub

在此处输入图像描述

UPDATE:更新:
Is there anyway that the customerID's in column F and G无论如何,CustomerID 在 F 和 G 列中
can be stacked vertically instead of horizontally?可以垂直堆叠而不是水平堆叠吗?

You can do this "manually" with vba;-)您可以使用 vba“手动”执行此操作;-)

Sub advanced_filter()
    Dim rgData As Range, rgCriteria As Range, rgOutput As Range
    
    With ThisWorkbook.Worksheets("Sheet 1")
        Set rgData = .Range("A1").CurrentRegion
        Call CopyTranspose
        Set rgCriteria = .Range("A10").CurrentRegion
        Set rgOutput = .Range("H1")
    
        .Range("H1:L7").ClearContents
    End With
    rgData.AdvancedFilter xlFilterCopy, rgCriteria, rgOutput

End Sub

Sub CopyTranspose()
'
' CopyTranspose Macro
'
    Range("A10:A15").EntireRow.ClearContents

    Dim lineCount As Integer
    lineCount = Range("F1").CurrentRegion.Count
    
    'Transpose Copy
    transposeAndPasteCol Range("F1").CurrentRegion, Range("A11")
    
    Set LastCellRange = Range("B10").Offset(0, lineCount - 2)
    Range("F1").Copy Range(Range("B10"), LastCellRange)

    Range("A10:A20").Delete Shift:=xlToLeft
End Sub

Sub transposeAndPasteCol(ColToCopy As Range, pasteRowTarget As Range)
    pasteRowTarget.Resize(, ColToCopy.Rows.Count) = Application.WorksheetFunction.Transpose(ColToCopy.Value)
End Sub

在此处输入图像描述

UPDATE 2:更新 2:

Sub advanced_filter_V4()
    Dim rgData As Range, rgCriteria As Range, rgOutput As Range
    
    With ThisWorkbook.Worksheets("Sheet 1")
        Set rgData = .Range("A1").CurrentRegion
        Call CopyTranspose
        Set rgCriteria = .Range("A15").CurrentRegion
        Set rgOutput = .Range("F1")
    
        .Range("F1:L7").ClearContents
    End With
    rgData.AdvancedFilter xlFilterCopy, rgCriteria, rgOutput

End Sub

Sub CopyTranspose()
    Range("A15:A20").EntireRow.ClearContents
    transposeAndPasteCol Range("A10:B14"), Range("A15")
End Sub

Sub transposeAndPasteCol(ColToCopy As Range, pasteRowTarget As Range)
    pasteRowTarget.Resize(ColToCopy.Columns.Count, ColToCopy.Rows.Count) _
     = Application.WorksheetFunction.Transpose(ColToCopy.Value)
End Sub

在此处输入图像描述

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

相关问题 VBA宏按特定单元格值过滤表并删除所有行 - VBA Macro to filter a table by specific cell value and delete all rows Excel VBA - 根据单元格中的值添加行 - Excel VBA - add rows in dependence of a value in a cell VBA:根据单元格值过滤数据透视表 - VBA: filter pivot table according to cell value 列表中的EXCEL VBA计算总和取决于其他单元格值 - EXCEL VBA calc sum in list depending on other cell value 如何在 Excel VBA 中使用邮件发送多个超链接,具体取决于单元格值 - How to send multiple hyperlinks with mail in Excel VBA, depending on the cell value Access VBA:删除单元格值与Access表中的值匹配的Excel行 - Access VBA: Delete Excel rows where cell value matches value in Access table Excel VBA 根据整个工作表中的多项选择更改单元格值 - Excel VBA to change a cell value depending on multiple selection throughout the sheet 如何根据条件删除表格的行Excel vba - how to delete rows of a table depending on condition Excel vba Excel-VBA:在应用过滤器后获取表中可见单元格的值? - Excel-VBA: Get Value of a Visible Cell in a Table after applying Filter? Excel VBA Loop with Excel Solver具有复制单元格值的能力,具体取决于某些不同的单元格值 - Excel VBA Loop with Excel Solver with copying cell value depending on certain different cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM