Can you help me? I need a VBA code which makes bold a chosen word in a cell

makiwara

Board Regular
Joined
Mar 8, 2018
Messages
171
The sheet's name is: animals

A B

1. lion The lions are beatufil animals.
2. dog Dogland is the home of the animals.

I need a VBA code which examines the words in column "A" and notes the first 2 characters of the word and then if a word in column "B" starts with that 2 characters then the code makes the whole word bold in column B.

Thank you for your time and help, it means to me a lot, if you try to help me!

Have a very nice day! :-)
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try

Code:
Sub hlword()
Dim lr As Long
Dim startlet As String
lr = Sheets("animals").Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lr
    startlet = Left(Sheets("animals").Cells(x, 1), 2)
    p = InStr(Sheets("animals").Cells(x, 2), startlet)
    e = InStr(p + 1, Sheets("animals").Cells(x, 2), " ") - 1
    Sheets("animals").Cells(x, 2).Characters(p, e - p).Font.Bold = True

Next x
 
End Sub
 
Upvote 0
6RG68
Fktacmv



Try

Code:
Sub hlword()
Dim lr As Long
Dim startlet As String
lr = Sheets("animals").Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lr
    startlet = Left(Sheets("animals").Cells(x, 1), 2)
    p = InStr(Sheets("animals").Cells(x, 2), startlet)
    e = InStr(p + 1, Sheets("animals").Cells(x, 2), " ") - 1
    Sheets("animals").Cells(x, 2).Characters(p, e - p).Font.Bold = True

Next x
 
End Sub
 
Upvote 0
The sheet's name is: animals

A B

1. lion The lions are beatufil animals.
2. dog Dogland is the home of the animals.

I need a VBA code which examines the words in column "A" and notes the first 2 characters of the word and then if a word in column "B" starts with that 2 characters then the code makes the whole word bold in column B.
This code will highlight every word, not just the first word) in Column B that starts with the first two letters of the word in Column A...
Code:
[table="width: 500"]
[tr]
	[td]Sub MakeSomeWordsBold()
  Dim R As Long, x As Long, TwoLetters As String, CellText As String
  With Sheets("animals")
    For R = 1 To .Cells(Rows.Count, "A").End(xlUp).Row
      Do
        TwoLetters = Left(.Cells(R, "A").Value, 2)
        CellText = .Cells(R, "B").Value
        x = InStr(x + 1, CellText, TwoLetters, vbTextCompare)
        If x Then .Cells(R, "B").Characters(x, InStr(x, .Cells(R, "B").Value, " ") - x + 1).Font.Bold = True
      Loop While x
    Next
  End With
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Thank you for your fast reply!! I think that just one little thing is missing.
I runned the code and the result is below. (1. row: nothing happened, 2. row: "is" bold, 3. row "t" is not bold, 4. row: perfect)

Do you know, what1s the problem? Or am I doing something wrong? Thank you for your help, I own you one:)

lion The lions are beatuful animals.
dog Dogland is the home of the animals.
cat The cat doesn't like horses.
duck Duccckktessssttt ???
 
Upvote 0
Thank you Rick! I launched your code too, the result is good, but in the first 2 rows now there are 2 more words bold ("are" and "is") Every other row is perfect. Thank you for your time and energy!

lion The lions are beautiful animals.
dog Dogland is the home of the animals.
cat The cat doesn't like horses.
duck Duccckktessssttt ???

This code will highlight every word, not just the first word) in Column B that starts with the first two letters of the word in Column A...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub MakeSomeWordsBold()
  Dim R As Long, x As Long, TwoLetters As String, CellText As String
  With Sheets("animals")
    For R = 1 To .Cells(Rows.Count, "A").End(xlUp).Row
      Do
        TwoLetters = Left(.Cells(R, "A").Value, 2)
        CellText = .Cells(R, "B").Value
        x = InStr(x + 1, CellText, TwoLetters, vbTextCompare)
        If x Then .Cells(R, "B").Characters(x, InStr(x, .Cells(R, "B").Value, " ") - x + 1).Font.Bold = True
      Loop While x
    Next
  End With
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Porblem detected, it was caused by me.

Works perfect! Thank you Rick you are awsome :-)))



Thank you Rick! I launched your code too, the result is good, but in the first 2 rows now there are 2 more words bold ("are" and "is") Every other row is perfect. Thank you for your time and energy!

lion The lions are beautiful animals.
dog Dogland is the home of the animals.
cat The cat doesn't like horses.
duck Duccckktessssttt ???
 
Upvote 0
Thank you Rick! I launched your code too, the result is good, but in the first 2 rows now there are 2 more words bold ("are" and "is") Every other row is perfect. Thank you for your time and energy!

lion The lions are beautiful animals.
dog Dogland is the home of the animals.
cat The cat doesn't like horses.
duck Duccckktessssttt ???
That is occurring because the "space" character after the word you want bolded in Column B is not a true space (ASCII 32), rather, it is a non-breaking space (ASCII 160) which usually results from copying text off of a web page. Try this code instead (it makes all spaces real spaces)...
Code:
[table="width: 500"]
[tr]
	[td]Sub MakeSomeWordsBold()
  Dim R As Long, x As Long, TwoLetters As String, CellText As String
  With Sheets("animals")
    For R = 1 To .Cells(Rows.Count, "A").End(xlUp).Row
      .Cells(R, "B") = Replace(.Cells(R, "B"), Chr(160), " ")
      Do
        TwoLetters = Left(.Cells(R, "A").Value, 2)
        CellText = .Cells(R, "B").Value
        x = InStr(x + 1, CellText, TwoLetters, vbTextCompare)
        If x Then .Cells(R, "B").Characters(x, InStr(x, .Cells(R, "B").Value, " ") - x + 1).Font.Bold = True
      Loop While x
    Next
  End With
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Thank you unique solution! Thank you very much! :-))))


That is occurring because the "space" character after the word you want bolded in Column B is not a true space (ASCII 32), rather, it is a non-breaking space (ASCII 160) which usually results from copying text off of a web page. Try this code instead (it makes all spaces real spaces)...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub MakeSomeWordsBold()
  Dim R As Long, x As Long, TwoLetters As String, CellText As String
  With Sheets("animals")
    For R = 1 To .Cells(Rows.Count, "A").End(xlUp).Row
      .Cells(R, "B") = Replace(.Cells(R, "B"), Chr(160), " ")
      Do
        TwoLetters = Left(.Cells(R, "A").Value, 2)
        CellText = .Cells(R, "B").Value
        x = InStr(x + 1, CellText, TwoLetters, vbTextCompare)
        If x Then .Cells(R, "B").Characters(x, InStr(x, .Cells(R, "B").Value, " ") - x + 1).Font.Bold = True
      Loop While x
    Next
  End With
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top