使用excel vba在一个单元格中使用两种不同的字体

axzmvihb  于 2023-04-13  发布在  其他
关注(0)|答案(2)|浏览(628)

因此,我在功能区中添加了一个宏按钮,用于在单元格中现有数据的后面插入度数符号。我还想制作一个按钮来添加复选标记符号。在我的研究中,Wingdings 2将“P”更改为复选标记。我不知道如何在不将整个单元格的字体更改为Wingdings 2的情况下保持现有数据的字体并添加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

我在Mr. Excel上浏览了这个post,但不知道如何修改它,youtube视频被删除了。
感谢您的任何帮助!

zfycwa2u

zfycwa2u1#

您需要在这里使用.Characters,类似于以下内容:

Sub AddTheCheck(ByVal rng As Range)
    Dim cell As Range
    For Each cell in rng
        With cell
            .Value = .Value & "P"
            .Characters(Start:=Len(.Value), Length:=1).Font.Name = "Wingdings 2"
        End With
    Next
End Sub

这样称呼它:

Sub DoIt()
   AddTheCheck ActiveCell
   'or 
   If TypeOf Selection Is Range Then
       AddTheCheck Selection 'for multiple selected cells
   End If
End Sub

或者,如果您只想使用ActiveCell,则:

Sub AddTheCheck()
    With ActiveCell
        .Value = .Value & "P"
        .Characters(Start:=Len(.Value), Length:=1).Font.Name = "Wingdings 2"
    End With
End Sub
qvtsj1bj

qvtsj1bj2#

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

和一个sub,用于在开始或结束时添加任何符号(默认)

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

另一个版本是对符号进行toogle

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

相关问题