Solved

Excel VBA - Highlight specific characters in a string

Posted on 2016-11-05
5
42 Views
Last Modified: 2016-11-17
I have this in a cell formatted Wingdings2 (example)
Activecell.Value = Chr(188) & Chr(189) & Chr(154) & Chr(152)

Edit: It goes that as a UDF (   =GetAttenanceString("C10") for example  )
It works OK when the string is actually entered in the cell. Mystery!


Where the character is Chr(154) I want to color it Red. I'm doing it like this. BUT, although the If condition seems to work the entire string is being colored Red, rather than just the Chr(154). What am I missing here please?

Sub BlankNonAttendingDays()

    For iDay = 1 To 7
        If Mid(ActiveCell.Value, iDay, 1) = Chr(154) Then
            'MsgBox iDay
        
            With ActiveCell.Characters(Start:=iDay, Length:=1).Font
                .Size = 11
                .Color = 192
            End With
        End If

    Next iDay

End Sub

Open in new window

0
Comment
Question by:hindersaliva
  • 2
  • 2
5 Comments
 
LVL 17

Assisted Solution

by:Roy_Cox
Roy_Cox earned 250 total points
Comment Utility
Why are using ActiveCell? An example workbook would help
0
 

Author Comment

by:hindersaliva
Comment Utility
ActiveCell for testing Roy. Yes I'll post a workbook later today.
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
Comment Utility
You cannot format the part of the text returned by the formula.
Copy and paste the value in another cell and the same piece of code would work without an issue with the cell with value only.
0
 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 250 total points
Comment Utility
Or you should try something like this.....
Sub BlankNonAttendingDays()
ActiveCell.Value = ActiveCell.Value 'This line will remove the formula and place the value
    For iDay = 1 To 7
        If Mid(ActiveCell.Value, iDay, 1) = Chr(154) Then
            With ActiveCell.Characters(Start:=iDay, Length:=1).Font
                .Size = 11
                .Color = 192
            End With
        End If
    Next iDay
End Sub

Open in new window

1
 
LVL 17

Expert Comment

by:Roy_Cox
Comment Utility
Pleased to help
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now