在Excel中计数独特的值,但在VBA中给出类型不匹配错误

发布于 2025-01-22 05:48:32 字数 757 浏览 1 评论 0原文

因此,我正在尝试获得我的数据范围内有多少个唯一类别,并且知道如何在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡Ms空城旧梦 2025-01-29 05:48:32

在VBA中,工作表函数的行为并不完全相同。您可以使用评估,按原样编写公式并使用范围地址。但是,更好的方法是使用scipting.dictionary。请尝试下一个代码,该代码将返回唯一值编号,但每个代码也是这些唯一值以及每个唯一值数字(在“ O:p”列中):

Sub countUnique()
  Dim sh As Worksheet, lastR As Long, arr, i As Long, dict As Object
  
  Set sh = ActiveSheet
  lastR = sh.Range("N" & sh.rows.count).End(xlUp).row
  arr = sh.Range("N2:N" & lastR).value 'place the range in an array for  faster processing
  Set dict = CreateObject("Scripting.Dictionary")
  dict.CompareMode = TextCompare

  For i = 1 To UBound(arr)
    dict(arr(i, 1)) = dict(arr(i, 1)) + 1
  Next i
  
  Debug.Print dict.count & " unique values"
  sh.Range("O2").Resize(dict.count, 1).value = Application.Transpose(dict.Keys)
  sh.Range("P2").Resize(dict.count, 1).value = Application.Transpose(dict.items)
End Sub

evaliate工作表函数,工作表函数,请尝试下一个代码:

Sub CountUniqueEvaluate()
  Dim sh As Worksheet, lastR As Long, rng As Range
  
  Set sh = ActiveSheet
  lastR = sh.Range("N" & sh.rows.count).End(xlUp).row
  Set rng = sh.Range("N2:N" & lastR)
  
   Debug.Print Evaluate("=SUMPRODUCT(1/COUNTIF(" & rng.Address & ", " & rng.Address & "))")
End Sub

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 a Scripting.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"):

Sub countUnique()
  Dim sh As Worksheet, lastR As Long, arr, i As Long, dict As Object
  
  Set sh = ActiveSheet
  lastR = sh.Range("N" & sh.rows.count).End(xlUp).row
  arr = sh.Range("N2:N" & lastR).value 'place the range in an array for  faster processing
  Set dict = CreateObject("Scripting.Dictionary")
  dict.CompareMode = TextCompare

  For i = 1 To UBound(arr)
    dict(arr(i, 1)) = dict(arr(i, 1)) + 1
  Next i
  
  Debug.Print dict.count & " unique values"
  sh.Range("O2").Resize(dict.count, 1).value = Application.Transpose(dict.Keys)
  sh.Range("P2").Resize(dict.count, 1).value = Application.Transpose(dict.items)
End Sub

To Evaluate the worksheet function, please try the next piece of code:

Sub CountUniqueEvaluate()
  Dim sh As Worksheet, lastR As Long, rng As Range
  
  Set sh = ActiveSheet
  lastR = sh.Range("N" & sh.rows.count).End(xlUp).row
  Set rng = sh.Range("N2:N" & lastR)
  
   Debug.Print Evaluate("=SUMPRODUCT(1/COUNTIF(" & rng.Address & ", " & rng.Address & "))")
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文