標題:
[發問]
連續3週以上資料標示
[打印本頁]
作者:
jcchiang
時間:
2015-2-25 12:43
標題:
連續3週以上資料標示
[attach]20289[/attach]
請教各位先進:
由右邊最後一週算起,連續3週以上的行列,以黃色底色標示,如何以VB完成
作者:
stillfish00
時間:
2015-2-25 19:54
回復
1#
jcchiang
Sub Test()
Dim ar, i As Long, j As Long, cmax As Long
With ActiveSheet
ar = .[A1].Resize(.Cells(.Rows.Count, 1).End(xlUp).Row, .Cells(1, .Columns.Count).End(xlToLeft).Column).value
cmax = UBound(ar, 2)
For i = 2 To UBound(ar)
For j = cmax To 2 Step -1
If ar(i, j) = "" Then Exit For
Next
If cmax - j >= 3 Then .Range(.Cells(i, j + 1), .Cells(i, cmax)).Interior.Color = vbYellow
Next
End With
End Sub
複製代碼
作者:
jcchiang
時間:
2015-2-26 07:57
回復
2#
stillfish00
測試無問題,謝謝大大指教
作者:
GBKEE
時間:
2015-3-1 10:21
回復
2#
stillfish00
For j = cmax To 2 Step -1
If ar(i, j) = "" Then Exit For '沒有資料跳出迴圈
Next
複製代碼
你的程式碼只可標示出由最後往前算連續3週以上資料
如資料有中斷後,再往前還有連續3週以上資料,你的程式碼無法標出儲存格底色
修正一下,可標出資料中不只一個區間有連續3週以上資料儲存格底色
Option Explicit
Sub Ex()
Dim R As Long, C As Long, Cmax As Integer
With ActiveSheet.Range("A1").CurrentRegion
.Cells.Interior.ColorIndex = xlNone
For R = 2 To .Rows.Count
C = 2
Do While C <= .Columns.Count
Cmax = 0
Do While .Cells(R, C + Cmax) <> ""
Cmax = Cmax + 1
Loop
If Cmax >= 3 Then .Cells(R, C).Resize(, Cmax).Interior.Color = vbYellow
C = C + 1 + Cmax
Loop
Next
End With
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)