Two different fonts in one cell using excel vba

0 votes

In order to put a degree symbol after existing data in a cell, I consequently created a macro button to my ribbon. Also, I wanted to create a button that would add a check mark. Wingdings 2 converts "P" to a checkmark, according to my research. I can't figure out how to add the Wingdings 2 checkmark and keep the font of my current data without changing the font of the entire cell to Wingdings 2.

Sub Add_check()

Dim cellinfo As String
Dim cellinfoplus As String

'Grab existing content
cellinfo = ActiveCell.Value

'Change the "P" to a checkmark
ActiveCell.Value = "P"
    With Selection.Font
        .Name = "Wingdings 2"
    End With
'Save the checkmark into variable
cellinfoplus = ActiveCell.Value

'Concatenate variables
ActiveCell.Value = cellinfo + cellinfoplus

End Sub

Apr 10, 2023 in Others by narikkadan
• 63,600 points
982 views

1 answer to this question.

0 votes

Public Sub add_degree_symbol()
   ActiveCell.Value = ActiveCell.Value & "°"
End Sub

Public Sub addCheck()
   With ActiveCell
      .Value = .Value & "P"
      .Characters(Len(.Value), 1).Font.Name = "Wingdings 2"
   End With
End Sub

and a sub for adding any symbols at start or at end (default)

Public Sub addSymbols(Optional addAny As String = "P", Optional atStart As Boolean = False, Optional fontName As String = "Wingdings 2")
   Dim ln As Integer, lnany As Integer

   With ActiveCell
      ln = Len(.Value)
      lnany = Len(addAny)
      If atStart Then
         .Value = addAny & .Value
         .characters(1, lnany).Font.Name = fontName
      Else
         .Value = .Value & addAny
         .characters(Len(.Value), lnany).Font.Name = fontName
      End If
   End With
End Sub

Sub helper()
   Call addSymbols("P ", True)
End Sub

And another version to toggle the symbols

Public Sub toogleSymbols(Optional addAny As String = "P", Optional atStart As Boolean = False, Optional fontName As String = "Wingdings 2")
   Dim ln As Integer, lnany As Integer

   With ActiveCell
      ln = Len(.Value)
      lnany = Len(addAny)
      If atStart Then
         If Mid(.Value, 1, lnany) = addAny Then
            fontName = .Characters(lnany + 1, 1).Font.Name
            .Value = Right(.Value, ln - lnany)
            .Characters(1, ln - lnany).Font.Name = fontName
         Else
            .Value = addAny & .Value
            .Characters(1, lnany).Font.Name = fontName
         End If
      Else
         If Right(.Value, lnany) = addAny Then
            .Value = Left(.Value, ln - lnany)
         Else
            .Value = .Value & addAny
            .Characters(Len(.Value) - 1, lnany).Font.Name = fontName
         End If
      End If
   End With
End Sub

Sub callToogleSymbols()
   Call toogleSymbols("P ", True)
End Sub
answered Apr 10, 2023 by Kithuzzz
• 38,000 points

Related Questions In Others

0 votes
1 answer

Excel formula for searching two text in one cell and return values based on the result

You can include a second IF within ...READ MORE

answered Oct 10, 2022 in Others by narikkadan
• 63,600 points
1,210 views
0 votes
1 answer

Activating a Specific Cell in Excel Using VBA Results to Error 400

I think you trying to select cells(4, ...READ MORE

answered Dec 27, 2022 in Others by narikkadan
• 63,600 points
547 views
0 votes
1 answer

Is there a way to test a formula result in excel and type it only once, all within one cell and not using a user defined function?

Use the Let function: =LET(Value,A1+B2+C4+G3+B4,IF(Value>10,"No",Value)) I hope this helps ...READ MORE

answered Jan 9, 2023 in Others by narikkadan
• 63,600 points
666 views
0 votes
1 answer

Excel VBA userform paste text in expanded format (not just to one cell)

It should work if you simply supply ...READ MORE

answered Jan 21, 2023 in Others by narikkadan
• 63,600 points
641 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,218 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,668 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
916 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
1,058 views
0 votes
1 answer

Combine Two Formulas in Separate Cells Into One Cell

Use the openpyxl module in Python to do that, ...READ MORE

answered Mar 26, 2023 in Others by Kithuzzz
• 38,000 points
792 views
0 votes
1 answer

How to split columns into two sub columns under their parent column using python in excel

Use str.split: df[['pre', 'post']] = df['Column A'].str.split(r'\s*-->\s*', expand=True) print(df) # Output ...READ MORE

answered Apr 7, 2023 in Others by Kithuzzz
• 38,000 points
1,926 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP