返回列表 上一主題 發帖

區域內顯示輸入值填滿顏色之問題

程式碼貼上,出現下述畫面!

區域.png (66.84 KB)

區域.png

希望支持!

TOP

試試看(完整版):
  1. Option Explicit
  2. Sub 開始統計()
  3.     Dim Cel As Range, Rng As Range
  4.     Dim FstAddr As String
  5.     Dim ndx As Integer, i As Integer, cNum As Integer
  6.     Set Rng = Range("B" & [B3] & ":X" & [C3] & "")   '設定啟始列與終止列之間的 小搜尋範圍
  7.     '先搜尋大範圍
  8.     For Each Cel In [G2:L2]
  9.         On Error Resume Next         '忽略錯誤繼續執行 VBA 代碼, 避免出現錯誤消息
  10.         Set Cel = [B5:X21].Find(What:=Cel, LookIn:=xlFormulas, LookAt:=xlWhole)   '設定[B5:X21]為搜尋範圍
  11.         If Cel Is Nothing Then
  12.             MsgBox "注意:" & Chr(10) & "資料輸入錯誤!!", vbCritical
  13.             Exit Sub
  14.         End If
  15.     Next
  16.     '再搜尋小範圍
  17.     Set Rng = Range("B" & [B3] & ":X" & [C3] & "")   '設定啟始列與終止列之為搜尋範圍
  18.     Rng.Select
  19.     For i = 7 To 10
  20.         With Selection.Borders(i)
  21.             .LineStyle = xlContinuous
  22.             .Weight = xlMedium
  23.         End With
  24.     Next
  25.     For Each Cel In [G2:L2]
  26.         Cel.Activate
  27.         cNum = Cel.Offset(-1, 0).Interior.ColorIndex
  28.         Cel.Interior.ColorIndex = cNum
  29.         ndx = 0
  30.         On Error Resume Next         '忽略錯誤繼續執行 VBA 代碼, 避免出現錯誤消息
  31.         Rng.Find(What:=Cel, LookIn:=xlFormulas, LookAt:=xlWhole).Activate
  32.         If ActiveCell.Address = Cel.Address Then GoTo next1   '如果原地踏就是找不到
  33.         '如果有找到, ... ...
  34.         ActiveCell.Interior.ColorIndex = cNum
  35.         FstAddr = ActiveCell.Address
  36.         Do
  37.             ndx = ndx + 1
  38.             Rng.FindNext(After:=ActiveCell).Activate    '繼續找下一個
  39.             ActiveCell.Interior.ColorIndex = cNum
  40.         Loop Until FstAddr = ActiveCell.Address         '直到回到第一次找到的儲存格
  41. next1:
  42.         Cel.Offset(1, 0) = ndx    '統計值寫入 ndx, 換下一格
  43.     Next
  44. End Sub

  45. Sub 清除底色_格線及統計結果()
  46.     Dim i As Integer
  47.     [B5:X21].Select
  48.     For i = 3 To 4
  49.         Selection.Borders(i).LineStyle = xlNone
  50.     Next
  51.     For i = 7 To 10
  52.         Selection.Borders(i).LineStyle = xlNone
  53.     Next
  54.     Selection.Interior.ColorIndex = xlNone
  55.     [G3:L3] = ""
  56. End Sub

  57. Private Sub Worksheet_Change(ByVal Target As Range)    'Target就是獨動 Worksheet_Change 的Range
  58.     Dim Rng As Range, Cel As Range
  59.     Set Rng = Application.Union([B3:C3], [G2:L2])   '設定 判斷條件 及輸入區 為獨動 Worksheet_Change 的範圍
  60.     If Intersect(Target, Rng) Is Nothing Then Exit Sub   '如果不在獨動範圍, 離開
  61.     If Target.Count > 1 Then Exit Sub       '一次改變太多格, 離開
  62.     清除底色_格線及統計結果
  63.     If Not Intersect(Target, [B3:C3]) Is Nothing Then   '如果獨動範圍為 判斷條件區
  64.         If [B3] > [C3] Then      '如果 啟始列的值 大於 終止列的值
  65.             MsgBox "注意:" & Chr(10) & "啟始列的值 不可以大於 終止列的值!!", vbCritical
  66.             Exit Sub
  67.         End If
  68.     End If
  69.     If WorksheetFunction.CountA([G2:L2]) < 6 Then Exit Sub    '如果輸入區未滿6格, 離開
  70.     [N2] = "=SUMPRODUCT((G2:L2<>"""")/COUNTIF(G2:L2,G2:L2&""""))"   '計算[G2:L2]的不重覆格有幾格
  71.     If [N2] < 6 Then        '如果不重覆格不到6格(注意輸入區已滿6格), 警告並離開
  72.         MsgBox "注意:" & Chr(10) & "輸入區資料重覆!!", vbCritical
  73.         Exit Sub
  74.     End If
  75.     開始統計
  76. End Sub
