计算VBA中数组中某些字符的数量

你好,所以我对VBA很新,但是在Java和HTML中有一些初学者的类,所以我几乎知道我在做什么。

所以我有一个基于逗号(,)分割的单元格,我想计算某个字符出现的次数,然后在另一个单元格中显示结果。 这样做,同时在这一列中的这些行的列。

例如:y,y,y,n,y =在单元格D6:4中

下面是我用VBA在线了解到的一点点信息生成的代码。

我也看到了这一点的代码,但它非常快速(链接)非常复杂,所以如果有人能解释一个,而不是我会非常感激。

(我试图评论答案,但没有足够的代表这样做)

请给出答案的时候试着解释一下这些方法,我没有理解每一种做法,对于编程和VBA来说都是新的,所以在你的回复中,看看我是否能够通过一些推。

Private Sub CommandButton15_Click() Dim Number As String Dim Yoccur As Integer Dim Noccur As Integer Dim Notapp As Integer Dim length As Integer Dim current As String Dim i As Integer Dim Row As Integer Do While Row < 84 For i = 1 To length 'parse data into a array here 'tempArr = Split(X(lngRow, 2), ",") ' would that work if I tried to split based on the comma? If current = "y" Then Yoccur = Yoccur + 1 If current = "n" Then Noccur = Noccur + 1 If current = "n/a" Then Notapp = Notapp + 1 Next i Wend Range("d45").Value = Yoccur Range("d46").Value = Noccur Range("d47").Value = Notapp End Sub 

下面的代码

  1. 设置从D6到列D最后使用的单元格的范围
  2. 它使用Join将此范围内的所有值连接成单个string( strV
  3. 它通过从未改变的string的长度中删除所有y个字符来减去string的长度(在下面更详细的解释)

    • [a1] = Len(strV) - Len(Replace(strV, "y", vbNullString))y值的个数放入A1
    • [a3] = (Len(strV) - Len(Replace(strV, "n/a", vbNullString))) / 3A2n / a值的个数(需要/3除去3个字符长度不适用作为一个替代)
    • [a2].Value = Len(strV) - Len(Replace(strV, "n", vbNullString)) - [a3].Value]计算n个值的个数 A3n / / a包含一个n

在这里输入图像说明

 Sub QuickDump() Dim rng1 As Range Dim strV As String Set rng1 = Range([d6], Cells(Rows.Count, "d").End(xlUp)) strV = Join(Application.Transpose(rng1.Value), ",") d = Filter(Application.Transpose(rng1.Value), "y", True, vbTextCompare) [a1] = Len(strV) - Len(Replace(strV, "y", vbNullString)) [a3] = (Len(strV) - Len(Replace(strV, "n/a", vbNullString))) / 3 [a2].Value = Len(strV) - Len(Replace(strV, "n", vbNullString)) - [a3].Value End Sub 

下面的代码应该有所帮助。 请注意以下假设:

  • 您的input位于工作表的A列中,跨多行,两行之间没有空格,从第1行开始。
  • “y”的输出列是D,因为“n”是E,“n / a”是F.

 Private Sub CommandButton15_Click() Dim lastRow As Long Dim row As Long Dim c As Range Dim arr() As String Dim s As Variant Dim sumY As Long Dim sumN As Long Dim sumNA As Long ' This is the last row of your input in column A. lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).row For row = 1 To lastRow ' Loop all used rows sumY = 0 ' Reset for every loop. sumN = 0 sumNA = 0 Set c = Cells(row, 1) arr = Split(c, ",") ' Split the value in the cell (row, 1) on comma. ' For each string in the split array, do lower-case comparisons ' for "y", "n" and "n/a" and increment appropriate counters. For Each s In arr If LCase(s) = "y" Then sumY = sumY + 1 ElseIf LCase(s) = "n" Then sumN = sumN + 1 ElseIf LCase(s) = "n/a" Then sumNA = sumNA + 1 Else MsgBox "Unknown value!" ' Sanity test if something is wrong with the input. End If Next ' Assign the sums of "y", "n" and "n/a" to columns 4, 5 and 6 (D, E and F). Cells(row, 4).Value = sumY Cells(row, 5).Value = sumN Cells(row, 6).Value = sumNA Next row End Sub 

我相信这不是“完美的”(现在已经很晚了,或者早了),但它应该起作用,至less应该给你一些关于如何从这里开始的想法。

让我知道你是否需要我解释什么不清楚。


UPDATE

给出与上面相同的假设(您可以通过在列索引编码中replace1到3来将列A更改为C列)以及有关换行符的注释(如果我正确理解),下面的代码现在不仅可以计算每一行,而是计算所有行中“y”,“n”和“n / a”的出现次数,并将结果放在单元格D1中为“y”,E1为“n”,F1为“n / a ”。

 Private Sub CommandButton15_Click() Dim lastRow As Long Dim row As Long Dim c As Range Dim arr() As String Dim s As Variant Dim sumY As Long Dim sumN As Long Dim sumNA As Long ' This is the last row of your input in column A. lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).row For row = 1 To lastRow ' Loop all used rows Set c = Cells(row, 1) arr = Split(c, ",") ' Split the value in the cell (row, 1) on comma. ' For each string in the split array, do lower-case comparisons ' for "y", "n" and "n/a" and increment appropriate counters. For Each s In arr If LCase(s) = "y" Then sumY = sumY + 1 ElseIf LCase(s) = "n" Then sumN = sumN + 1 ElseIf LCase(s) = "n/a" Then sumNA = sumNA + 1 Else MsgBox "Unknown value!" ' Sanity test if something is wrong with the input. End If Next Next row ' Assign the sums of "y", "n" and "n/a" to columns 4, 5 and 6 (D, E and F). Cells(1, 4).Value = sumY Cells(1, 5).Value = sumN Cells(1, 6).Value = sumNA End Sub