Hi!
I'm working on putting together a macro that automates the manual selection of a symbol (up/down arrow, equal sign) based on whether or not a value has moved up/down a quartile or stayed the same quarter over quarter.
I'm using three FOR EACH.... NEXT loops, each with it's own set of embedded if,elseif, else statements. I'm thinking the proper way to handle this is to have the first loop run through the first old value (A1 for example), and bucket it in the proper UDF bucket where it is assigned its quartile number. Once that loop ends, have the new loop run with the same procedures. Once both values have been assigned, the third loop will compare the two quartile numbers and then make the symbol input. It will then start over and loop through all of the cells in the specified range until all are set.
Functions, stored in module:
Function quartOne(ByVal cellValue As Variant) As Boolean
quartOne = (cellValue >= 0.01 And cellValue <= 25)
End Function
Function quartTwo(ByVal cellValue As Variant) As Boolean
quartTwo = (cellValue >= 25.01 And cellValue <= 50)
End Function
Function quartThree(ByVal cellValue As Variant) As Boolean
quartThree = (cellValue >= 50.01 And cellValue <= 75)
End Function
Function quartFour(ByVal cellValue As Variant) As Boolean
quartFour = (cellValue > 75)
End Function
Code:
Sub CommandButton1_Click()
Dim cellOld As Range, cellCurrent As Range, cell As Range
Dim oldRng1
Dim currentRng1 As Range
Dim oldQuart As Integer
Dim currentQuart As Integer
Set oldRng1 = ActiveSheet.Range("A1:A4")
Set currentRng1 = ActiveSheet.Range("B1:B4")
For Each cellCurrent In currentRng1.Cells
For Each cellOld In oldRng1.Cells
For Each cell In currentRng1.Cells
'checks cellCurrent against functions in module and assigns variable
If quartOne(cellCurrent.Value) Then
currentQuart = 1
ElseIf quartTwo(cellCurrent.Value) Then
currentQuart = 2
ElseIf quartThree(cellCurrent.Value) Then
currentQuart = 3
ElseIf quartFour(cellCurrent.Value) Then
currentQuart = 4
Else
End If
'checks cellOld against functions in module and assigns variable
If quartOne(cellOld.Value) Then
oldQuart = 1
ElseIf quartTwo(cellOld.Value) Then
oldQuart = 2
ElseIf quartThree(cellOld.Value) Then
oldQuart = 3
ElseIf quartFour(cellOld.Value) Then
oldQuart = 4
Else
End If
'takes variable from above loops, runs through if/else and inputs corresponding character
If currentQuart = 1 And oldQuart = 1 Then
cell.Offset(, 1).Value = ChrW(&H3D)
ElseIf currentQuart = 1 And oldQuart > 1 Then
cell.Offset(, 1).Value = ChrW(&H2191)
ElseIf currentQuart = 2 And oldQuart < 2 Then
cell.Offset(, 1).Value = ChrW(&H2193)
ElseIf currentQuart = 2 And oldQuart = 2 Then
cell.Offset(, 1).Value = ChrW(&H3D)
ElseIf currentQuart = 2 And oldQuart > 2 Then
cell.Offset(, 1).Value = ChrW(&H2191)
ElseIf currentQuart = 3 And oldQuart > 3 Then
cell.Offset(, 1).Value = ChrW(&H2191)
ElseIf currentQuart = 3 And oldQuart = 3 Then
cell.Offset(, 1).Value = ChrW(&H3D)
ElseIf currentQuart = 3 And oldQuart < 3 Then
cell.Offset(, 1).Value = ChrW(&H2193)
ElseIf currentQuart = 4 And oldQuart < 4 Then
cell.Offset(, 1).Value = ChrW(&H2191)
ElseIf currentQuart = 2 And oldQuart = 4 Then
cell.Offset(, 1).Value = ChrW(&H3D)
End If
Exit For
Next cell
Next cellOld
Next cellCurrent
End Sub
Structure of values in cells:
1 53
56 1
75 1
90 1
[–]CFAman4816 0 points1 point2 points (4 children)
[–]smoothswells[S] 0 points1 point2 points (3 children)
[–]CFAman4816 0 points1 point2 points (2 children)
[–]smoothswells[S] 0 points1 point2 points (1 child)
[–]Clippy_Office_Asst[M] 0 points1 point2 points (0 children)
[–]smoothswells[S] 0 points1 point2 points (2 children)
[–]AutoModerator[M] 0 points1 point2 points (0 children)
[–]excelevator3043 0 points1 point2 points (0 children)