[英]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.