返回列表 上一主題 發帖

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

試試看:
  1. Sub 開始統計()
  2.     Dim Cel As Range, Rng As Range
  3.     Dim FstAddr As String, ndx As Integer, cNum As Integer
  4.     Set Rng = Range("B" & [B21] & ":X" & [C21] & "")
  5.     cNum = 0
  6.     For Each Cel In [G20:L20]
  7.         cNum = Cel.Offset(-1, 0).Interior.ColorIndex
  8.         Cel.Interior.ColorIndex = cNum
  9.         ndx = 0
  10.         On Error GoTo next1
  11.         Rng.Find(What:=Cel, LookIn:=xlFormulas, LookAt:=xlWhole).Activate
  12.         FstAddr = ActiveCell.Address
  13.         Cel.Interior.ColorIndex = cNum
  14.         ActiveCell.Interior.ColorIndex = cNum
  15.         Do
  16.             ndx = ndx + 1
  17.             On Error GoTo next1
  18.             Rng.FindNext(After:=ActiveCell).Activate
  19.             ActiveCell.Interior.ColorIndex = cNum
  20.         Loop Until FstAddr = ActiveCell.Address
  21. next1:
  22.         Cel.Offset(1, 0) = ndx
  23.     Next
  24. End Sub

  25. Private Sub Worksheet_Change(ByVal Target As Range)
  26.     Dim Rng As Range
  27.     Set Rng = Application.Union([B21:C21], [G20:L20])
  28.     If Intersect(Target, Rng) Is Nothing Then Exit Sub
  29.     If Not Intersect(Target, [B21:C21]) Is Nothing Then
  30.         [B2:X18].Interior.ColorIndex = xlNone
  31.         [G20:L20].Interior.ColorIndex = xlNone
  32.         [G21:L21] = ""
  33.         If [B21] > [C21] Then
  34.             MsgBox "注意:" & Chr(10) & "啟始列的值 不可以大於 終止列的值", vbCritical
  35.             Exit Sub
  36.         End If
  37.     End If
  38.     If Not Intersect(Target, [G20:L20]) Is Nothing Then
  39.         [B2:X18].Interior.ColorIndex = xlNone
  40.         [G20:L20].Interior.ColorIndex = xlNone
  41.         [G21:L21] = ""
  42.         [N21] = "= COUNTA(G20:L20)"
  43.         If [N21] <> 6 Then Exit Sub
  44.         [N20] = "=SUMPRODUCT((G20:L20<>"""")/COUNTIF(G20:L20,G20:L20&""""))"
  45.         If [N20] < 6 Then
  46.             MsgBox "注意:" & Chr(10) & "輸入區資料重覆!!", vbCritical
  47.             Exit Sub
  48.         End If
  49.     End If
  50.     開始統計
  51. End Sub
複製代碼
test.gif

TOP

回復 15# s7659109
以#14F 為例回覆:
Q1. 在倒數第4列
(即在49與50列之間)
插入
        On Error Resume Next
        [B2:X18].Find(What:=Target, LookIn:=xlFormulas, LookAt:=xlWhole).Activate
        MsgBox "注意:" & Chr(10) & "資料輸入錯誤!!", vbCritical
        Exit Sub
即可.
Q2. 這是版面設計問題,
何不將輸入區直放到到最下面?
中間列空白列可暫先穏藏?
Q3. 看不出輸入區與順序列顏色有何不一致?
不是有動畫圖可對照嗎?

TOP

回復 15# s7659109
Sorry, Q1 還沒有找到答案, 原想法會進入自我循環, Sorry!!

TOP

回復 18# s7659109
問題2:沒錯, 只要在VBA中相關位址改一改就行了, 而且你的想法(放到最上面)更棒!!
問題3:不是2010的問題, 而是, 因為資料輸入錯誤(也就是前面的Q1問題),
我也不知道要如何改,
目前想到的是改用 CommandButton(被動執行),
不要用 Worksheet_Change(自動執行)才不會掉進自我循環中,
另請高明吧, Sorry!!

TOP

回復 13# s7659109
試試看:
我試過好像沒問題
還是舊的檔, 問題40-標示-1.rar, 但b21,c21 有改
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range, Cel As Range
    Set Rng = Application.Union([B21:C21], [G20:L20])
    If Intersect(Target, Rng) Is Nothing Then Exit Sub
    If Not Intersect(Target, [B21:C21]) Is Nothing Then
        [B2:X18].Interior.ColorIndex = xlNone
        [G20:L20].Interior.ColorIndex = xlNone
        [G21:L21] = ""
        If [B21] > [C21] Then
            MsgBox "注意:" & Chr(10) & "啟始列的值 不可以大於 終止列的值", vbCritical
            Exit Sub
        End If
    End If
    Set Rng = Range("B" & [B21] & ":X" & [C21] & "")
    If Not Intersect(Target, [G20:L20]) Is Nothing Then
        [B2:X18].Interior.ColorIndex = xlNone
        [G20:L20].Interior.ColorIndex = xlNone
        [G21:L21] = ""
        On Error Resume Next
        Set Cel = Rng.Find(What:=Target, LookIn:=xlFormulas, LookAt:=xlWhole)
        If Cel Is Nothing Then
            MsgBox "注意:" & Chr(10) & "資料輸入錯誤!!", vbCritical
            Exit Sub
        End If
        [N21] = "= COUNTA(G20:L20)"
        If [N21] <> 6 Then Exit Sub
        [N20] = "=SUMPRODUCT((G20:L20<>"""")/COUNTIF(G20:L20,G20:L20&""""))"
        If [N20] < 6 Then
            MsgBox "注意:" & Chr(10) & "輸入區資料重覆!!", vbCritical
            Exit Sub
        End If
    End If
    開始統計
End Sub

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

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

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

        靜思自在 : 愛不是要求對方,而是要由自身的付出。
返回列表 上一主題