2015-05-26 38 views
1

在Excel片1不匹配的細胞我有一個名爲PHONETYPE其具有在每個單元中一些字符串列。高亮使用VBA

我在同一個excel工作簿中有工作表2,列名爲允許的電話號碼以及每個單元格中的一些字符串。

現在我想如果片1Phonetype列中的字符串相同的字符串中片2allowed phonetype柱進行比較;如果不突出顯示那些細胞。

一切使用VBA。

Sheet 1       Sheet 2 
column name:"Phonetype"    columnname:"allowed phone type" 
cell 1:welcome      cell 1:welcome 
cell 2:        cell 2:hi121 
cell 3:heythere 
cell 4:hi121 

字符串 「heythere」 不存在於表2(柱: 「允許手機型」),所以應該強調

+1

你做了什麼?什麼不起作用?郵政編碼,你應該顯示你已經嘗試過。只要你知道,像這樣漂亮的類似的問題已被要求,並回答了很多次所以儘量搜索功能。 –

回答

0

這裏的東西,讓你開始

Option Explicit 
'// Campare and Hilight Unique 
Sub CompareHighlightUnique() 
    Dim Range1  As Range 
    Dim Range2  As Range 
    Dim i   As Integer 
    Dim j   As Integer 
    Dim isMatch  As Boolean 

    For i = 2 To Sheets("Sheet1").Range("A" & .Rows.Count).End(xlUp).Row 
     isMatch = False 
     Set Range1 = Sheets("Sheet1").Range("A" & i) 
     For j = 1 To Sheets("Sheet2").Range("A" & .Rows.Count).End(xlUp).Row 
      Set Range2 = Sheets("Sheet2").Range("A" & j) 
      If StrComp(Trim(Range1.Text), Trim(Range2.Text), vbTextCompare) = 0 Then 
       isMatch = True 
       Exit For 
      End If 
      Set Range2 = Nothing 
     Next j 
     If Not isMatch Then 
      Range1.Interior.Color = RGB(255, 0, 0) 
     End If 
     Set Range1 = Nothing 
    Next i 
End Sub 

要更改高亮顏色編輯RGB(255, 0, 0)

改變工作表Sheet1 Sheet2的或編輯("Sheet1") and ("Sheet2")

0

檢查出來,,

Sub Button1_Click() 
    Dim ws As Worksheet, sh As Worksheet 
    Dim Rws As Long, Rng As Range, a As Range 
    Dim Rws2 As Long, rng2 As Range, c As Range 

    Set ws = Sheets("Sheet1") 
    Set sh = Sheets("Sheet2") 

    With ws 
     Rws = .Cells(.Rows.Count, "A").End(xlUp).Row 
     Set Rng = Range(.Cells(2, 1), .Cells(Rws, 1)) 
     Rng.Interior.ColorIndex = 6 
    End With 

    With sh 
     Rws2 = .Cells(.Rows.Count, "A").End(xlUp).Row 
     Set rng2 = Range(.Cells(2, 1), .Cells(Rws2, 1)) 
    End With 

    For Each a In Rng.Cells 
     For Each c In rng2.Cells 
      If a = c Then a.Interior.Color = xlNone 
     Next c 
    Next a 

End Sub 

Found here,

+0

在片材2,我有一個名爲「允許PHONETYPE」列包含字符串例如列表。小區1 = ABC,小區2 = DEF等等。現在,在下的列「PHONETYPE」片材1,該細胞含有樣細胞1 = ABC串,小區3 = GHF等我有以突出細胞如果在片材1的串不等於表2中的任何字符串。你能修改你的代碼嗎? –

+0

請看看這個http://stackoverflow.com/questions/30474144/how-to-loop-through-numbers-in-vba?noredirect=1#comment49039212_30474144 –