Consulting

Results 1 to 4 of 4

Thread: find the text in the cell in column A within the text in column B in the same row

  1. #1

    find the text in the cell in column A within the text in column B in the same row

    Hi,

    I have written a macro but it doesn’t work.
    Could you please correct the macro?

    The macro can perform this task:

    I will select some cells in column A.
    The macro will find the text in the cell in column A within the text in column B in the same row.

    For example, if cell A1’s value is John, then the word – John in cell B1 will be bold.
    For example, if cell A2’s value is May, then the word – May in cell B2 will be bold.

    Please see the picture and code below.

    Thanks

    [vba]
    Sub bold()

    For Each x In Selection

    Set i = Application.WorksheetFunction.Find(x.Value, x.Offset(0, 1).Value)
    j = Len(x.Value)

    With x.Offset(0, 1).Characters(Start:=i, Length:=j).Font
    .FontStyle = "bold"
    End With

    Next

    End Sub
    [/vba]
    Attached Images Attached Images

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,954
    Location
    [VBA]Option Explicit

    Sub BoldParts()
    Dim cell As Range, acell As Range
    Dim i As Integer

    For Each cell In Selection
    Set acell = cell.Offset(0, 1)
    i = InStr(1, acell.Value2, cell.Value2, vbBinaryCompare)
    If i > 0 Then acell.Characters(i, Len(cell.Value2)).Font.FontStyle = "Bold"
    Next cell
    End Sub[/VBA]

  3. #3
    Quote Originally Posted by Kenneth Hobs
    [vba]Option Explicit

    Sub BoldParts()
    Dim cell As Range, acell As Range
    Dim i As Integer

    For Each cell In Selection
    Set acell = cell.Offset(0, 1)
    i = InStr(1, acell.Value2, cell.Value2, vbBinaryCompare)
    If i > 0 Then acell.Characters(i, Len(cell.Value2)).Font.FontStyle = "Bold"
    Next cell
    End Sub[/vba]
    hi

    thank you, you code is perfect.

    can I ask you a further question?

    In you macro, you set:
    acell = cell.Offset(0, 1)

    In my macro, I set:
    i = Application.WorksheetFunction.Find(x.Value, x.Offset(0, 1).Value)

    However, my method is not successful.
    Excel say "object required" for the Find function.

    could you please explain any mistake I have made, and how to correct?

    thanks

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,366
    Location
    Hi there,

    You were trying to use the Set Statement to Set a reference to i, which is not an Object (such as a cell or a range, worksheet, etc), but rather, a Value (a number indicating at which character the worksheet function found the start of the string being looked for).

    Does that make sense?

    Mark

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •