在Excel中计数独特的值,但在VBA中给出类型不匹配错误
因此,我正在尝试获得我的数据范围内有多少个唯一类别,并且知道如何在Excel中进行操作,
=SUMPRODUCT(1/COUNTIF(général!N2:N229;général!N2:N229))
但是当我尝试通过VBA使用它时,我会收到类型的不匹配错误。
WorksheetFunction.SumProduct(1 / Application.WorksheetFunction.CountIf(Range("N2:N229"), Range("N2:N229")))
x4 = Worksheets("général").Cells(Rows.Count, 14).End(xlUp).Row
'x4 = 229
WorksheetFunction.SumProduct(1 / WorksheetFunction.CountIf(Range("N2:N"& x4), Range("N2:N" & x4)))
WorksheetFunction.SumProduct(1 / WorksheetFunction.CountIf(Worksheets("général").Range("N2:N" & x4), Worksheets("général").Range("N2:N" & x4)))
有人可以帮我吗?这让我发疯了。
So I'm trying to get how many unique categories I have in my data range and I know how to do it in Excel
=SUMPRODUCT(1/COUNTIF(général!N2:N229;général!N2:N229))
but when I try to use it via VBA i get a type mismatch error.
WorksheetFunction.SumProduct(1 / Application.WorksheetFunction.CountIf(Range("N2:N229"), Range("N2:N229")))
x4 = Worksheets("général").Cells(Rows.Count, 14).End(xlUp).Row
'x4=229
WorksheetFunction.SumProduct(1 / WorksheetFunction.CountIf(Range("N2:N"& x4), Range("N2:N" & x4)))
WorksheetFunction.SumProduct(1 / WorksheetFunction.CountIf(Worksheets("général").Range("N2:N" & x4), Worksheets("général").Range("N2:N" & x4)))
Can someone help me? It's driven me insane.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在VBA中,工作表函数的行为并不完全相同。您可以使用
评估
,按原样编写公式并使用范围地址。但是,更好的方法是使用scipting.dictionary
。请尝试下一个代码,该代码将返回唯一值编号,但每个代码也是这些唯一值以及每个唯一值数字(在“ O:p”列中):evaliate
工作表函数,工作表函数,请尝试下一个代码:In VBA the worksheet functions do not behave exactly in the same way. You can use
Evaluate
, writing the formulas as they are and using range addresses. But a better way would be using aScripting.Dictionary
. Please, try the next code, which will return the unique values number, but also which are these unique values and how many per each (in columns "O:P"):To
Evaluate
the worksheet function, please try the next piece of code: