Sub ccount()
Dim c As Range, d$, d2$, m%
Application.FindFormat.Font.ColorIndex = 7
Set c = Cells.Find(What:="", After:=[a1], SearchDirection:=xlNext, SearchFormat:=True)
If Not c Is Nothing Then
d = c.Address
Do
d2 = c.Address
m = m + 1
Set c = Cells.Find(What:="", After:=Range(d2), SearchDirection:=xlNext, SearchFormat:=True)
Loop Until c.Address = d
End If
[l1] = m
End Sub作者: freeffly 時間: 2011-5-19 07:43
Sub Ex()
Dim Rng As Range, X As Range, R As Range, Tolta%, A%, i%, ii%
Cells.ClearFormats
Set Rng = Range("B2:I" & [A2].End(xlDown).Row)
For Each R In Rng.Rows
Set Rng = R.Cells(1) R.Cells(1)→這個是指? A = 0
For ii = 2 To 8
If R.Cells(1, ii) <> "" And Rng > R.Cells(1, ii) Then R.Cells(1, ii)這個跟上面問的差異在哪?
Set Rng = R.Cells(1, ii)
R.Cells(1, ii).Font.ColorIndex = 7
Tolta = Tolta + 1
A = A + 1
With R.Cells(1, 10)
.Value = A
.Interior.ColorIndex = 4
End With
ElseIf R.Cells(1, ii) <> "" And Rng < R.Cells(1, ii) Then
Set Rng = R.Cells(1, ii) 這一整句的意思是?
End If
Next
Next
[L1] = Tolta
End Sub
回復 6#freeffly
For Each R In Rng.Rows R-> Rng每一整列範圍
Set Rng = R.Cells(1) R.Cells(1)→ R這列範圍的裡第1個Cell
R.Cells(2)→ R這列範圍的裡第2個Cell
For Each R In Rng.Columns R-> Rng每一整欄範圍
Set Rng = R.Cells(1, ii) R.Cells(1, ii)→ R這欄範圍的裡第1列,第 ii欄的Cell
R.Cells(2, ii)→ R這欄範圍的裡第2列,第 ii欄的Cell作者: freeffly 時間: 2011-5-27 14:53
Sub ex()
[AE:AE] = ""
R = 5'第5列開始
Do Until Cells(R, 2) = ""
Set Rng = Range(Cells(R, 3), Cells(R, "AD"))'C:AD欄的列範圍
i = 1
Do Until Rng(1, i) <> ""'找到該列的第一個數值位置
i = i + 1
Loop
first = Rng(1, i): i = i + 1'把第一個數值記住,準備從第一個數值後開始找數值
For k = i To Rng.Count
If Rng(1, k) > first Then cnt = cnt + 1'如果儲存格數值比前一個數值大,計數的變數就加1
If Rng(1, k) <> "" Then first = Rng(1, k)'如果不是空格,就把儲存格的值記住,準備跟下一個數值做比較
Next
Cells(R, "AE") = cnt: cnt = 0'整列跑完後,就把計數的值填入,然後歸零
R = R + 1'下一列
Loop
End Sub作者: freeffly 時間: 2011-6-6 20:29
假設:For X = 1 To Rng ~ Next X 之內容非常複雜
1. 如果在 For Next Loop 內 Coding 時一時疏忽,同時引用了 Rng 變數並將其
值改變;
2. 之後, 在 For Next Loop 後段處理時,亦同樣引用了 Rng 變數值,
此處是希望引用原先的 Rng 變數值時,他忽略了在 For Next Loop 內
已異動了 Rng 變數值,結果執行發生了不可預期的錯誤。
3. 這種情事常易發生在當模組龐大,處理內容複雜時的不經意處理。