SOLVED

VBA no ejecuta macro

Copper Contributor

Buenos días, tengo un macro creado, pero al darle a Ejecutar, no hace nada.

La idea del macro es: si una celda tiene valor mayor de 120 o el valor es negativo, que cambie el color del texto en una selección de casillas en la misma fila.

 

El macro que tengo es el siguiente:

Sub Macro2()

If Range("G19") > 120 And Range("G19") < 1 Then

Range("G19:L19").Font.COLOR = RGB(221, 221, 221)

ElseIf Range("G28") > 120 And Range("G28") < 1 Then

Range("G28:L28").Font.COLOR = RGB(221, 221, 221)

ElseIf Range("I37") > 120 And Range("I37") < 1 Then

Range("F37:M37").Font.COLOR = RGB(221, 221, 221)

ElseIf Range("G51") > 120 And Range("G51") < 1 Then

Range("G51:Q51").Font.COLOR = RGB(221, 221, 221)

ElseIf Range("G61") > 120 And Range("G61") < 1 Then

Range("G61:Q61").Font.COLOR = RGB(221, 221, 221)

ElseIf Range("L71") > 120 And Range("L71") < 1 Then

Range("G71:Q71").Font.COLOR = RGB(221, 221, 221)

ElseIf Range("L81") > 120 And Range("L81") < 1 Then

Range("G81:Q81").Font.COLOR = RGB(221, 221, 221)

ElseIf Range("L95") > 120 And Range("L95") < 1 Then

Range("G95:Q95").Font.COLOR = RGB(221, 221, 221)

ElseIf Range("L105") > 120 And Range("L105") < 1 Then

Range("G105:Q105").Font.COLOR = RGB(221, 221, 221)

ElseIf Range("L115") > 120 And Range("L115") < 1 Then

Range("G115:Q115").Font.COLOR = RGB(221, 221, 221)

ElseIf Range("L125") > 120 And Range("L125") < 1 Then

Range("G115:G125").Font.COLOR = RGB(221, 221, 221)

ElseIf Range("N135") > 120 And Range("N135") < 1 Then

Range("G135:U135").Font.COLOR = RGB(221, 221, 221)

ElseIf Range("N145") > 120 And Range("N145") < 1 Then

Range("G145:U145").Font.COLOR = RGB(221, 221, 221)

ElseIf Range("P155") > 120 And Range("P155") < 1 Then

Range("G155:Y155").Font.COLOR = RGB(221, 221, 221)

ElseIf Range("P165") > 120 And Range("P165") < 1 Then

Range("G165:Y165").Font.COLOR = RGB(221, 221, 221)

 

End If

End Sub

1 Reply
best response confirmed by PepsS (Copper Contributor)
Solution

@PepsS 

 

With AND criteria, all the conditions must be evaluated as True. You should replace the And criteria with Or criteria.

 

So replace the existing code with the following one...

 

Sub Macro2()

If Range("G19") > 120 Or Range("G19") < 0 Then
    Range("G19:L19").Font.Color = RGB(221, 221, 221)
ElseIf Range("G28") > 120 Or Range("G28") < 0 Then
    Range("G28:L28").Font.Color = RGB(221, 221, 221)
ElseIf Range("I37") > 120 Or Range("I37") < 0 Then
    Range("F37:M37").Font.Color = RGB(221, 221, 221)
ElseIf Range("G51") > 120 Or Range("G51") < 0 Then
    Range("G51:Q51").Font.Color = RGB(221, 221, 221)
ElseIf Range("G61") > 120 Or Range("G61") < 0 Then
    Range("G61:Q61").Font.Color = RGB(221, 221, 221)
ElseIf Range("L71") > 120 Or Range("L71") < 0 Then
    Range("G71:Q71").Font.Color = RGB(221, 221, 221)
ElseIf Range("L81") > 120 Or Range("L81") < 0 Then
    Range("G81:Q81").Font.Color = RGB(221, 221, 221)
ElseIf Range("L95") > 120 Or Range("L95") < 0 Then
    Range("G95:Q95").Font.Color = RGB(221, 221, 221)
ElseIf Range("L105") > 120 Or Range("L105") < 0 Then
    Range("G105:Q105").Font.Color = RGB(221, 221, 221)
