大家好,所以我对VBA很新,但是已经学习了一些Java和HTML的初学者课程,所以我几乎知道自己在做什么。
所以我有一个基于逗号(,)分割的单元格 我想计算某个角色的出现次数,然后在另一个单元格中显示该结果。在单列中的这些行的列中执行此操作。
ex: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
答案 0 :(得分:0)
以下代码应该有所帮助。请注意以下假设:
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
我确定它不是“完美的”(它已经很晚了 - 或者很早 - 在这里!)但它应该有用,至少应该给你一些如何从这里开始的想法。
如果您需要我解释任何不清楚的事情,请告诉我。
<强>更新强>
考虑到与上面相同的假设(您可以通过在代码的列索引中替换1到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
答案 1 :(得分:0)
以下代码
D6
到列D
strV
Join
)
它通过从未改变的字符串长度中删除所有 y 字符的字符串长度减去A1中 y 值的数量(更多)详细解释如下)
[a1] = Len(strV) - Len(Replace(strV, "y", vbNullString))
将 y 值的数量放在A1
[a3] = (Len(strV) - Len(Replace(strV, "n/a", vbNullString))) / 3
将 n / a 值的数量放在A2
中(需要/3
才能计算删除3个字符的长度 n / 作为替代品)[a2].Value = Len(strV) - Len(Replace(strV, "n", vbNullString)) - [a3].Value]
计算 n 值的数量 - A3
中 n / a 值的数量(因为 n / 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