複製代碼
test.gif
顯示輸入值填滿顏色.rar (19.08 KB)

TOP

謝謝,還滿完整,但最後一個請求:
1.倘列數再增加(如再增100列,甚至1000列,甚至列自動增加時,搜尋列數自動跟著增加,並建議表單選項拿掉,因列數大長時,實在不好用)如何調整。
2.搜尋區域增加為2區

1041119顯示輸入值填滿顏色.rar (14.6 KB)

希望支持!

TOP

回復 23# s7659109
Sorry, 太難了!!

TOP

謝謝yen956努力到此,但還是希望有人幫忙最後提出的問題!
希望支持!

TOP

回復 25# s7659109
(這一兩天老是回錯主題, 連貼兩次都貼錯地方, 真奇怪!),
經准大再三指正後, 總算完成了!!
試試看:
  1. Option Explicit
  2. Public inAllNum As Integer
  3. Public inNowNum As Integer

  4. Function BigRng() As Range
  5.     Dim rL As String, I As Integer
  6.     rL = Split([B5].End(xlToRight).Address, "$")(1)
  7.     Set BigRng = Range("B5:" & rL & [B5].End(xlDown).Row & "")
  8.     BigRng.Select
  9.     Selection.Interior.ColorIndex = xlNone    '清除底色
  10.     For I = 1 To 4
  11.         Selection.Borders(I).LineStyle = xlNone   '清除格線
  12.     Next
  13. '    For I = 7 To 10
  14. '        Selection.Borders(I).LineStyle = xlNone
  15. '    Next
  16. End Function

  17. Function SmallRng() As Range
  18.     Dim rL As String, I As Integer
  19.     rL = Split([B5].End(xlToRight).Address, "$")(1)
  20.     Set SmallRng = Range("B" & [D2] & ":" & rL & [D3] & "")    '設定啟始列與終止列之為搜尋範圍
  21. End Function

  22. Function inputRng() As Range
  23.     Dim rL As String, cNumAr
  24.     Dim str1 As String, I As Integer
  25.     cNumAr = Array(4, 6, 7, 8, 15, 17, 19, 20, 22, 24, 33, 35, 36, 37, 39, 40)  '挑選淺色系
  26.     Rows("1:3").Interior.ColorIndex = xlNone     '清除輸入區底色
  27.     rL = Split([G1].End(xlToRight).Address, "$")(1)
  28.     Range("G3:" & rL & "3") = ""    '清除統計結果
  29.     inAllNum = [G1].End(xlToRight).Column - 6    '輸入區共有幾格
  30.     inNowNum = [G2].End(xlToRight).Column - 6    '目前已經輸入幾格
  31.     [F1].FormulaR1C1 = "=SUMPRODUCT((R[1]C[1]:R[1]C[" & inAllNum & "]<>"""")/COUNTIF(R[1]C[1]:R[1]C[" & inAllNum & "],R[1]C[1]:R[1]C[" & inAllNum & "]&""""))"
  32.     For I = 7 To 6 + inAllNum
  33.         Cells(1, I).Resize(3).Interior.ColorIndex = cNumAr(I - 7)
  34.     Next
  35.     str1 = "G2:" & rL & "2"
  36.     Set inputRng = Range(str1)
  37. End Function

  38. Sub 開始統計()
  39.     Dim inRng As Range, bRng As Range, sRng As Range
  40.     Dim Rng As Range, Cel As Range
  41.     Dim FstAddr As String
  42.     Dim ndx As Integer, cNum As Integer
  43.     Set bRng = BigRng
  44.     Set sRng = SmallRng
  45.     Set inRng = inputRng
  46.    
  47.     '先搜尋大範圍
  48.     For Each Cel In inRng
  49.         If Cel = "" Then Exit For
  50.         On Error Resume Next         '忽略錯誤繼續執行 VBA 代碼, 避免出現錯誤消息
  51.         Set Cel = bRng.Find(What:=Cel, LookAt:=xlWhole)   '在大範圍中搜尋
  52.         If Cel Is Nothing Then
  53.             MsgBox "注意:" & Chr(10) & "資料輸入錯誤!!" & Chr(10) & "請修正!!", vbCritical
  54.             Exit Sub
  55.         End If
  56.     Next
  57.     '再搜尋小範圍
  58.     For Each Cel In inRng
  59.         Cel.Activate
  60.         cNum = Cel.Interior.ColorIndex
  61.         ndx = 0
  62.         On Error Resume Next         '忽略錯誤繼續執行 VBA 代碼, 避免出現錯誤消息
  63.         sRng.Find(What:=Cel, LookAt:=xlWhole).Activate
  64.         If ActiveCell.Address = Cel.Address Then GoTo next1   '如果原地踏就是找不到
  65.         '如果有找到, ... ...
  66.         ActiveCell.Interior.ColorIndex = cNum
  67.         FstAddr = ActiveCell.Address
  68.         Do
  69.             ndx = ndx + 1
  70.             sRng.FindNext(After:=ActiveCell).Activate   '繼續找下一個
  71.             ActiveCell.Interior.ColorIndex = cNum
  72.         Loop Until FstAddr = ActiveCell.Address         '直到回到第一次找到的儲存格
  73. next1:
  74.         Cel.Offset(1, 0) = ndx    '統計值寫入 ndx, 換下一格
  75.     Next
  76. End Sub
  77. Private Sub CommandButton1_Click()
  78.     Dim Rng As Range, inRng As Range, bRng As Range, sRng As Range
  79.     Dim I As Integer
  80.     Set bRng = BigRng
  81.     Set sRng = SmallRng
  82.     Set inRng = inputRng
  83.    
  84.     If Val([D2]) < 5 Then
  85.         MsgBox "注意:" & Chr(10) & "判斷條件的啟始列的值 不可小於 5!!", vbCritical
  86.         Exit Sub
  87.     End If
  88.     If Val([D3]) > [B5].End(xlDown).Row Then
  89.         MsgBox "注意:" & Chr(10) & "判斷條件的終止列的值 不可大於" & [B5].End(xlDown).Row & "!!", vbCritical
  90.         Exit Sub
  91.     End If
  92.     If Val([D2]) > Val([D3]) Then      '如果 啟始列的值 大於 終止列的值
  93.         MsgBox "注意:" & Chr(10) & "啟始列的值 不可以大於 終止列的值!!", vbCritical
  94.         Exit Sub
  95.     End If
  96.     If inNowNum < inAllNum Then
  97.         MsgBox "注意:" & Chr(10) & "輸入區尚未填满前," & Chr(10) & "請勿按【開始統計】!!", vbCritical
  98.         Exit Sub     '如果輸入區未滿格, 離開
  99.     End If
  100.     If Val([F1]) < inNowNum Then
  101.         MsgBox "注意:" & Chr(10) & "輸入區的值重覆," & Chr(10) & "請修正!!", vbCritical
  102.         Exit Sub
  103.     End If
  104.     開始統計
  105.     SmallRng.Select
  106.     For I = 7 To 10
  107.         With Selection.Borders(I)      '畫格線
  108.             .LineStyle = xlContinuous
  109.             .Weight = xlMedium
  110.         End With
  111.     Next
  112. End Sub
複製代碼
  
顯示輸入值填滿顏色2.rar (28.32 KB)

TOP

test.gif

TOP

又有進階問題請教,分為輸入區與顯示區,其他功能
好用請保持。

顯示輸入值填滿顏色-進階問題1041126.rar (24.15 KB)

希望支持!

TOP

        靜思自在 : 【行善要及時】行善要及時,功德要持續。如燒開水一般,未燒開之前千萬不要停熄火候,否則重來就太費事了。
返回列表 上一主題