VBA Partial Match Text Conditional Formatting

dwhitey1124

New Member
Joined
Oct 24, 2014
Messages
28
Hi everyone, I am working on a file that requires constant use of underscores. I would like to hide these underscores. I having been trying to find a way to select all instances of underscores (in unhidden and hidden sheets) and change the font to transparent. The closest I got is the below, but it's not good. Any help would be very much appreciated. Sub Underscore()
Dim rng As Selection
Dim Cell As Range
Dim start_str As Integer
Workbook.Select
Set rng = Selection
For Each Cell In rng
start_str = InStr(Cell.Value, "_")
If start_str Then
With Cell.Characters(start_str, 1).Font
.ColorIndex = xlNone
End With
End If
Next
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
.
.

Try something like this:

Code:
Sub ConcealUnderscores()

    Dim wks As Worksheet
    Dim rng As Range
    Dim byt As Byte
    
    For Each wks In ActiveWorkbook.Worksheets
        For Each rng In wks.Cells.SpecialCells( _
            xlCellTypeConstants, _
            xlTextValues)
            
            byt = InStr(rng, "_")
            If byt > 0 Then
                rng.Characters(byt, 1).Font.Color = _
                    rng.Interior.Color
            
            End If
        Next rng
    Next wks

End Sub
 
Upvote 0
Thanks gpeacok. There are 2 additional things I would like in the code: 1) I would like it to format more than just 1 underscore in a cell. For example, abc_def_ghi is only formatting the first underscore. I would also like the second to be formatted. 2) I would like this macro to run constantly and automatically. Thanks in advance.
.
.

Try something like this:

Code:
Sub ConcealUnderscores()

    Dim wks As Worksheet
    Dim rng As Range
    Dim byt As Byte
    
    For Each wks In ActiveWorkbook.Worksheets
        For Each rng In wks.Cells.SpecialCells( _
            xlCellTypeConstants, _
            xlTextValues)
            
            byt = InStr(rng, "_")
            If byt > 0 Then
                rng.Characters(byt, 1).Font.Color = _
                    rng.Interior.Color
            
            End If
        Next rng
    Next wks

End Sub
 
Upvote 0
Sorry for the double post. I would like to add that I already am using "Private Sub Worksheet_Change(ByVal Target As Range)" on the main sheet. Is there a way of having 2 worksheet change event macros on the same sheet with different key ranges? This would be one way of having the macro run automatically. Thanks.
Thanks gpeacok. There are 2 additional things I would like in the code: 1) I would like it to format more than just 1 underscore in a cell. For example, abc_def_ghi is only formatting the first underscore. I would also like the second to be formatted. 2) I would like this macro to run constantly and automatically. Thanks in advance.
 
Upvote 0
Sorry for the double post. I would like to add that I already am using "Private Sub Worksheet_Change(ByVal Target As Range)" on the main sheet. Is there a way of having 2 worksheet change event macros on the same sheet with different key ranges? This would be one way of having the macro run automatically. Thanks.


You would need to place this code within your existing Worksheet_Change procedure:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim wks As Worksheet
    Dim rng As Range
    Dim byt As Byte
    
    For Each wks In ThisWorkbook.Worksheets
        For Each rng In wks.Cells.SpecialCells( _
            xlCellTypeConstants, _
            xlTextValues)
            
            For byt = 1 To Len(rng.Value)
                If Mid$(rng.Value, byt, 1) = Chr(95) Then
                    rng.Characters(byt, 1).Font.Color = _
                        rng.Interior.Color
                End If
            Next byt
        Next rng
    Next wks

End Sub
 
Upvote 0
.
.

Actually, it would be better to use a Workbook_SheetChange event-handler for the above code, since we're dealing within underscores on all worksheets...
 
Upvote 0
If this is now a workbook change event, would I put the code on This Workbook instead of the sheet that has the worksheet event currently?

.
.

Actually, it would be better to use a Workbook_SheetChange event-handler for the above code, since we're dealing within underscores on all worksheets...
 
Upvote 0
If this is now a workbook change event, would I put the code on This Workbook instead of the sheet that has the worksheet event currently?


Yes, exactly.

And you'll need to change the first line to:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
 
Upvote 0

Forum statistics

Threads
1,215,374
Messages
6,124,573
Members
449,173
Latest member
Kon123

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