VBA select case multiple ranges - vba

I am trying to figure out how to use the select case for the problem below.
In cell A1 I will have the text "white" or "black"
In cell B1 is a number
In cell C1 is the result i am looking for to be shown
If A1=white and B1<2 then C1=25
If A1=white and B1>=2 then C1=49
If A1=black and B1<2 then C1=14
If A1=black and B1>=2 then C1=30

You could use the following:
Select Case True
Case Range("A1") = "white" And Range("B1") < 2: Range("C1")=25
Case Range("A1") = "white" And Range("B1")>=2: Range("C1")=49
Case Range("A1") = "black" And Range("B1") < 2: Range("C1")=14
Case Range("A1") = "black" And Range("B1")>=2: Range("C1")=30
End Select

Related

How to simplify many "If Then Else" queries?

Can you give me a hint on how to make such code more elegant?
I need some more of these queries in the future and I would like to do it more professionally.
Thank you!
If Case = "V" Then
Case Is = "Sal"
Else
If Case = "K" Then
Case Is = "Dep"
Else
If Case = "A" Then
Case Is = "Auf"
Else
If Case = "M" Then
Case Is = "Mon"
Else
If Case = "T" Then
Case Is = "Tec"
Else
If Case = "W" Then
Case Is = "Ver"
Else
If Case = "B" Then
Case Is = "Ber"
Else
If Case = "P" Then
Case Is = "Ver"
Else
GoTo GoNext
End If
End If
End If
End If
End If
End If
End If
End If
Use Select Case
Sub test()
Dim strCode As String
Dim strVal As String
strCode = "B"
Select Case strCode
Case "A"
strVal = "Jan"
Case "B"
strVal = "Feb"
Case "C"
strVal = "Mar"
Case Else
strVal = "No match found"
End Select
End Sub
Using Select Case ... End Select
But you need to declare a variable, let us say x to be the reference
Dim x as String, y as String
'Allocate value to the `x` variable. In any way. Then:
Select Case x
Case "V": y = "Sal"
Case "K": y = "Dep"
Case "A": y = "Auf"
' and so on...
Case Else: y = "Whatever..."
End Select
Finally you obtain the y value according to the x one...
As usual, multiple ifs are simplified into one dictionary / hash table / object .
Dim variablename
Set variablename = CreateObject("Scripting.Dictionary")
variablename.Add ("V", "Sal")
variablename.Add ("K", "Dep")
....
variablename.Add ("P", "Var")
Get the variable as follows
ans = 'Nothing'
if variablename.exists("P") then
ans = variablename("P")
Rem ans = variablename.item("P") ?
Rem Equals "Var"
end if
see also https://excelmacromastery.com/vba-dictionary/
What about the following:
If Case = "V" Then
ElseIf Case = "K" Then
ElseIf Case = "A" Then
...
End If

ActiveWorkbook.Styles VBA

When I select an item from the drop down list my code goes to palette sheet (Contains of 100 Items cell “B5” has match formula and named as “Ref_rowOffset”. Next to items, I have 4 colours A, B, C, D) find the item and applies the colours for the tables. The table has been referenced as A, B, C, D. But as I have some sheets that contains from many pivot tables and slicers so the code won’t work for it. So the plan is to name ActiveWorkbook.Styles as let’s say as ("A") and use as reference. Any ideas would be fantastic?
Public Function get_color(str_type As String) As String
Dim iColOffset As Integer
Dim strRange As Range
Select Case str_type
Case Is = "A"
iColOffset = 1
Case Is = "B"
iColOffset = 2
Case Is = "C"
iColOffset = 3
Case Is = "D"
iColOffset = 4
Case Else
End Select
iRowOffset = Sheets("Palette").Range("Ref_rowOffset").Value
Set strRange = Sheets("Palette").Range("B5")
get_color = strRange.Offset(iRowOffset, iColOffset).Interior.Color
End Function
Public Sub ABSD()
Sheets("Dashboard").Range("A").Interior.Color = get_color("A")
Sheets("Dashboard").Range("B").Interior.Color = get_color("B")
Sheets("Dashboard").Range("C").Interior.Color = get_color("C")
Sheets("Dashboard").Range("D").Interior.Color = get_color("D")
End Sub

How do I IF with 3 conditions in VBA

