jevi
Active Member
- Joined
- Apr 13, 2010
- Messages
- 339
- Office Version
- 2016
- Platform
- Windows
Hi all,
I have text boxes with values like this
which they get the data from an excel sheet K166:P167 and is connected with a slicer with you can choose to view the data of Gen, Feb and March. So the textboxes are connected with =K166, the other one, L166 etc etc. But if in the cells of excel sheet the values are conditional formatting with the colours but in the textboxes they don't keep the conditional formatting and I would like to color them in automatic so when is ▲ green and when is ▼ red. I don't know how to do it with text boxes.
Jamie gave me help and I have this macro with works but when I click on the slicer to choose the month of the data to view everytime I need to click the macro...so It doesn't do it automatically and it will be nicer to do it automatically.
Thank you,
VBA Code:
Sub ChangeColourTwo()
If Range("K166").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 1")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 1")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("L166").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 2")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 2")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("M166").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 3")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 3")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("N166").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 4")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 4")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("O166").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 5")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 5")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("P166").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 6")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 6")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("K167").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 7")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 7")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("L167").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 8")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 8")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("M167").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 9")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 9")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("N167").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 10")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 10")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("O167").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 11")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 11")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("P167").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 12")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 12")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
End Sub
IMPORTANT:- Right click on the sheet tab and choose view code; paste the code below
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("K166:P167")) Is Nothing Then
Call ChangeColour
End If
End Sub
I have text boxes with values like this
which they get the data from an excel sheet K166:P167 and is connected with a slicer with you can choose to view the data of Gen, Feb and March. So the textboxes are connected with =K166, the other one, L166 etc etc. But if in the cells of excel sheet the values are conditional formatting with the colours but in the textboxes they don't keep the conditional formatting and I would like to color them in automatic so when is ▲ green and when is ▼ red. I don't know how to do it with text boxes.
Jamie gave me help and I have this macro with works but when I click on the slicer to choose the month of the data to view everytime I need to click the macro...so It doesn't do it automatically and it will be nicer to do it automatically.
Thank you,
VBA Code:
Sub ChangeColourTwo()
If Range("K166").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 1")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 1")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("L166").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 2")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 2")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("M166").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 3")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 3")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("N166").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 4")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 4")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("O166").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 5")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 5")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("P166").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 6")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 6")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("K167").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 7")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 7")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("L167").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 8")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 8")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("M167").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 9")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 9")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("N167").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 10")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 10")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("O167").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 11")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 11")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
If Range("P167").Value < 0 Then
ActiveSheet.Shapes.Range(Array("TextBox 12")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) 'change TextBox 1 to your Textbox name
Else
ActiveSheet.Shapes.Range(Array("TextBox 12")).TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 176, 80)
End If
End Sub
IMPORTANT:- Right click on the sheet tab and choose view code; paste the code below
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("K166:P167")) Is Nothing Then
Call ChangeColour
End If
End Sub