简体   繁体   中英

How to change Current Users Selected Font Color in Excel VBA

So essentially, I'm trying to make a log book and am trying to write a VBA Macro that detects the current user who has the workbook open (Unshared), then auto selects a font color for them. (So for example Person X opens up the workbork, then it would auto select the Color purple so that all text they enter by default is purple to indicate they are the one who input the text).

I wrote:

Sub userFont()
    Dim user As String
    
    Cells(19, 11).Value = ActiveWorkbook.UserStatus
    user = Cells(19, 11).Text
    
    
    If InStr(user, "xxx") > 0 Then
        Cells(3, 11).Value = "Found em"
        Excel.Font.Color = vbCyan
    End If

End Sub

So I already can detect who the user is ( as you can see from the "Found em" line haha) and that works perfectly fine, but once I select the user; how do I change their Font color selection by default/ and or, have any text they input auto change to their designated color. I appreciate any help!

Thanks a ton!

Try adding this code to your "ThisWorkbook" module

Private Sub Workbook_Open()
    ' Add a named constant for the font colour
    ' You'll see this constant "Formulas" tab -> "Name Manager"
    If InStr(1, "XXX", Application.UserName, vbTextCompare) > 0 Then
        Me.Names.Add Name:="FontColor", RefersTo:="=" & vbCyan
    Else
        Me.Names.Add Name:="FontColor", RefersTo:="=" & vbBlack
    End If
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Target.Font.Color = [FontColor]
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