Consulting

Results 1 to 10 of 10

Thread: Changing a font style and color in VBA

  1. #1

    Changing a font style and color in VBA

    I'm trying to write a function that takes two numbers as input and returns the product of the two. If the product is negative I want it to be colored red and italicized, If positive it should be colored blue. I've started a code but I keep getting the output #VALUE!. Is there a way to do it without calling on the specific worksheet in the code?

    Function Multiplier(dNumber1 As Double, dNumber2 As Double) As Double
    
    Dim dProduct As Double
    Dim Data As Range, Cell As Range
    
    
    dProduct = dNumber1 * dNumber2
    Multiplier = dProduct
    
    
    For Each Cell In Data
        If Cell.Value < 0 Then
            Cell.Font.Italic = True
            Cell.Font.Color = 3
        
        Else: Cell.Font.Color = 6
    End If
    Next
    
    
    End Function

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You cannot use a UDF to change Font, Colours, etc. You could use conditional formatting.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    What Range is Data?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    I guess what I'm trying to do is that have the function wrong on a cell by cell basis. So I can pick a random cell put the function name in and have the output be italicized or not and have it be the desired color. Is that possible.

  5. #5

    HELP! Color change and italics

    I'm trying to write a function that takes two numbers as input and returns the product of the two. If the product is negative, I want to color it red and italicize it and if it is positive, it should be blue. Two problems I'm running into. The code wont italicized the negative numbers and you can't just drag the code down. You have to manually type the function into each cell. It works otherwise...multiplies the two numbers and changes color. Any suggestions?

    Thanks!

    option explicit
    option base 1
    
    Function Multiplier(dNumber1 As Double, dNumber2 As Double) As Double
    
    Dim dProduct As Double
    
    dProduct = dNumber1 * dNumber2
    
    If dProduct < 0 Then
        ActiveCell.Font.Italic = True
        ActiveCell.Font.Color = RGB(255, 0, 0)
    
    Else: ActiveCell.Font.Color = RGB(0, 0, 255)
    
    End If
    
    Multiplier = dProduct
     
    End Function

  6. #6
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    I think

    You should use "Conditional formatting"

  7. #7
    it has to be part of my code.

  8. #8
    How would I go about using conditional formatting to get my desired end result...Negative values are colored red and italicized and positive numbers colored blue. I'm super confused.

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    If you want to avoid using CF, then maybe put this in the WS code module



    Option Explicit
     
    Private Sub Worksheet_Calculate()
        Dim c As Range
        
        On Error Resume Next
        
        For Each c In Me.UsedRange.SpecialCells(xlCellTypeFormulas).Cells
            If c.Formula Like "=Multiplier*" Then
                If c.Value < 0 Then
                    c.Font.Italic = True
                    c.Font.Color = RGB(255, 0, 0)
                Else
                    c.Font.Italic = False
                    c.Font.Color = RGB(0, 0, 255)
                End If
            End If
        Next
    End Sub


    and this in a standard module


    Option Explicit
    Option Base 1
     
    Function Multiplier(dNumber1 As Double, dNumber2 As Double) As Double
         
        Dim dProduct As Double
         
        dProduct = dNumber1 * dNumber2
         
        Multiplier = dProduct
         
    End Function


    To get the formula to flow, you have to use relative addresses

    Capture.JPG
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10
    Oh man that's awesome! I finally figured out something similar but yours runs so much smoother. Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •