vba:使用表并删除具有条件的重复项
我想使用VBA将一个表中的多列复制到另一个表中(删除重复项并应用一些约束)。所有这些,如果可能的话,以表格的形式 我对vba非常陌生,我不知道这是否可行,但我需要的是从下面获取独特的产品商店组合,以便销售额>0vba:使用表并删除具有条件的重复项,vba,excel,Vba,Excel,我想使用VBA将一个表中的多列复制到另一个表中(删除重复项并应用一些约束)。所有这些,如果可能的话,以表格的形式 我对vba非常陌生,我不知道这是否可行,但我需要的是从下面获取独特的产品商店组合,以便销售额>0 Product Store day sales Apple A monday 3 Apple A tuesday 0 Apple A wednesday 4 Apple B thursday 7 Pe
Product Store day sales
Apple A monday 3
Apple A tuesday 0
Apple A wednesday 4
Apple B thursday 7
Pear A monday 3
Pear C tuesday 0
因此,结果应该是:
Product Store
Apple A
Apple B
Pear A
我已经试着记录宏,但结果真的很长
顺便说一句,数据非常大,所以我认为逐行进行不是一种选择。试试这个
Sub FilterAndCopy()
Columns("A:D").Select 'Change to your actual cells that holds the data
Selection.AutoFilter
Columns("A:B").Select 'Change to your columns that holds the Products and Store data
ActiveSheet.Range("$A:$B").RemoveDuplicates Columns:=Array(1, 2), Header :=xlNo 'Change to your columns that holds the Products and Store data
Range("A1", Cells(Cells(2, 1).End(xlDown).Row, 2)).Select 'Change to your columns that holds the Products and Store data
Selection.Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
End Sub
以下代码应该有帮助:
Option Explicit
Sub Demo()
Application.ScreenUpdating = False 'stop screen flickering
Application.Calculation = xlCalculationManual 'prevent calculation while execution
Dim i As Long, lastrow As Long
Dim dict As Object
Dim ws As Worksheet
Set dict = CreateObject("Scripting.Dictionary")
Set ws = ThisWorkbook.Worksheets("Sheet1") 'change Sheet1 to your worksheet
With ws
lastrow = .Range("A" & .Rows.Count).End(xlUp).Row 'get last row with data from Column A
'get unique records for product and store combined together
For i = 2 To lastrow
If .Cells(i, 4).Value <> 0 Then 'consider product only if sales is not 0
dict(.Cells(i, 1).Value & "|" & .Cells(i, 2).Value) = dict(.Cells(i, 1).Value & "|" & .Cells(i, 2).Value)
End If
Next
With .Range("F2").Resize(dict.Count) 'unique product and store will be displayed from cell F2
.Value = Application.Transpose(dict.Keys)
.TextToColumns Destination:=.Cells, DataType:=xlDelimited, Other:=True, OtherChar:="|"
.Offset(, 2).Resize(dict.Count).Value = Application.Transpose(dict.Items)
End With
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
选项显式
副演示()
Application.ScreenUpdate=False“停止屏幕闪烁”
Application.Calculation=xlCalculationManual“在执行时阻止计算”
朦胧的我一样长,最后一排一样长
作为对象的Dim dict
将ws设置为工作表
Set dict=CreateObject(“Scripting.Dictionary”)
设置ws=ThisWorkbook.Worksheets(“Sheet1”)将Sheet1更改为工作表
与ws
lastrow=.Range(“A”&.Rows.Count).End(xlUp.Row)”使用列A中的数据获取最后一行
'将产品和商店的独特记录合并在一起
对于i=2到最后一行
如果.Cells(i,4).Value为0,则“仅当销售额不为0时才考虑产品”
dict(.Cells(i,1).Value&“|”和.Cells(i,2.Value)=dict(.Cells(i,1.Value&“|”和.Cells(i,2.Value)
如果结束
下一个
将从单元格F2显示带有.Range(“F2”).Resize(dict.Count)”的唯一产品和存储
.Value=应用程序转置(dict.Keys)
.TextToColumns目标:=.Cells,数据类型:=xlDelimited,其他:=True,其他字符:=“|”
.Offset(,2).Resize(dict.Count).Value=Application.Transpose(dict.Items)
以
以
Application.ScreenUpdating=True
Application.Calculation=xlCalculationAutomatic
端接头
输出如下:
如果宏的结果很长,但可以运行,则不需要解决方案,只需对代码进行改进。如果需要帮助,则需要提供更多信息,包括生成的宏代码,以及宏的可用和不可用内容。@cdom-这有帮助吗?