ElseIf Range("L115") > 120 Or Range("L115") < 0 Then
    Range("G115:Q115").Font.Color = RGB(221, 221, 221)
ElseIf Range("L125") > 120 Or Range("L125") < 0 Then
    Range("G115:G125").Font.Color = RGB(221, 221, 221)
ElseIf Range("N135") > 120 Or Range("N135") < 0 Then
    Range("G135:U135").Font.Color = RGB(221, 221, 221)
ElseIf Range("N145") > 120 Or Range("N145") < 0 Then
    Range("G145:U145").Font.Color = RGB(221, 221, 221)
ElseIf Range("P155") > 120 Or Range("P155") < 0 Then
    Range("G155:Y155").Font.Color = RGB(221, 221, 221)
ElseIf Range("P165") > 120 Or Range("P165") < 0 Then
    Range("G165:Y165").Font.Color = RGB(221, 221, 221)
End If

End Sub

 

 

Also, you are using ElseIf and that means if the first condition is evaluated as True, it will ignore all the rest conditions.

 

If you want to check all the cells/ranges for those conditions, try the below code...

 

 

Sub Macro2()

If Range("G19") > 120 Or Range("G19") < 0 Then
    Range("G19:L19").Font.Color = RGB(221, 221, 221)
End If

If Range("G28") > 120 Or Range("G28") < 0 Then
    Range("G28:L28").Font.Color = RGB(221, 221, 221)
End If

If Range("I37") > 120 Or Range("I37") < 0 Then
    Range("F37:M37").Font.Color = RGB(221, 221, 221)
End If

If Range("G51") > 120 Or Range("G51") < 0 Then
    Range("G51:Q51").Font.Color = RGB(221, 221, 221)
End If

If Range("G61") > 120 Or Range("G61") < 0 Then
    Range("G61:Q61").Font.Color = RGB(221, 221, 221)
End If

If Range("L71") > 120 Or Range("L71") < 0 Then
    Range("G71:Q71").Font.Color = RGB(221, 221, 221)
End If

If Range("L81") > 120 Or Range("L81") < 0 Then
    Range("G81:Q81").Font.Color = RGB(221, 221, 221)
End If

If Range("L95") > 120 Or Range("L95") < 0 Then
    Range("G95:Q95").Font.Color = RGB(221, 221, 221)
End If

If Range("L105") > 120 Or Range("L105") < 0 Then
    Range("G105:Q105").Font.Color = RGB(221, 221, 221)
End If

If Range("L115") > 120 Or Range("L115") < 0 Then
    Range("G115:Q115").Font.Color = RGB(221, 221, 221)
End If

If Range("L125") > 120 Or Range("L125") < 0 Then
    Range("G115:G125").Font.Color = RGB(221, 221, 221)
End If

If Range("N135") > 120 Or Range("N135") < 0 Then
    Range("G135:U135").Font.Color = RGB(221, 221, 221)
End If

If Range("N145") > 120 Or Range("N145") < 0 Then
    Range("G145:U145").Font.Color = RGB(221, 221, 221)
End If

If Range("P155") > 120 Or Range("P155") < 0 Then
    Range("G155:Y155").Font.Color = RGB(221, 221, 221)
End If

If Range("P165") > 120 Or Range("P165") < 0 Then
    Range("G165:Y165").Font.Color = RGB(221, 221, 221)
End If
End Sub

 

1 best response

Accepted Solutions
best response confirmed by PepsS (Copper Contributor)
Solution

@PepsS 

 

With AND criteria, all the conditions must be evaluated as True. You should replace the And criteria with Or criteria.

 

So replace the existing code with the following one...

 

Sub Macro2()

If Range("G19") > 120 Or Range("G19") < 0 Then
    Range("G19:L19").Font.Color = RGB(221, 221, 221)
ElseIf Range("G28") > 120 Or Range("G28") < 0 Then
    Range("G28:L28").Font.Color = RGB(221, 221, 221)
ElseIf Range("I37") > 120 Or Range("I37") < 0 Then
    Range("F37:M37").Font.Color = RGB(221, 221, 221)
ElseIf Range("G51") > 120 Or Range("G51") < 0 Then
    Range("G51:Q51").Font.Color = RGB(221, 221, 221)
