[英]VBA Count number of different values within a column, starting from a fixed point, ending at last cell that has a value in it
VBA Hey guys, I just started to work with VBA and i am looking for a solution to my problem. VBA 大家好,我刚开始使用 VBA,我正在寻找解决问题的方法。 The task is to count the number of different Values (Suppliers) inside a column, starting at a fixed point, lets say for column A to start at A5, but end at the last value in that column.
任务是计算列内不同值(供应商)的数量,从固定点开始,假设 A 列从 A5 开始,但以该列的最后一个值结束。 Every time i try to put the range from A5 to last written cell in column AI either get errors or a wrong answer.
每次我尝试将 A5 到最后写入的单元格的范围放在 AI 列中时,要么得到错误,要么得到错误的答案。 Anyone here that can help me out?
这里有人可以帮助我吗?
Sub Count_Values_()
Sheets("Präsentation").Select
Dim dblAnz As Double
Dim rngRange As Range, rngRangeCnt As Range
Set rngRange = Range("A5:A50")
For Each rngRangeCnt In rngRange
dblAnz = dblAnz + 1 / WorksheetFunction.CountIf(rngRange, rngRangeCnt.Text)
Next
Dim Bereich As Range
Dim Zelle As Range
Dim lAnzahl As Long
Set Bereich = Range("A5:A50")
For Each Zelle In Bereich
If Zelle.Font.Bold = True Then
lAnzahl = lAnzahl + 1
End If
Next Zelle
Sheets("Anleitung").Select
Range("F1").Value = dblAnz - lAnzahl - 1
End Sub
This is the code I used before by searching values from A5 to A50 and subtracting all the bold headlines.这是我之前通过搜索从 A5 到 A50 的值并减去所有粗体标题使用的代码。 I also added a -1 as an empty cell counts as a value as well I guess or at least I got the right numbers this way.
我还添加了一个 -1,因为空单元格也算作一个值,我猜或者至少我通过这种方式得到了正确的数字。
Please, try the next way:请尝试下一种方法:
Sub testCountDefinedRange()
Dim sh As Worksheet, startRow As Long
Const searchVal As Double = 25.13 'what is it to be searched for
Set sh = ActiveSheet 'use here the sheet you need
startRow = 5 'the starting row of the range to be searched
Debug.Print WorksheetFunction.CountIf(sh.Range("A" & startRow, sh.Range("A" & sh.rows.count).End(xlUp)), searchVal)
End Sub
But according to community spirit, you should prove your involvement in solving the problem by your own.但根据社区精神,你应该证明你参与解决了你自己的问题。 Even if you cannot show us a working piece of code/formula etc. Anything proving that you did something, at least, some research on the inte.net...
即使你不能向我们展示一段有效的代码/公式等。任何能证明你做了某事的东西,至少,对互联网的一些研究……
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.