Burada diğer iş parçacığından neredeyse aynı makro var . Ve şimdi, her kelimeyi kodda yazmak zorunda kalmadan, Mylist'in kodundakiyle aynı olanları yazabileceğim Inputbox'ı eklemeye çalışıyorum. Ancak, renkli sözcükler almak için her bir kelimeyi giriş kutusundan alıntı yapmakta zorluk çektim. Sadece bir kelimeyi renklendirebilirim ve her bir kelimeyi giriş kutusuna ayrı ayrı nasıl alıntılayabileceğime şaşırdım.
İşte orjinal bir diziden gelen kodum:
Option Explicit
Option Compare Text
Sub test()
Dim myList, myColor, myPtn As String, r As Range, m As Object, msg As String, x
' Application.Selection.Font.ColorIndex = xlAutomatic
msg = Application.InputBox("Choose keywords to highlight (max 6) that are separated with commas and space", "Input keywords", , , , , , 2)
myList = VBA.Array(msg) '<-- add more if needed
myColor = VBA.Array(vbRed, vbBlue, vbYellow, vbCyan, vbGreen, vbMagenta) '<-- adjust as per myList(use Color value, not ColorIndex)
myPtn = Join$(myList, Chr(2))
With CreateObject("VBScript.RegExp")
.Global = True
.IgnoreCase = True
.Pattern = "([\^\$\(\)\[\]\*\+\-\?\.\|])"
myPtn = Replace(.Replace(myPtn, "\$1"), Chr(2), "|")
.Pattern = "\b(" & myPtn & ")\b"
For Each r In Application.Selection
If .test(r.Value) Then
For Each m In .Execute(r.Value)
x = Application.Match(m, myList)
If Not IsError(x) Then
r.Characters(m.firstindex + 1, m.Length).Font.Color = myColor(x - 1)
End If
Next
End If
Next
End With
End Sub