VBA Excel automatic colour and value change - vba

I am trying to set up a personal management spreadsheet for work. I have a list of tasks with varying priority.
What I am trying to do here is if the number of tasks * priority goes hits certain thresholds the colour of the availability cells changes and the description cell value changes, eg "busy"
here is the code I have so far, how do I implement it to change automatically when I change the value of the task list
Sub Avail_flag()
TasksRange = ActiveSheet.Range("P3:P6")
availcells = Range("M8,N8")
busyflag = 0
medBusyFlag = 0
highBusyFlag = 0
imedBusyFlag = 0
If Range("p4") > 0 Then
medBusyFlag = 1
ElseIf Range("p4") > 2 Then
medBusyFlag = 2
ElseIf Range("p5") > 0 Then
highBusyFlag = 1
ElseIf Range("p5") > 2 Then
highBusyFlag = 2
ElseIf Range("p6") > 0 Then
imedBusyFlag = 1
End If
For Each sell In lRange
busyflag = (medBusyFlag + (highBusyFlagI * 2) + (imedBusyFlag * 3))
If busyflag > 0 Then
For Each cell In Range(availcells)
cell.Color = green
Next
cell("N8").Value = "Occupied"
ElseIf busyflag > 3 Then
For Each cell In Range(availcells)
cell.Color = orange
Next
cell("N8").Value = "Busy"
ElseIf busyflag > 5 Then
For Each cell In Range(availcells)
cell.Color = red
Next
cell("N8").Value = "Unavailable"
Else
For Each cell In Range(availcells)
cell.Color = white
End If
End Sub
here is a capture of the spreadsheet if that helps, the highlighted grey part is where all the magic happens

You can use the Change event for the sheet:
Private Sub Worksheet_Change(ByVal Target As Range)

I went for conditional formatting, something I hadn't heard of before. After looking it up and learning how to use it it seem to be by far the best option. Thank you #mehow for the usggestion

Related

How to disable/enable multiple combo boxes based on selection on one combo box on access form?

When I select option 1 in the main combo box I want to disable all 15 combo boxes, when option 2 is selected I want to disable 10 combo boxes and enable 5 and when option 3 is selected I want to disable 5 combo boxes and enable 10. I am using afterupdate for the main combo box. I am using disbale/enable =True/False for the 15 combo boxes. I was wondering if there is an easy way to do this instead writing individual disable/enable for each combo box may like a loop?
Say you have 15 combos with name like cbo01, cbo02, ...and so on.
and you set the enabled property from option group name frameCombos (has 3 options in it).
Maybe this is the easy way you're wondering for. Maybe.
Private Sub FrameCombos_AfterUpdate()
disableCombos Me.FrameCombos.Value
End Sub
Private Sub disableCombos(optVal As Byte)
Dim i As Byte, j As String
Const prefixCbo As String = "cbo"
Select Case optVal
Case 1
'disable all combos
For i = 1 To 15
If i < 10 Then
j = "0" & i
Else
j = i
End If
Me.Controls(prefixCbo & j).Enabled = False
Next i
Case 2
'disable 10 combos (cbo01, ..., cbo10)
'enable 5 combos (cbo11, ..., cbo15)
For i = 1 To 15
If i < 10 Then
j = "0" & i
Else
j = i
End If
If i <= 10 Then
Me.Controls(prefixCbo & j).Enabled = False
ElseIf i >= 11 And i <= 15 Then
Me.Controls(prefixCbo & j).Enabled = True
End If
Next i
Case 3
'enable 5 combos (cbo01, ..., cbo05)
'disable 10 combos (cbo06, ..., cbo15)
For i = 1 To 15
If i < 10 Then
j = "0" & i
Else
j = i
End If
If i <= 5 Then
Me.Controls(prefixCbo & j).Enabled = False
ElseIf i >= 6 And i <= 15 Then
Me.Controls(prefixCbo & j).Enabled = True
End If
Next i
End Select
End Sub

Excel VBA: "Too many different cell formats" - Is there a way to remove or clear these formats in a Macro?

