2016-09-26 35 views
0

我想將字體變爲紅色,以便在Excel中出現單詞列表。到目前爲止,我能夠找到一個單詞,但我需要搜索整個數組。我是VBA的新手,並且很掙扎。到目前爲止,我已經能夠發現這是一個解決方案,但它找到一個字符串,「F1」交易:在Excel中格式化文本字符串列表

Sub test4String2color() 
Dim strTest As String 
Dim strLen As Integer 
strTest = Range("F1") 
For Each cell In Range("A1:D100") 
If InStr(cell, strTest) > 0 Then 
cell.Characters(InStr(cell, strTest), strLen).Font.Color = vbRed 
End If 
Next 
End Sub 

編輯:

我需要強調已在項目中列出的細胞用逗號分隔格式。例如,「Apple 1,Apple 3,Banana 4,Orange」。要搜索的值列表位於不同的單元格中,「Apple」,「Banana 4」。我只想突出顯示「Banana 4」,因爲這是與逗號分隔值完全匹配的。在目前的表述中,說「Apple 1」或「Apple 4」的文字會部分突出顯示。

編輯2:

Examples

這是我的工作簿中的實際格式:

Example 3

+0

「......但我需要搜索整個數組」,你是什麼意思?如果我把'Gotham'放在'F1'中,它正確地突出顯示了在'A1:D100'範圍內的所有'Gotham'的發現(它突出顯示「Gotham」以及單元格之後的所有文本)。你是說'F1'可以說'Gotham,City,Wayne Enterprises',並且你想要搜索每一個單詞(用逗號或其他分隔符分隔)? – BruceWayne

+0

你在正確的軌道上。你可以使'strTest'成爲一個Collection並循環遍歷它,以便與第二個For循環匹配。由於您沒有使用任何UDT,集合將很容易設置並循環。 – Tyeler

+0

@BruceWayne我在說我想要尋找Gotham,City,Wayne Enterprises。但是它們全部位於不同的單元格中,而不是以逗號分隔的單個單元格中。我有一個約200個單詞的列表,每個單詞都在我想用作搜索條件的不同單元格中。 – user6883405

回答

2

這是實現通過一系列循環你的願望的方法,集合和數組。

代碼會在集合(您選擇的匹配詞)和數組(每個單元格中分隔的詞的字符串)之間找到匹配項。如果找到匹配項,則會設置字符串中的起始和結束字符,並將這些值之間的字符着色。

Sub ColorMatchingString() 
    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets(1) 
    Dim strTest As Collection: Set strTest = New Collection 
    Dim udRange As Range: Set udRange = ws.Range("AC2:AC311") 'Define Search Ranges 
    Dim myCell, myMatch, myString, i 
    Dim temp() As String, tempLength As Integer, stringLength As Integer 
    Dim startLength as Integer 

    For Each myMatch In udRange 'Build the collection with Search Range Values 
     strTest.Add myMatch.Value 
    Next myMatch 

    For Each myCell In ws.Range("A2:AB1125") 'Loop through each cell in range 
     temp() = Split(myCell.Text, ", ") 'define our temp array as "," delimited 
     startLength = 0 
     stringLength = 0 

     For i = 0 To UBound(temp) 'Loop through each item in temp array 
      tempLength = Len(temp(i)) 
      stringLength = stringLength + tempLength + 2 

      For Each myString In strTest 
    'Below compares the temp array value to the collection value. If matched, color red. 
       If StrComp(temp(i), myString, vbTextCompare) = 0 Then 
        startLength = stringLength - tempLength - 1 
        myCell.Characters(startLength, tempLength).Font.Color = vbRed 
       End If 
      Next myString 
     Next i 
     Erase temp 'Always clear your array when it's defined in a loop 
    Next myCell 
End Sub 
+0

是的!這很好。但我最初忽略了另一層複雜性。我需要突出顯示的單元格以逗號分隔格式列出項目。例如,「Apple 1,Banana 3,Banana,Orange 4」。要搜索的值列表位於不同的單元格中,「Apple」,「Orange」,「Apple 1」,「Banana 3」。我只想突出顯示「Apple 1」和「Banana 3」,因爲這些是與逗號分隔值完全匹配的。 – user6883405