I have a field in a report with 4 potential values, "A", "B", "C" & "D".
The following code makes the font color "red" for "C" and "D" and "black" for all other values. How do I add another statement to make the color "green" for value "A"?
<Color>=IIF((Fields!DFEE_condition.Value = "C") or (Fields!DFEE_condition.Value = "D"),"Red","Black")</Color>
Thanks
Have you tried to reuse IIF statement like this :
<Color>=IIF((Fields!DFEE_condition.Value = "C") or (Fields!DFEE_condition.Value = "D"),"Red",IIF((Fields!DFEE_condition.Value = "A"),"Green","Black"))</Color>
You could convert [A..D] to [1..4] and Choose():
color = choose(asc(Fields!DFEE_condition.Value)-64, "Green", "Black", "Red", "Red")
(If you can use statements, you would Select Case)
You need to write nested iif's ;)
result = IIF(condition1, value_if_true, IIF(condition2, value_if_true, value_if_false))
Try something like this:
<Color>
=IIF((Fields!DFEE_condition.Value = "A"), "Green", IIF(Fields!DFEE_condition.Value = "C") or (Fields!DFEE_condition.Value = "D"), "Red", "Black"))
</Color>

DataGridView CellFormatting not formatting all specified cells

I have an SQL query that populates a DataGridView through a DataTable:
SELECT top 100 a.Ordnum, oldBFE, newBFE, oldCBRA, newCBRA, oldLOMC, newLOMC, oldfld, newfld FROM
(SELECT Ordnum, PrpBFE as oldBFE, CBRADte as oldCBRA, LOMCDte as oldLOMC, FldZne AS oldfld
FROM [TOD].[dbo].[Orders] with (NOLOCK) WHERE RecRevDesc = '1 - Order Clone') a
JOIN
(SELECT Ordnum, PrpBFE as newBFE CBRADte as newCBRA, LOMCDte as newLOMC, FldZne AS newfld
FROM [TOD].[dbo].[Orders] with (NOLOCK) WHERE RecRevDesc = '2 Determination Completed-Workflow') b
ON a.Ordnum = b.Ordnum
If the value of certain pairs of cells are not equal, I need both cells to have a red forecolor. Right now, I am firing this through the cellformatting event:
For i As Integer = 0 To Me.gridCompare.RowCount - 1
If Me.gridCompare.Rows(i).Cells(1).ToString <> Me.gridCompare.Rows(i).Cells(2).ToString Then
Me.gridCompare.Rows(i).Cells(1).Style.ForeColor = Color.Red
Me.gridCompare.Rows(i).Cells(2).Style.ForeColor = Color.Red
ElseIf Me.gridCompare.Rows(i).Cells(3).ToString <> Me.gridCompare.Rows(i).Cells(4).ToString Then
Me.gridCompare.Rows(i).Cells(3).Style.ForeColor = Color.Red
Me.gridCompare.Rows(i).Cells(4).Style.ForeColor = Color.Red
ElseIf Me.gridCompare.Rows(i).Cells(5).ToString <> Me.gridCompare.Rows(i).Cells(6).ToString Then
Me.gridCompare.Rows(i).Cells(5).Style.ForeColor = Color.Red
Me.gridCompare.Rows(i).Cells(6).Style.ForeColor = Color.Red
ElseIf Me.gridCompare.Rows(i).Cells(7).ToString <> Me.gridCompare.Rows(i).Cells(8).ToString Then
Me.gridCompare.Rows(i).Cells(7).Style.ForeColor = Color.Red
Me.gridCompare.Rows(i).Cells(8).Style.ForeColor = Color.Red
End If
Next
However, only cells 1 and 2 have the correct formatting. What am I doing wrong? The order of the columns does not matter. I have tried individual IF statements as well.
You can't use the ElseIf branch since that will prevent testing the other pairs. Put each test in it's own IF-EndIf. You also need to test the Value property of the Cell:
If Me.gridCompare.Rows(i).Cells(1).Value.ToString <> _
Me.gridCompare.Rows(i).Cells(2).Value.ToString Then
'etc
End If
If Me.gridCompare.Rows(i).Cells(3).Value.ToString <> _
Me.gridCompare.Rows(i).Cells(4).Value.ToString Then
'etc
End If
Also, make sure those cells aren't null (or nothing) or else it will throw an exception.

VBA Excel automatic colour and value change

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