ElseIf Range("G61") > 120 Or Range("G61") < 0 Then
    Range("G61:Q61").Font.Color = RGB(221, 221, 221)
ElseIf Range("L71") > 120 Or Range("L71") < 0 Then
    Range("G71:Q71").Font.Color = RGB(221, 221, 221)
ElseIf Range("L81") > 120 Or Range("L81") < 0 Then
    Range("G81:Q81").Font.Color = RGB(221, 221, 221)
ElseIf Range("L95") > 120 Or Range("L95") < 0 Then
    Range("G95:Q95").Font.Color = RGB(221, 221, 221)
ElseIf Range("L105") > 120 Or Range("L105") < 0 Then
    Range("G105:Q105").Font.Color = RGB(221, 221, 221)
ElseIf Range("L115") > 120 Or Range("L115") < 0 Then
    Range("G115:Q115").Font.Color = RGB(221, 221, 221)
ElseIf Range("L125") > 120 Or Range("L125") < 0 Then
    Range("G115:G125").Font.Color = RGB(221, 221, 221)
ElseIf Range("N135") > 120 Or Range("N135") < 0 Then
    Range("G135:U135").Font.Color = RGB(221, 221, 221)
ElseIf Range("N145") > 120 Or Range("N145") < 0 Then
    Range("G145:U145").Font.Color = RGB(221, 221, 221)
ElseIf Range("P155") > 120 Or Range("P155") < 0 Then
    Range("G155:Y155").Font.Color = RGB(221, 221, 221)
ElseIf Range("P165") > 120 Or Range("P165") < 0 Then
    Range("G165:Y165").Font.Color = RGB(221, 221, 221)
End If

End Sub

 

 

Also, you are using ElseIf and that means if the first condition is evaluated as True, it will ignore all the rest conditions.

 

If you want to check all the cells/ranges for those conditions, try the below code...

 

 

Sub Macro2()

If Range("G19") > 120 Or Range("G19") < 0 Then
    Range("G19:L19").Font.Color = RGB(221, 221, 221)
End If

If Range("G28") > 120 Or Range("G28") < 0 Then
    Range("G28:L28").Font.Color = RGB(221, 221, 221)
End If

If Range("I37") > 120 Or Range("I37") < 0 Then
    Range("F37:M37").Font.Color = RGB(221, 221, 221)
End If

If Range("G51") > 120 Or Range("G51") < 0 Then
    Range("G51:Q51").Font.Color = RGB(221, 221, 221)
End If

If Range("G61") > 120 Or Range("G61") < 0 Then
    Range("G61:Q61").Font.Color = RGB(221, 221, 221)
End If

If Range("L71") > 120 Or Range("L71") < 0 Then
    Range("G71:Q71").Font.Color = RGB(221, 221, 221)
End If

If Range("L81") > 120 Or Range("L81") < 0 Then
    Range("G81:Q81").Font.Color = RGB(221, 221, 221)
End If

If Range("L95") > 120 Or Range("L95") < 0 Then
    Range("G95:Q95").Font.Color = RGB(221, 221, 221)
End If

If Range("L105") > 120 Or Range("L105") < 0 Then
    Range("G105:Q105").Font.Color = RGB(221, 221, 221)
End If

If Range("L115") > 120 Or Range("L115") < 0 Then
    Range("G115:Q115").Font.Color = RGB(221, 221, 221)
End If

If Range("L125") > 120 Or Range("L125") < 0 Then
    Range("G115:G125").Font.Color = RGB(221, 221, 221)
End If

If Range("N135") > 120 Or Range("N135") < 0 Then
    Range("G135:U135").Font.Color = RGB(221, 221, 221)
End If

If Range("N145") > 120 Or Range("N145") < 0 Then
    Range("G145:U145").Font.Color = RGB(221, 221, 221)
End If

If Range("P155") > 120 Or Range("P155") < 0 Then
    Range("G155:Y155").Font.Color = RGB(221, 221, 221)
End If

If Range("P165") > 120 Or Range("P165") < 0 Then
    Range("G165:Y165").Font.Color = RGB(221, 221, 221)
End If
End Sub

 

View solution in original post