+0

對我的評論不太清楚。這可能更有意義:我需要突出顯示的單元格以逗號分隔格式列出項目。例如,「Apple 1,Apple 3,Banana 4,Orange」。要搜索的值列表位於不同的單元格中,「Apple」,「Banana 4」。我只想突出顯示「Banana 4」,因爲這是與逗號分隔值完全匹配的。在目前的表述中,說「Apple 1」或「Apple 4」的文字會部分突出顯示。 「 – user6883405

+0

@ user6883405我已更新我的代碼以執行所需操作,請檢查結果是否與您期望的一樣... wait nvm。這將適用於您的字符串的任何迭代,給我一點時間.. – Tyeler

1

與你原來的代碼保持一致,你可以添加其他For each cell in Range(和其他一些東西):

Sub test4String2color() 
Dim wb As Workbook 
Dim ws As Worksheet 

Dim strLen As Integer 
Dim i  As Long 
Dim tst As Range 

Set wb = ActiveWorkbook 
Set ws = wb.ActiveSheet 

Dim keyWordRng As Range 
Dim dataRng As Range 
Set keyWordRng = ws.Range("F1:F2") 
Set dataRng = ws.Range("A1:A5") 

For Each tst In keyWordRng 
    Debug.Print "Searching for: " & tst 
    For Each cell In dataRng 
     If tst.Value = cell.Value Then 
      cell.Characters(InStr(cell, tst), strLen).Font.Color = vbRed 
     ElseIf InStr(1, cell.Value, ",") > 0 Then 
      getWordsInCell cell, tst.Value 
     End If 
    Next cell 
Next tst 
End Sub 


Sub getWordsInCell(ByVal cel As Range, keyword As String) 
Dim words() As String 
Dim keywordS As Integer, keywordE As Integer 
words = Split(cel.Value, ",") 

Dim i As Long 
For i = LBound(words) To UBound(words) 
    Debug.Print "Found multiple words - one of them is: " & words(i) 
    If Trim(words(i)) = keyword Then 
     keywordS = ActiveWorkbook.WorksheetFunction.Search(keyword, cel, 1) 
     keywordE = ActiveWorkbook.WorksheetFunction.Search(",", cel, keywordS) 
     cel.Characters(keywordS, (keywordE - keywordS)).Font.Color = vbRed 
    End If 
Next i 

End Sub 

請注意,我加入到範圍(keyWordRngdataRng),你將需要調整您的工作表。這應該(手指交叉)工作!

enter image description here

+0

太棒了!有沒有一種方法只突出顯示完全匹配?我需要突出顯示的單元格以逗號分隔格式列出項目。例如,「Apple 1,Apple 3,Banana 4,Orange」。要搜索的值列表位於不同的單元格中,「Apple」,「Banana 4」。我只想突出顯示「Banana 4」,因爲這是與逗號分隔值完全匹配的。在目前的表述中,說「Apple 1」或「Apple 4」的文字會部分突出顯示。 – user6883405

+0

@ user6883405 - 查看更新的代碼(它只是'如果'行被更新)。從我收集的信息來看,您的範圍F1:H1(例如)在每個單元格中都有一個「關鍵字」,例如「Banana」,「Banana 4」,「Apple」。您的範圍'A1:D100'在單個單元中有一堆字,例如'Banana 4,Apple,Cars'。您**只希望突出顯示單元格,其中*整個單元格與您的關鍵字匹配?因此,如果該單元格僅爲*「香蕉4」,它會突出顯示單元格「C4」? – BruceWayne

+0

差不多!我希望突出顯示的部分具有完全匹配。因此,在您的示例中,Banana 4和Apple將突出顯示。但是,如果它說的是香蕉5,我不想只突出香蕉部分。我只想讓它突出顯示,如果它是逗號分隔值之間的精確匹配。 @BruceWayne – user6883405