菩薩慈悲:
看是不是您要的結果,再討論。感恩感恩 南無阿彌陀佛
Option Explicit
Sub CountNonEmptyCells()
Dim W, X As Integer, lastColumn As Integer, sum, i As Integer, count As Integer
For X = 2 To 5
lastColumn = Cells(X, Columns.count).End(xlToLeft).Column
For i = 10 To lastColumn
If Cells(X, i) <> "" Then
count = count + 1
sum = sum + Cells(X, i)
End If
Next i
W = sum
MsgBox W
Next X
End Sub
sum是儲存格內數字的總和
count則是有資料的儲存格總數量
看菩薩您的需求是什麼 阿彌陀佛
Option Explicit
Sub CountNonEmptyCells()
Dim W, X As Integer, lastColumn As Integer, sum, i As Integer, count As Integer
For X = 2 To 5
lastColumn = Cells(X, Columns.count).End(xlToLeft).Column
For i = 10 To lastColumn
If Cells(X, i) <> "" Then
count = count + 1
sum = sum + Cells(X, i)
End If
Next i
W = sum
MsgBox "第" & X & "列 總和:" & W & " 有資料的儲存格總數量:" & count
sum = 0: count = 0
Next X
End Sub
感恩chatGPT大菩薩的提示指導。感恩感恩 讚歎讚歎 南無阿彌陀佛