返回列表 上一主題 發帖

格式化條件

回復 1# s13983037
試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim Rng As Range
  4.     With Worksheets(1).Columns("A:A")
  5.         .Parent.Activate
  6.         Set Rng = .Range("F1")
  7.         Set Rng = .Range(Rng, Rng.End(xlDown))
  8.         .FormatConditions.Delete
  9.         With .SpecialCells(xlCellTypeConstants)
  10.             .Cells(1).Select
  11.             .FormatConditions.Add Type:=xlExpression, Formula1:="=MATCH(" & .Cells(1).Address(0, 0) & "," & Rng.Address(1, 1) & ",0)>0"
  12.             With .FormatConditions(1).Interior
  13.                 .ColorIndex = 6
  14.                 .PatternColorIndex = xlAutomatic
  15.             End With
  16.         End With
  17.     End With
  18. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 3# s13983037
要說清楚,只要A欄每一儲存格的字串開頭在F欄中有出現就變色
ThisWorkbook物件模組的程式碼
  1. Option Explicit
  2. Private Sub Workbook_Open()
  3.     Run "SHEET1.Auto_Open", Range("A:A")
  4.     'SHEET1: Worksheet(1)->VBA的物件名稱
  5.     '你可修改
  6. End Sub
複製代碼
SHEET1物件模組的程式碼
  1. Option Explicit
  2. Dim Rng As Range, Ar
  3. Private Sub Worksheet_Change(ByVal Target As Range)
  4.     Dim R
  5.     If Rng Is Nothing Then Set_Rng
  6.     If Intersect(Target, Rng) Is Nothing Then
  7.         Auto_Open [a:a]
  8.     ElseIf Not Intersect(Target, Rng) Is Nothing Then
  9.         If Ar(Target.Row, 1) <> Target Then
  10.             With Range("a:a").SpecialCells(xlCellTypeConstants)
  11.                 For Each R In .SpecialCells(xlCellTypeConstants)
  12.                     If UCase(R) Like UCase(Target & "*") Then
  13.                         R.Interior.ColorIndex = 6
  14.                     Else
  15.                         R.Interior.ColorIndex = xlNo
  16.                     End If
  17.                    Next
  18.             End With
  19.         End If
  20.         Ar = Rng.Value
  21.     End If
  22. End Sub
  23. Private Sub Set_Rng()
  24.     Set Rng = Range("F1")
  25.     Set Rng = Range(Rng, Rng.End(xlDown))
  26.     Ar = Rng.Value
  27. End Sub

  28. Private Sub Auto_Open(Sheet_Rng As Range)
  29.     Dim E As Range, R As Range
  30.     Set_Rng
  31.     With Sheet_Rng
  32.         .Interior.ColorIndex = xlNo
  33.         If Application.CountA(.Cells) > 0 Then
  34.             For Each E In Rng
  35.                 For Each R In .SpecialCells(xlCellTypeConstants)
  36.                     If UCase(R) Like UCase(E) & "*" Then
  37.                         R.Interior.ColorIndex = 6
  38.                     End If
  39.                 Next
  40.             Next
  41.         End If
  42.     End With
  43. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 發脾氣是短暫的發瘋。
返回列表 上一主題