简体   繁体   English

VBA 计算一列中不同值的数量,从固定点开始,到最后一个有值的单元格结束

[英]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.

相关问题 计算单元格中的值数 - Count number of values within cell VBA代码中的多个IF语句,用于根据另一列从多个列中提取不同的单元格值。 - Multiple IF statements within VBA code to pull in different cell values from multiple columns based on another column. VBA 使用值设置从单元格到最后一列的范围 - VBA Set range from cell to last column with value 检查哪个是特定列中最后一个具有值的单元格,然后在excel vba中选择其下方的单元格 - Check which is the last cell in a specific column which has a value and select the cell below it in excel vba 如何使用VBA在Excel中从N列开始选择固定数量的相邻列 - How to select a fixed number of adjacent columns starting from N column in excel using VBA Excel:如何根据来自不同列的值计算唯一值的总数 - Excel: How to count total number of unique values based on a value from a different column VBA 代码用于绘制具有给定起点和终点的图形 - VBA code for plotting graph with given starting point and ending point 从单元格B15开始计数B列中的行数 - Count number of row in column B starting from cell B15 计算列中单元格中每个数字的值 - Count Values for Each Number in a cell in a Column 获取范围内最后一个非空单元格的列号(如果值不是唯一的) - Get the column number of the last non-empty cell within a range (if values are not unique)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM