Old text in one color, new text in another color. Excel VBA - vba

Each day there is a new worksheet where I write some notes. I would like to have a button that creates a new worksheet everyday, puts the old text in black and when I start writing new text (can be in a cell with text already) it should be blue.
Is there a way to do that? I can use VBA as well.

Whenever you run it, the cells with values on the activesheet will be blue and on the empty cells you will be writing in black:
Public Sub TestMe()
Cells.Font.Color = vbBlack
ActiveSheet.UsedRange.Font.Color = vbBlue
End Sub

Related

Reset Button Excel VBA - Value as Number not Text

I have a spreadsheet and I want to reset some cells back to how they begin.
My Reset Button works using the following code.....
Sub Reset_Cells()
'Updateby Extendoffice 20161008
Range("D4", "D8").Value = "£0.00"
Range("D11").Value = "£0.00"
End Sub
It works great but it inserts the £0.00 as text which effects a dependant cell with an IF statement (see below)
=IF(D4=0,0,IF(D4<=300,35,IF(D4<=500,50,IF(D4<=1000,70,IF(D4<=1500,80,IF(D4<=3000,115,IF(D4<=5000,205,IF(D4<=10000,455,IF(D4<=200000,ROUND(D4/100,2)*5,IF(D4>200000,10000))))))))))
When clicked the vlaues 'look' how they should but the dependant cell shows £10,000 (the highest IF option) instead of £0.00.
The resetted (is that even a word?!) cells have the green triangle in the corner which tell me the cell has a Number Stored as Text.
How can I fix this?
Thanks
How about:
Sub Reset_Cells()
With Range("D11,D4,D8")
.Value = 0
.NumberFormat = "£#,##0.00"
End With
End Sub

Color Chart Columns by Cell Color Error

I am trying create a chart using cell colors from the active cells on a specific worksheet. When using the macro provided below I find that only some of the assigned cell RGB color codes match to the chart. I am not sure why some colors would match and some would not. The correct colors display in the chart when I manually enter the color codes. Is there something I am leaving out of this macro or an extra step I need to take?
I am using Excel 2016 for this project.
Sub ColorChartColumnsbyCellColor()
With Sheets("Sheet1").ChartObjects(1).Chart.SeriesCollection(1)
Set vAddress = ActiveSheet.Range(Split(Split(.Formula, ",")(1), "!")(1))
For i = 1 To vAddress.Cells.Count
.Points(i).Format.Fill.ForeColor.RGB =
ThisWorkbook.Colors(vAddress.Cells(i).Interior.ColorIndex)
Next i
End With
End Sub
You're assigning a color index to the RGB property. Color indexes have nothing to do with Red Green Blue. Besides, #Tim William's has a point: conditional formatting may play a role in what you're doing.
Try this code, which assigns the Color property to the RGB property:
Sub ColorChartColumnsbyCellColor()
With Sheets("Sheet1").ChartObjects(1).Chart.SeriesCollection(1)
Set vAddress = ActiveSheet.Range(Split(Split(.Formula, ",")(1), "!")(1))
For i = 1 To vAddress.Cells.Count
'Comment the line below and uncomment the next one to take conditional formatting into account.
.Points(i).Format.Fill.ForeColor.RGB = vAddress.Cells(i).Interior.Color
'.Points(i).Format.Fill.ForeColor.RGB = vAddress.Cells(i).DisplayFormat.Interior.Color
Next i
End With
End Sub

Changing colour of cells using command button (VBA Excel)

I currently have a userform that loads up with three command buttons (primary, secondary and tertiary). The idea here is that a user will select their own colour scheme using these three buttons.
I have cells in my workbook that represent each colour and then a macro which sets all chart objects to these colours (using the cells) when it is run.
I have managed to initialise the userform so that the three command buttons interior colour is determined by the three cells in my workbook:
Private Sub UserForm_Initialize()
Dim cs As Worksheet
Set cs = Sheets("ColourScheme")
TextBox1.SetFocus '' Shift focus away from primary
Primary.BackColor = cs.Range("B1").Interior.color
Secondary.BackColor = cs.Range("B2").Interior.color
Tertiary.BackColor = cs.Range("B3").Interior.color
End Sub
What I want to do now is that when each button is clicked, the colour palette loads up, the user selects a colour either using the wheel or an RGB figure and then finally the cells in my workbook and the command buttons interior colour change based on the user's choice.
I don't know if this can be done but so far I'm having no luck with anything I've tried to load the palette up:
Private Sub Primary_Click()
Application.Dialogs.Item(xlDialogColorPalette).Show
End Sub
Try the function which I use on several Applications, it's slightly converted to fit your original minimalized code, let me know if it works for you...
Private Sub Primary_Click()
Const ColorIndexLast As Long = 32 'index of last custom color in palette
Dim PickNewColor As Double 'color that was picked in the dialogue
Dim myOrgColor As Double 'original color of color index 32
'save original palette color,modify Range according to your needs
myOrgColor = Range("A1").Interior.Color
'call the color picker dialogue
If Application.Dialogs(xlDialogEditColor).Show(ColorIndexLast) = True Then
' "OK" was pressed, read the new color from the palette
PickNewColor = ActiveWorkbook.Colors(ColorIndexLast)
ActiveWorkbook.Colors(ColorIndexLast) = myOrgColor ' reset palette color to its original value
Else
' "Cancel" was pressed, palette wasn't changed >> return old color (or xlNone if no color was passed to the function)
PickNewColor = myOrgColor
End If
' update Colors in relevant Cell
Range("A1").Interior.Color = PickNewColor
End Sub
This is the solution I arrived at:
Private Sub Primary_Click()
Dim cs As Worksheet
Set cs = Sheets("ColourScheme")
Dim newColour As Long
Application.Dialogs(xlDialogEditColor).Show (1)
newColour = ThisWorkbook.Colors(1)
cs.Range("B1").Interior.color = newColour
Primary.BackColor = newColour
End Sub

