PDA

View Full Version : VBA: color given data with given color.



jadgon
07-10-2017, 04:12 PM
VBA: color given data with given color.

Sheet named "USA" has multiple columns. In each columns I have data ( with or with out gap strings or numbers). I have colored each column data with a different color. Say "Data in Column A is blue", "Data in Column C is Red" , "Data in Column B data is Green"........ upto any column. Any column data will not match with one another.

Now in multiple sheets if there is any data appears anywhere and if any string or number matched with data ( with or with out gap strings or numbers) from Sheet named "USA" then color the appearing string or number with the given color.

SamT
07-10-2017, 04:58 PM
Some Text


3





This is some text


1234


Like this?

jadgon
07-10-2017, 05:18 PM
Yes.

SamT
07-10-2017, 07:09 PM
wow

p45cal
07-11-2017, 01:31 AM
Click the button in the attached. Case insensitive.
Macro code is:
Sub blah()
For Each cll In Sheets("USA").UsedRange.Cells
myPhrase = Trim(cll.Value)
phraseLength = Len(myPhrase)
If phraseLength > 0 Then
clr = cll.Font.Color
For Each sht In ThisWorkbook.Sheets
If sht.Name <> "USA" Then
For Each celle In sht.UsedRange
x = InStr(1, celle.Value, myPhrase, vbTextCompare)
If x > 0 Then celle.Characters(Start:=x, Length:=phraseLength).Font.Color = clr
Next celle
End If
Next sht
End If
Next cll
End Sub

jadgon
07-11-2017, 06:13 PM
Thank you PASCAL.
And if I also want to see the partially matching with regex.

p45cal
07-11-2017, 10:36 PM
Supply the regex code and examples of what you're after. Say, in a workbook.

jadgon
07-14-2017, 06:57 PM
Simply say I want to match upto 3 letters only and want to color with below plan. [Gaps in-between, before and after is considered]

Multi ply = Multi ply
Multiply = Multi pl
Multiply = Multi p
Multiply = Multi
Multiply = Mult
Multiply = Mul

jadgon
07-17-2017, 08:48 PM
I request another VBA code to consider color up to 4 charterer match.

p45cal
07-18-2017, 03:14 AM
Supply the regex code and examples of what you're after. For example a few cells showing the text before highlighting, some cells showing what you're looking for, and some clls where you've manually highlighted the corresponding parts - in a workbook.
I can't make any sense out of your msg#8.
The code I supplied will allow you to consider any number of charterers match.

jadgon
07-18-2017, 09:43 AM
Here it is..

p45cal
07-18-2017, 02:14 PM
try:
Sub blah2()
For Each cll In Sheets("USA").UsedRange.Cells
myPhrase = Trim(cll.Value)
phraseLength = Len(myPhrase)
For j = phraseLength To 4 Step -1
For i = 1 To phraseLength - 3
myNewPhrase = Trim(Mid(myPhrase, i, j))
myNewPhraselength = Len(myNewPhrase)
If myNewPhraselength > 0 Then
clr = cll.Font.Color
For Each sht In ThisWorkbook.Sheets
If sht.Name <> "USA" Then
For Each celle In sht.UsedRange
x = InStr(1, celle.Value, myNewPhrase, vbTextCompare)
If x > 0 Then
celle.Characters(Start:=x, Length:=myNewPhraselength).Font.Color = clr
Do
x = InStr(x + 1, celle.Value, myNewPhrase, vbTextCompare)
If x > 0 Then celle.Characters(Start:=x, Length:=myNewPhraselength).Font.Color = clr
Loop Until x = 0
End If
Next celle
End If
Next sht
End If
Next i
Next j
Next cll
End Sub
It uses a brute force technique so be prepared to wait (15 secs here).

jadgon
07-19-2017, 09:49 PM
Thank you.....
Then I want to extract them in Canada sheet...please have a look.

jadgon
07-26-2017, 02:57 AM
Can we get this as well as in the excel file ?

jadgon
07-28-2017, 06:34 PM
Hello pascal one last issue. I have attached a file in 2nd last post.

jadgon
08-01-2017, 02:58 PM
Hello I am looking for a VBA code for last attachment p45cal (http://www.vbaexpress.com/forum/member.php?3494-p45cal)

jadgon
01-08-2019, 01:27 AM
Can I get all words or shared words in cells that are in color (partial/full) in cells of a new sheet?