返回列表 上一主題 發帖

[發問] 請問前輩關於VBA 中字串統計問題

回復 1# ii31sakura
  1. Sub Test()
  2.   Dim r As Long, sCond As String
  3.   Dim d, sSN As String, sType As String, max, maxType
  4.   
  5.   Set d = CreateObject("scripting.dictionary")
  6.   
  7.   With Sheets("Data")
  8.     sCond = .[G1].Text
  9.     r = .Cells(.Rows.count, "A").End(xlUp).Row - 1
  10.     If r <= 0 Then Exit Sub
  11.     For Each s In .[A2].Resize(r).Value
  12.       sSN = Split(s, "-")(0)
  13.       sType = Split(s, "-")(1)
  14.       If sSN = sCond Then
  15.         d(s) = d(s) + 1
  16.         If d(s) > max Then max = d(s): maxType = sType
  17.       End If
  18.     Next
  19.    
  20.     .[J1].Value = maxType
  21.     .[J2].Value = max
  22.   End With
  23.   
  24. End Sub
複製代碼

TOP

        靜思自在 : 地上種了菜,就不易長草;心中有善,就不易生惡。
返回列表 上一主題