返回列表 上一主題 發帖

[發問] 如何利用VBA按鍵,將指定日期填滿顏色?

  1. Sub test()
  2. Dim d,d1,m%,n%,i%,j%,Rng, found
  3. Set d = CreateObject("scripting.dictionary")
  4. Set d1 = CreateObject("scripting.dictionary")
  5. Range("c3:f" & Cells(Rows.Count, 3).End(xlUp).Row).Interior.Color = xlNone
  6. For i = 3 To Range("c3").End(xlDown).Row
  7.     d(Cells(i, 3).Value) = ""
  8. Next i
  9. k = d.keys
  10. For i = 0 To UBound(k)
  11.     For j = 3 + n To Range("c3").End(xlDown).Row
  12.         If k(i) = Cells(j, 3) Then
  13.             m = m + 1
  14.             n = n + 1
  15.         Else
  16.             Set Rng = Cells(j - m, 3).Resize(m, 4)
  17.             For Each Cell In Rng.Range(Cells(1, 4), Cells(Rng.Rows.Count, 4))
  18.                 If CDate(Split(Cell.Value, " ")(0)) = Date Then
  19.                     found = True
  20.                     d1(Split(Cell.Value, " ")(0)) = ""
  21.                 Else
  22.                     d1(Split(Cell.Value, " ")(0)) = ""
  23.                 End If
  24.             Next Cell
  25.                 k1 = d1.keys
  26.             Select Case d1.Count
  27.                 Case 1
  28.                     If CDate(k1(0)) = Date Then
  29.                     Rng.Interior.Color = 255
  30.                     End If
  31.                 Case 2
  32.                     If found = True Then
  33.                         For ii = 1 To Rng.Rows.Count
  34.                             If CDate(Split(Rng.Cells(ii, 4), " ")(0)) <> Date Then
  35.                                 Rng.Cells(ii, 1).Resize(1, 4).Interior.Color = 16711935
  36.                             End If
  37.                         Next ii
  38.                     End If
  39.                 Case Else
  40.                     If found = True Then
  41.                         For ii = 1 To Rng.Rows.Count
  42.                             If CDate(Split(Rng.Cells(ii, 4), " ")(0)) = Date Then
  43.                                 Rng.Cells(ii, 1).Resize(1, 4).Interior.Color = 255
  44.                             End If
  45.                         Next ii
  46.                     End If
  47.             End Select
  48.             m = 0
  49.             found = False
  50.             d1.RemoveAll
  51.             Exit For
  52.         End If
  53.     Next j
  54. Next i
  55. If Range("a2") <> "" Then
  56. Set Rng = Range("c3:c" & Cells(Rows.Count, 3).End(xlUp).Row)
  57.     For i = 1 To Rng.Rows.Count
  58.        If Rng.Cells(i) = Range("a2") Then
  59.        Rng.Resize(i, 4).Interior.Color = xlNone
  60.        End If
  61.     Next i
  62. End If
  63. End Sub
複製代碼
回復 1# RCRG

TOP

測試資料&#153219;沒有當天日期,所以不會填充顏色
請自行輸入當天日期,再執行程式

TOP

回復 5# RCRG
我完全沒有考慮到C1儲存格,C1有任何變動是不會影響到程式的
有要做修改的部分,請隨附件上模擬你要的結果,光字面上說明,有
時不一定每個人都能明白你真正的需求,或有時別人會誤解你的恴思

TOP

回復 9# RCRG
看看這樣可以嗎
  1. Sub ex()
  2. Dim d, d1, rng() As Range, arr, i%, j%, rng1 As Range
  3. For i = 3 To Cells(Rows.Count, 3).End(xlUp).Row
  4. If Right(Cells(i, 3), 1) = ChrW(160) Then
  5. Cells(i, 3).Replace ChrW(160), ""
  6. End If
  7. Next i
  8. Set d = CreateObject("scripting.dictionary")
  9. Set d1 = CreateObject("scripting.dictionary")
  10. With Sheets("測試頁")
  11. Set rng1 = .Range("a2:a" & .Cells(Rows.Count, 1).End(xlUp).Row)
  12. arr = .Range("c3:f" & .Cells(Rows.Count, 3).End(xlUp).Row)
  13. .Range("c3:f" & .Cells(Rows.Count, 3).End(xlUp).Row).Interior.Color = xlNone
  14.     For i = 1 To UBound(arr)
  15.         d(arr(i, 1)) = d(arr(i, 1)) + 1
  16.     Next i
  17.     ReDim rng(1 To d.Count)
  18.     t = d.items
  19.     For i = 1 To d.Count
  20.         k = k + t(i - 1)
  21.         Set rng(i) = .Range(.Cells(2 + k - t(i - 1) + 1, 3), .Cells(2 + k, 6))
  22.         If WorksheetFunction.CountIf(rng(i), ">" & .Range("c1")) = rng(i).Rows.Count And _
  23.             WorksheetFunction.CountIf(rng1, rng(i).Cells(1, 1)) = 0 Then
  24.             rng(i).Interior.Color = 255
  25.         ElseIf WorksheetFunction.CountIf(rng(i), ">" & .Range("c1")) < rng(i).Rows.Count And _
  26.                WorksheetFunction.CountIf(rng(i), ">" & .Range("c1")) <> 0 Then
  27.             For j = 1 To rng(i).Rows.Count
  28.                 If rng(i).Cells(j, 4) < Range("c1") Then
  29.                     spr = Split(rng(i).Cells(j, 4), " ")(0)
  30.                     d1(spr) = d1(spr) + 1
  31.                 End If
  32.             Next j
  33.              k1 = d1.keys
  34.              If d1.Count = 1 Then
  35.                For j = 1 To rng(i).Rows.Count
  36.                  If rng(i).Cells(j, 4) < .Range("c1") And WorksheetFunction.CountIf(rng1, rng(i).Cells(1, 1)) = 0 Then
  37.                     rng(i).Rows(j).Interior.Color = 16711935
  38.                  End If
  39.                Next j
  40.              End If
  41.           End If
  42.         d1.RemoveAll
  43.     Next i
  44. End With
  45. End Sub
複製代碼

TOP

回復 14# RCRG

Selection.FormulaR1C1 = ""這行下面再加
Selection.interior.color = xlnone

TOP

        靜思自在 : 稻穗結得越飽滿,越會往下垂,一個人越有成就,就要越有謙沖的胸襟。
返回列表 上一主題