It is possible to trap AutoFilter as an Event?

I am trying to show the filter columns to users in the report. Excel gives a different icon but for large no. columns it will be good to color the columns in another color like blue.
I found code at Is there a way to see which filters are active in Excel, other than just the funnel icons?
It works for me, but how do start this code without any button
SheetChange and selection change do not work.
code
Sub test()
Call markFilter(ActiveSheet)
End Sub
Sub markFilter(wks As Worksheet)
Dim lFilCol As Long
With wks
If .AutoFilterMode Then
For lFilCol = 1 To .AutoFilter.Filters.Count
'/ If filter is applied then mark the header as bold and font color as red
If .AutoFilter.Filters(lFilCol).On Then
.AutoFilter.Range.Columns(lFilCol).Cells(1, 1).Font.Color = vbRed
.AutoFilter.Range.Columns(lFilCol).Cells(1, 1).Font.Bold = True
Else
'/ No Filter. Column header font normal and black.
.AutoFilter.Range.Columns(lFilCol).Cells(1, 1).Font.Color = vbBlack
.AutoFilter.Range.Columns(lFilCol).Cells(1, 1).Font.Bold = False
End If
Next
Else
'/ No Filter at all. Column header font normal and black.
.UsedRange.Rows(1).Font.Color = vbBlack
.UsedRange.Rows(1).Font.Bold = False
End If
End With
End Sub
I will use the same example I used in the answer that you mentioned in your post. I answered that. :)
There is no filter change event in excel. One work-around that I would use is trapping the calculate method of the worksheet or better the workbook.
So, in the worksheet with filter add a dummy formula like this: =SUBTOTAL(3,Sheet2!$A$1:$A$100) this counts only visible cells. But its up to you. Feel free to use any formula that responds to filter change.
After that, go to workbook's code and add this :
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Call markFilter(Sh)
MsgBox "Filter changed"
End Sub
Boom. Now you are trapping the filter change events and it will update the filtered columns by firing the vba code.
Note markFilter is coming from the answer that you mentioned.
The key points from my article Trapping a change to a filtered list with VBA
A "dummy" WorkSheet is added with a single SUBTOTAL formula in A1 pointing back to the range being filtered on the main sheet.
A Worksheet_Calculate() Event is added to the "dummy" WorkSheet, this Event fires when the SUBTOTAL formula updates when the filter is changed.
The next two steps are only needed if it is desired to run the Workbook Calculation as Manual
Add a Workbook_Open Event to set the EnableCalculation property of all sheets other than "Dummy" to False.
Run the Workbook in Calculation mode

Conditional formatting excel textbox

What is the best way to change the font color of a single textbox based on value of linked cell?
Textbox is located on sheet1 when recording macro it recognizes textbox as ActiveSheet.Shapes.Range(Array("TextBox 1")).Select
I have inserted an image on sheet1, then I inserted textbox's from the insert toolbar. All the textbox's are linked to data on the "stylist" sheet. This sheet gets updated with a macro when the workbook is opened. I'm trying to get the textbox fonts to be red or green based on comparing the value of the linked cell to another cell on the stylist sheet.
enter image description here
Please try this..
' replace Text with your text box name
ActiveSheet.Text.Object.ForeColor = RGB(0, 255, 0)
With the code you provided and described as having a linked cell, one would assume you are referring to a TextBox from the ActiveX toolbar.
The code for that textbox is located in the Worksheet module. Right click on the sheet tab and select View code to open that module.
If your Linked cell is A1 then we could use a Worksheet_Change event to trigger the code when you change A1.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Address <> "$A$1" Then Exit Sub
Dim x
x = IIf(Target <= 0, vbRed, vbGreen)
Me.TextBox1.ForeColor = x
End Sub
You can also use the TextBox1_Change event, this would trigger when the textbox changed.
Private Sub TextBox1_Change()
Dim x
x = IIf(TextBox1.Value <= 0, vbRed, vbGreen)
Me.TextBox1.ForeColor = x
End Sub
You did not indicate what the conditions were so I made up my own.This example uses,
if <=0 then red, else green
The results would be