So, I made a fun and simple macro that randomly selects R, G, and B values until it uses every possible combination (skipping repeats), and setting the color values of a 10x10 square with each new color.
The only problem is that I have run into the limit for the number of cell formats. Microsoft says that the limit should be around 64000, but I found it to be exactly 65429 on a blank workbook in Excel 2013.
I've included a clear format code, but it seems to have no effect:
Cells(X, Y).ClearFormats
Microsoft lists some resolutions, but 3 out of the 4 of them are essentially "Don't make too many formats", and the 4th format is to use a third party application.
Is there really nothing that can be done in VBA?
A1:J10 will print a new color
K1 will print the percentage to completion
L1 will print the number of colors used
M1 will print the number of times a color combination is repeated
Dim CA(255, 255, 255) As Integer
Dim CC As Long
Dim RC As Long
Dim R As Integer
Dim G As Integer
Dim B As Integer
Dim X As Integer
Dim Y As Integer
CC = 0
RC = 0
X = 1
Y = 1
Do While ColorCount < 16777216
R = ((Rnd * 256) - 0.5)
G = ((Rnd * 256) - 0.5)
B = ((Rnd * 256) - 0.5)
If CA(R, G, B) <> 1 Then
CA(R, G, B) = 1
'Step down to the next row
'If at the 10th row, jump back to the first and move to the next column
If X < 10 Then
X = X + 1
Else
X = 1
If Y < 10 Then
Y = Y + 1
Else
Y = 1
End If
End If
Cells(X, Y).ClearFormats 'doesn't do what I hope :(
Cells(X, Y).Interior.Color = RGB(R, G, B)
CC = CC + 1
Cells(1, 11).Value = (CC / 16777216) * 100
Cells(1, 12).Value = CC
Else
RC = RC + 1
Cells(1, 13).Value = RC
End If
Loop
There are several ways to resolve this issue, but the cleanest and easiest method is to remove all extra styles (I have seen workbooks with 9000+ styles )
With the following simple VBA code you can remove all non-builtin styles and in the vast majority of cases this fixes the error.
Sub removeStyles()
Dim li as long
On Error Resume Next
With ActiveWorkbook
For li = .Styles.Count To 1 Step -1
If Not .Styles(li).BuiltIn Then
.Styles(li).Delete
End If
Next
End With
End Sub

Need to repeat code 30 times

this is my code and I want to re-execute it so that the next column has the exact same code repeated on it. That is, D:28 moves to E:28 and range E:110:I120 moves to F110:J120. I am having trouble finding a loop that does this, can anyone please help. My code is,
Sub Rebuild()
tonnes = Range("D28").Value
If tonnes > 2600000 Then
Range("E110:I120").Select
Selection.Copy
Range("E18:I28").Select
ActiveSheet.Paste
Else:
Range("E18:I28").Interior.Color = xlNone
Range("E18:I18") = ""
Range("E19:I19") = ""
Range("E20:I20") = 0
Range("E21:I21") = 2.4
Range("E22:I22") = "=E21+E20"
Range("E23:I23") = "=24 - E22"
Range("E24:I24") = "=100 * E23 / 24"
Range("E25:I25") = 3000
Range("E26:I26") = "=E25 * E23"
Range("E27:I27") = "=E26"
Range("E28:I28") = "=D28 + 27"
End If
End Sub
Option Explicit
Sub Rebuild()
Dim cumtonnes As Long
'you initially had tonnes as the variable name, but I was not sure if this was a typo or not.
cumtonnes = Range("D28").Value
If cumtonnes > 2600000 Then
Range("E110:I120").Copy Range("F110:J120")
Range("D28").Copy Range("E28")
Else:
Range("E18:I28").Interior.Color = xlNone
Range("E18:I18") = ""
Range("E19:I19") = ""
Range("E20:I20") = 0
Range("E21:I21") = 2.4
Range("E22:I22") = "=E21+E20"
Range("E23:I23") = "=24 - E22"
Range("E24:I24") = "=100 * E23 / 24"
Range("E25:I25") = 3000
Range("E26:I26") = "=E25 * E23"
Range("E27:I27") = "=E26"
Range("E28:I28") = "=D28 + 27"
End If
End Sub
So I adjusted the part that will do the copy and paste of the cells. I did not add in any loop currently as I did not know what you wanted repeated 30 times.

Adding Data to an existing SeriesCollection in excel

I am creating a chart from data inside an Excel sheet. Everything works. But now I want to remove values that are below a limit and display them as "Others". Removing them works but I don't know how to add an additonal "others" value.
This is part of the code:
Co.chart.SetSourceData Source:=DataSource
Co.chart.ChartTitle.Text = "Best selling games"
Co.chart.SeriesCollection(1).ApplyDataLabels ShowPercentage:=True, ShowValue:=False
For Each d In Co.chart.SeriesCollection(1).DataLabels
v = CLng(Split(d.Caption, "%")(0))
If v < 10 Then
Rest = Rest + v
d.Delete
End If
Next
If Rest > 0 Then
Co.chart.SeriesCollection(1).DataLabels.AddData("Others",Rest); ' HERE
End If
In the second last line is some pseudocode about what I want to achieve.
I found a "dirty" solution for this. Instead of deleting the first item I RENAME it to "Others" instead of deleting it and adding the "Others" afterwards:
For Each d In Co.chart.SeriesCollection(1).DataLabels
Counter = Counter + 1
v = CLng(Split(d.Caption, "%")(0))
If v <= 10 Then
If RestPos < 0 Then
RestPos = Counter
Else
d.Delete
End If
Rest = Rest + v
End If
Next
If Rest > 0 Then
Co.chart.SeriesCollection(1).DataLabels(RestPos).Caption = Rest & " %"
End If

vba check for negative value

I wrote a simple VBA code to check if the value of a cell is negative and if it is negative, highlight it red. For some reason, I keep getting "run-time mismatch". My code is
For x = 2 To 100
Set val3 = Worksheets("Summary").Cells(x, 9)
If val3.Value < 0 Then
Worksheets("Summary").Cells(x, 9).FontColorIndex = 3
End If
Next x
Column 9 (the column I am checking) is filled with dollar values. Thank you in advance for your help.
In your code, you're simply missing a dot.
FontColorIndex = 3
should be:
Font.ColorIndex = 3
Public Sub test()
For x = 2 To 100
Set val3 = Worksheets("Sheet1").Cells(x, 9)
If val3.Value < 0 Then
Worksheets("Sheet1").Cells(x, 9).Font.Color = RGB(99, 179, 73)
End If
Next x
End Sub
Code above works in excel 2007
Thanks for your post, I needed to change the negative numbers to zeros after a certain process - BUT I want to check them before I use them so changing the font color is useful, so this code works well for the whole sheet range until (245,50)
Sub negnumbers()
For x = 1 To 245
For y = 1 To 50
Set val3 = Worksheets("target sheet").Cells(x, y)
If val3.Value < 0 Then
Worksheets("target sheet").Cells(x, y).Font.ColorIndex = 3
Worksheets("target sheet").Cells(x, y) = 0
End If
Next y
Next x
End Sub