Reputation: 13
Good Afternoon all,
I have the following code that works well, however I need to expand it a little. What I need, is to round up if the decimal is >= #.8. If the value is <=#.7, then round down.
So, an example is 1.7 would = 1 ; 1.8 would = 2 ; -1.7 would = -1 and -1.8 would equal -2.
Is there something I can write that would build off what I have already created?
If (Worksheets("DATA_STAGING").Range("X27").Value) > 1 Or (Worksheets("DATA_STAGING").Range("X27").Value) < -1 Then
Worksheets("STAFFING_VISUAL").Shapes("SinglesPackBUBBLE").Fill.ForeColor.RGB = vbRed
If Range("'DATA_STAGING'!X27").Value >= 0.7 Or Range("'DATA_STAGING'!X27").Value <= -0.7 Then
Worksheets("STAFFING_VISUAL").Shapes("SinglesPackBUBBLE").TextFrame.Characters.Text _
= Round((Worksheets("DATA_STAGING").Range("X27").Value), 0)
Else
Worksheets("STAFFING_VISUAL").Shapes("SinglesPackBUBBLE").TextFrame.Characters.Text _
= 0
End If
End If
Upvotes: 1
Views: 61
Reputation: 53623
If I understand correctly, the code below yields the following "rounded" results:
1.8 --> 2
1.83 --> 2
1.85 --> 2
1.7 --> 1
-1.7 --> -1
-1.75 --> -2
-1.8 --> -2
Here's an example:
Sub TestRounding()
Dim numbers() As Variant
Dim num As Variant
numbers = Array(1.8, 1.83, 1.85, 1.7, -1.7, -1.75, -1.8)
For Each num In numbers
Select Case GetMod(CDbl(num))
Case Is >= 0.8
Debug.Print num & " --> " & Application.WorksheetFunction.RoundUp(num, 0)
Case Is <= 0.7
Debug.Print num & " --> " & Application.WorksheetFunction.RoundDown(num, 0)
Case Else
'there should not be anything here because we round num in the GetMod function
End Select
Next
End Sub
Function GetMod(num As Double) As Double
num = Round(num, 1) 'to account for anything like 1.74, etc.
GetMod = Abs(num) - Int(Abs(num))
End Function
Upvotes: 1