简体   繁体   中英

Change font color for variable as part of text in cell

I am struggling with VBA macro which should color part of the text.

The macro looks like

Sub Note()
        Dim c As Range
        Dim val As String
        Set c = ActiveCell
        val = InputBox("Add note", "Note text")
            If IsEmpty(c.Value) = True Then
                c.Value = Format(Now(), "DD MMM YY Hh:Nn") & ": " & val
            Else
                c.Value = c.Value & Chr(10) & Format(Now(), "DD MMM YY Hh:Nn") & ": " & val
        End If
        End Sub

And I want to achieve that Now() will be red and rest of text will be green.

I tried to play with .Font.Color = vbRed etc but without any luck

I also look on this answer but it not quite what I wanted

Try like this:

Option Explicit

Sub Note()

    Dim c           As Range
    Dim val         As String: val = "vit"
    Dim lngLen      As Long

    Set c = ActiveCell
    c.Value = Format(Now(), "DD MMM YY Hh:Nn") & ": " & val
    lngLen = Len(Format(Now(), "DD MMM YY Hh:Nn"))

    c.Characters(Start:=1, Length:=lngLen).Font.Color = vbRed

End Sub

I have removed the input box, but you can return it easily. It gives probably what you want.Pretty much, it asks for the length of the Now() format and it colors the first N signs in the formula in red, following the logic from the question you have mentioned in your question.

You linked an answer but you weren't using what was in there, why?

Try this :

Sub Note()
Dim c As Range
Dim val As String
Dim StartChar As Integer, _
    LenColor As Integer
Set c = ActiveCell

val = InputBox("Add note", "Note text")

With c
    .Font.Color = RGB(0, 0, 0)
    If IsEmpty(.Value) = True Then
        StartChar = 1
        LenColor = Len("DD MMM YY Hh:Nn")
        .Value = Format(Now(), "DD MMM YY Hh:Nn") & ": " & val
        .Characters(Start:=StartChar, Length:=LenColor).Font.Color = RGB(255, 0, 0)
    Else
        StartChar = Len(.Value) + 1
        LenColor = Len("DD MMM YY Hh:Nn")
        .Value = .Value & Chr(10) & Format(Now(), "DD MMM YY Hh:Nn") & ": " & val
        .Characters(Start:=StartChar, Length:=LenColor).Font.Color = RGB(255, 0, 0)
    End If
End With 'c
End Sub

Try this:

Sub Note()
Dim c As Range
Dim val As String
Dim lngPos As Integer
Set c = ActiveCell
val = InputBox("Add note", "Note text")
c.Value = ""
    If IsEmpty(c.Value) = True Then
        c.Value = Format(Now(), "DD MMM YY Hh:Nn") & " - " & val
        lngPos = InStr(ActiveCell.Value, " - ")
        With ActiveCell.Font
            .ColorIndex = 4
        End With
        With ActiveCell.Characters(Start:=1, Length:=lngPos - 1).Font
            .ColorIndex = 3 'or .Color = RGB(255, 0, 0)
        End With
    Else
        c.Value = c.Value & Chr(10) & Format(Now(), "DD MMM YY Hh:Nn") & " - " & val 
        lngPos = InStr(ActiveCell.Value, " - ")
        With ActiveCell.Font
            .ColorIndex = 4
        End With
        With ActiveCell.Characters(Start:=1, Length:=lngPos - 1).Font
            .ColorIndex = 3 'or .Color = RGB(255, 0, 0)
        End With
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM