2013-03-18 17 views
0

我一直在嘗試這一段時間。我快到了,但是我的vba技能還沒有完成。我在工作表中有一些文本,我需要搜索和查找某些關鍵字的所有實例。將一個工作表中的關鍵字從第二個工作表中的列表中發光

理想情況下,我會在第二個工作表上的範圍內使用關鍵字。我無法弄清楚。我已經能夠使用以下來搜索一個固定的數組,但無法弄清楚如何進行下一步從工作表中獲取單詞的步驟。

Sub X() 

Dim vntWords As Variant 
Dim lngIndex As Long 
Dim rngFind As Range 
Dim strFirstAddress As String 
Dim lngPos As Long 

vntWords = Array("sales", "job") 

With ActiveSheet.UsedRange 
    For lngIndex = LBound(vntWords) To UBound(vntWords) 
     Set rngFind = .Find(vntWords(lngIndex), LookIn:=xlValues, lookat:=xlPart) 
     If Not rngFind Is Nothing Then 
      strFirstAddress = rngFind.Address 
      Do 
       lngPos = 0 
       Do 
        lngPos = InStr(lngPos + 1, rngFind.Value, vntWords(lngIndex), vbTextCompare) 
        If lngPos > 0 Then 
         With rngFind.Characters(lngPos, Len(vntWords(lngIndex))) 
          .Font.Bold = True 
          .Font.Size = .Font.Size 
          .Font.ColorIndex = 3 
         End With 
        End If 
       Loop While lngPos > 0 
       Set rngFind = .FindNext(rngFind) 
      Loop While rngFind.Address <> strFirstAddress 
     End If 
    Next 
End With 

末次

回答

0

可能會有所幫助你

ThisWorkbook.Sheets("name of sheet with search terms").Range("A1:A") 

這將會給你在A列陣列中的所有值。你可以在你的代碼中使用它。

+0

我用vntWords = ThisWorkbook.Sheets(「Keywords」)。Range(「A1:A」)替換了vntWords = Array(「sales」,「job」)並得到了「Run-time error 1004 Appplicaiton-defined或對象定義的錯誤「,所以似乎沒有工作。 – Swilliford 2013-03-18 14:18:27

+0

在這種情況下,我試圖做的是按名稱獲取一張表,所以請使用最新的VBA語法「工作表」(「關鍵字」)來嘗試。範圍(「A1:A」)' – 2013-03-20 08:58:29

相關問題