[英]How to get unique value Counts in Excel VBA?
*Hi All, *大家好,
I need to find count of unique values from multiple fields using VBA SQl method, below i have provided all the information. 我需要使用VBA SQl方法从多个字段中找到唯一值的计数,以下我提供了所有信息。
My data is look like a* 我的数据看起来像一个*
VC SMP# JDEStyle PO# QTY
ARU 10176 AM7619 F117849OG 64
ARU 10176 AM7619 F118215OG 192
ARU 10176 AM7619 F248062OD 336
ARU 653 AE7968 F114004O6 1
ARU 653 AE7968 F241623O4 18
ARU 653 AE7968 F8340O4 1
GOK 2061 SM8360 F248018OD 246
GOK 2061 SM8360 F248019OD 360
GOK 2061 SM8360 F248020OD 774
GOK 2061 SM8360 F248021OD 66
GOK 2061 SM8360 F248022OD 372
GOK 2061 SM8360 F256233OD 120
GOK 2061 SM8360 F256234OD 360
GOK 2061 SM8360 F256235OD 120
GOK 2061 SM8360 F256236OD 360
IND 10176 AB7049 F118324OG 217
IND 10176 AB7049 F258738OD 87
IND 10176 AB7049 F258739OD 540
IND 2068 AB7011 F114006O6 5
IND 2068 AB7011 F241625O4 3
SEP 10313 AL4596 F117270OG 4005
SEP 10313 AL4596 F118173OG 7911
SEP 10313 AL4596 F254362OD 540
SEP 10313 AL4596 F254365OD 150
and my expecting output is 我期望的输出是
VC SMP # JDEStyle PO # QTY
ARU 10176 1 3 592
ARU 653 1 3 20
GOK 2061 1 9 2778
IND 10176 1 3 844
IND 2068 1 2 8
SEP 10313 1 4 12606
But i am getting output like 但是我得到的输出像
VC SMP # JDE Style Count of PO # Sum of QTY
ARU 10176 3 3 592
ARU 653 3 3 20
GOK 2061 9 9 2778
IND 10176 3 3 844
IND 2068 2 2 8
SEP 10313 4 4 12606
and my code is 我的代码是
Sub Quality_audit()
Dim query1 As String
Dim path As String
Dim i As Integer
Dim n As Integer
Dim m As Long
Dim wks As Worksheet
path = "U:\BA\Testing\QA\2014.09.29 SP 15 PROD FLW UP RAW DATA.xlsx"
query1 = "Select distinct [VC], [SMP #], Count([JDEStyle]) as 'JDE', Count ([PO #]) as PO, Sum ([Quantity]) as QTY from [Page 1$] where [SMP #] is not null AND ([VC] <> 'HEA' AND [VC] <> 'JMS' AND [VC] <> 'SJL') group by [VC], [SMP #]"
Set conn = New ADODB.Connection
With conn
.Provider = "microsoft.ace.oledb.12.0;"
.ConnectionString = "Data source = " & path & ";" & _
"extended properties = excel 12.0;"
.Open
End With
Set rs = New ADODB.Recordset
Set rs = conn.Execute(query1)
Workbooks.Add
Range("a2").CopyFromRecordset rs
n = 1
For i = 0 To rs.Fields.Count - 1
Cells(1, n).Value = rs.Fields(i).Name
n = n + 1
Next
rs.Close
conn.Close
end sub
Your help will be appreciated 您的帮助将不胜感激
Well, here is a alternate solution you may wish to explore if your returned records are less than several thousand. 好吧,这是您可能希望探讨的替代解决方案,如果您返回的记录少于几千条。
The formulas are: 公式为:
G2 - =IF(LEN(G1),IFERROR(INDEX(A$2:A$999, MATCH(0, IF(LEN(A$2:A$999), COUNTIFS($G$1:$G1,$A$2:$A$999,$H$1:$H1,$B$2:$B$999), 1),0)),""),"")
Array formula (Ctrl+Shift+Delete), copy to H2. G2-
=IF(LEN(G1),IFERROR(INDEX(A$2:A$999, MATCH(0, IF(LEN(A$2:A$999), COUNTIFS($G$1:$G1,$A$2:$A$999,$H$1:$H1,$B$2:$B$999), 1),0)),""),"")
数组公式(Ctrl + Shift + Delete),复制到H2。
I2 - =IF(LEN($G2),SUMPRODUCT(($A$2:$A$999=$G2)*($B$2:$B$999=$H2)/COUNTIF(C$2:C$999, C$2:C$999&"")),"")
Standard formula, copy to J2. I2-
=IF(LEN($G2),SUMPRODUCT(($A$2:$A$999=$G2)*($B$2:$B$999=$H2)/COUNTIF(C$2:C$999, C$2:C$999&"")),"")
标准公式,复制到J2。
K2 - =IF(LEN(G2),SUMIFS($E$2:$E$999,$A$2:$A$999,$G2,$B$2:$B$999,$H2),"")
Standard formula. K2-
=IF(LEN(G2),SUMIFS($E$2:$E$999,$A$2:$A$999,$G2,$B$2:$B$999,$H2),"")
标准公式。 Fill all five formulas down as necessary to catch the records you require. 根据需要填写所有五个公式,以获取所需的记录。
As you are surely aware, array formulas and SUMPRODUCT
chew up calculation resources remorselessly. 如您所知,数组公式和
SUMPRODUCT
毫不SUMPRODUCT
消耗了计算资源。 However, if a pivot table was ever considered this may be a viable option. 但是,如果曾经考虑过使用数据透视表,则这可能是一个可行的选择。
Is the problem that you're having not getting the distinct counts correct? 您没有获得非重复计数的问题正确吗? The problem would be with the SQL then, yes?
那么问题出在SQL上,是吗? I think your DISTINCT is in the wrong place.
我认为您的DISTINCT位置错误。
Select
vc,
smp,
Count(distinct jdestyle) as 'JDE',
Count (distinct po) as 'PO',
Sum (qty) as 'QTY'
from stuff
group by VC, SMP;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.