Home » excel » excel – How to Change Multiple Format Parameters of a Cell Based on Its Value?

excel – How to Change Multiple Format Parameters of a Cell Based on Its Value?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to write a code that changes the style properties of a range of selected cells based on the cell value.

It worked when I only changed the text-color or font but since I added multiple arguments to each if the code does nothing. I also don’t get an error.

Dim userRange As Range

Set userRange = Application.InputBox("Select a range", Type:=8)

For Each cell In Selection

        If cell.Value < 0 Then cell.Font.FontStyle = "Comic Sans MS" & cell.Font.Size = 18 & cell.Font.Color = vbRed

        If cell.Value >= 0 And cell.Value <= 500 Then cell.Font.Bold = True & cell.Font.Italic = True & cell.Font.Underline = True

        If cell.Value > 500 And cell.Value <= 1000 Then cell.Font.FontStyle = "Monotype Corsiva" & cell.Font.Color = vbBlue & cell.Font.Underline = xlUnderlineStyleDouble

        If cell.Value > 1000 Then cell.Font.FontStyle = "Arial" & cell.Font.Bold = True & cell.Font.Italic = True & cell.Interior.Color = vbGreen & cell.Font.Color = vbWhite

Next cell

I think I’m really close but I can’t seem to figure out what I’m doing wrong! I hope my explanation is clear since I’m not really used to programming/scripting.

Thanks in advance!

How to&Answers:

I think this should fix it. Your old code wasn’t executing each line. You have to insert a space of a : instead of an &. Also, it saves some typing if you use the With feature to save some typing. Also take note that you are using ActiveCell, make sure that’s intentional.

Dim userRange As Range, d As Double, cell As Range 'added more variables

Set userRange = Application.InputBox("Select a range", Type:=8)

For Each cell In userRange.Cells
    d = cell.Value

    With cell.Font

        If d < 0 Then
            .FontStyle = "Comic Sans MS"
            .Size = 18
            .Color = vbRed
        End If


        If d >= 0 And d <= 500 Then
            .Bold = True
            .Italic = True
            .Underline = True

        End If


        If d > 500 And d <= 1000 Then
            .FontStyle = "Monotype Corsiva"
            .Color = vbBlue
            ActiveCell.Underline = xlDouble ' is this right?
        End If


        If d > 1000 Then
            .FontStyle = "Arial"
            .Bold = True
            .Italic = True
            .Color = vbGreen 'this is being undone in the next line of code.
            .Color = vbWhite 
        End If

    End With

Next cell

Answer:

You defined userRange but later on you are looping over cells in selection. Also you are using & incorrectly. You can try this:

Dim userRange As Range

Set userRange = Application.InputBox("Select a range", Type:=8)

For Each cell In userRange

        If cell.Value < 0 Then
            cell.Font.FontStyle = "Comic Sans MS"
            cell.Font.Size = 18 & cell.Font.Color = vbRed
        End If

        If cell.Value >= 0 And cell.Value <= 500 Then
            cell.Font.Bold = True & cell.Font.Italic = True
            cell.Font.Underline = True
        End If

        If cell.Value > 500 And cell.Value <= 1000 Then
            cell.Font.FontStyle = "Monotype Corsiva"
            cell.Font.Color = vbBlue
            cell.Font.Underline = xlUnderlineStyleDouble
        End If

        If cell.Value > 1000 Then
            cell.Font.FontStyle = "Arial"
            cell.Font.Bold = True
            cell.Font.Italic = True
            cell.Interior.Color = vbGreen
            cell.Font.Color = vbWhite
        End If
Next cell