Question:
Remove certain characters without losing formatting

Problem: 

So I have a large set of data, within one of the columns is a data field that I’m needing to italicize based on a certain character. If a word begins with a $ and ends with a & it needs to be italicized for instance a cell could contain the following $Text1& (Text2). After formatting I would need it to look like this: Text1 (Text2). I’ve tried serval diffrent ways to format, but every time I find and replace all the $ and & it either italicizes everything in the cell or nothing at all.


I’ve tried VBA, python, I seem to run into the same issue every time.


Solution:

Assume that in each cell, there is at most one pair markers ($ &), but not more than one.

Option Explicit

Sub demo()

    Dim dataRng As Range, c As Range, sTxt

    Dim Index1 As Integer, Index2 As Integer

    Set dataRng = ActiveSheet.Range("A1:A3") ' Update as needed

    For Each c In dataRng

        sTxt = c.Text

        Index1 = InStr(sTxt, "$")

        Index2 = InStr(sTxt, "&")

        If Index1 * Index2 > 0 And Index2 > Index1 Then

            c.Value = Replace(Replace(sTxt, "$", ""), "&", "")

            c.Characters(Index1, Index2 - Index1 - 1).Font.Italic = True

        End If

    Next

End Sub



Answered by: >taller_ExcelHome

Credit: >Stack overflow


Suggested reads:

>What is data binding in Angular?

>What is microservice architecture, and why is it better than monolithic architecture?

>What is pipe in Angular?

>What makes Python 'flow' with HTML nicely as compared to PHP?

>What to do when MQTT terminates an infinite loop while subscribing to a topic in Python?

>Creating custom required rule - Laravel validation

>How to configure transfers for different accounts in stripe with laravel?

>Laravel Query Solved: Laravel withOnly not restricting the query


Ritu Singh

Ritu Singh

Submit
0 Answers