返回列表 上一主題 發帖

請問如何計算加總出人名+數字=數字總合

請問如何計算加總出人名+數字=數字總合

請問如何計算加總出人名+數字=數字總合.jpg

回復 1# 阿宇哥

謝謝前輩發表此主題
後學藉此題練習VBA_陣列與字典!
不知道是否會錯題意?
範例請試試看!
20221102_4.zip (52.15 KB)

執行前:


執行結果:


Option Explicit
Sub 字典與陣列練習()
Dim Brr, Crr, c&, i&, x&, xR, Y, U, ERR&
Set Y = CreateObject("Scripting.Dictionary")
Brr = Range([操作表!B2], [操作表!B65536].End(3))
For i = 1 To UBound(Brr)
   If Trim(Brr(i, 1)) = "" Then ERR = 1: GoTo 111
   Crr = Split(Trim(Brr(i, 1)), " ")
   U = Trim(Crr(0)) & Y(Trim(Crr(0)) & i)
   Crr = Split(Replace((Brr(i, 1)), " ", ""), U)
   If UBound(Crr) <= 1 Then ERR = 1: GoTo 111
   For x = UBound(Crr) To 2 Step -1
      If IsNumeric(Trim(Crr(x))) = False Then
         ERR = 1: GoTo 111
      End If
      Y(U & i) = Y(U & i) + Val(Trim(Crr(x)))
   Next
Next

111
If ERR = 1 Then
   Cells(i + 1, 2).Activate
   ActiveCell.Interior.ColorIndex = 38
   MsgBox "請檢查資料!"
   Exit Sub
End If
[C2].Resize(Y.Count, 1) = Application.Transpose(Y.ITEMS)
End Sub

TOP

google"EXCEL迷"  blog  或google網址:https://hcm19522.blogspot.com/

TOP

        靜思自在 : 成功是優點的發揮,失敗是缺點的累積。
返回列表 上一主題