Hello,
I am working on a spreadsheet that I have a goal seek function to find the optimal number of units based on certain parameters. My problem is that I need to round these numbers up or down depending on which one it is. I found a solution that mostly works but sometimes it does not act like I need it to. Most of the time it will round up or down properly, but certain numbers will not. I need these numbers to be whole numbers after each goal seek so that the next goal seek can operate correctly. I have included the code below which operates without errors. Any suggestions are very welcome.
I am working on a spreadsheet that I have a goal seek function to find the optimal number of units based on certain parameters. My problem is that I need to round these numbers up or down depending on which one it is. I found a solution that mostly works but sometimes it does not act like I need it to. Most of the time it will round up or down properly, but certain numbers will not. I need these numbers to be whole numbers after each goal seek so that the next goal seek can operate correctly. I have included the code below which operates without errors. Any suggestions are very welcome.
Code:
Function RDown(Amount As Double, digits As Integer) As Double
RDown = Int((Amount + (1 / (10 ^ (digits + 1)))) * (10 ^ digits)) / (10 ^ digits)
End Function
Function RUp(Amount As Double, digits As Integer) As Double
RUp = RDown(Amount + (10 / (10 ^ (digits + 1))), digits)
End Function
Private Sub CommandButton1_Click()
If Range("G2").Value = Range("V4") Then
'Goal Seek System Width
Range("C20").GoalSeek Goal:=Range("G4"), _
ChangingCell:=Range("C9")
Dim wsnumber As Double
wsnumber = RDown(Cells(9, 3).Value, 0)
Cells(9, 3).Value = wsnumber
'Goal Seek System Vol.
Range("C30").GoalSeek Goal:=Range("H4"), _
ChangingCell:=Range("C10")
Dim vwsnumber As Double
vwsnumber = RUp(Cells(10, 3).Value, 0)
Cells(10, 3).Value = vwsnumber
ElseIf Range("G2").Value = Range("V5") Then
'Goal Seek System Length
Range("C21").GoalSeek Goal:=Range("G4"), _
ChangingCell:=Range("C10")
Dim lsnumber As Double
lsnumber = RDown(Cells(10, 3).Value, 0)
Cells(10, 3).Value = lsnumber
'Goal Seek System Vol.
Range("C30").GoalSeek Goal:=Range("H4"), _
ChangingCell:=Range("C9")
Dim vlsnumber As Double
vlsnumber = RUp(Cells(9, 3).Value, 0)
Cells(9, 3).Value = vlsnumber
ElseIf Range("G2").Value = Range("V2") Then
'Goal Seek System Width
Range("C13").GoalSeek Goal:=Range("G4"), _
ChangingCell:=Range("C9")
Dim wnumber As Double
wnumber = RDown(Cells(9, 3).Value, 0)
Cells(9, 3).Value = wnumber
'Goal Seek System Vol.
Range("C28").GoalSeek Goal:=Range("H4"), _
ChangingCell:=Range("C10")
Dim vwnumber As Double
vwnumber = RUp(Cells(10, 3).Value, 0)
Cells(10, 3).Value = vwnumber
ElseIf Range("G2").Value = Range("V3") Then
'Goal Seek System Length
Range("C14").GoalSeek Goal:=Range("G4"), _
ChangingCell:=Range("C10")
Dim lnumber As Double
lnumber = RDown(Cells(10, 3).Value, 0)
Cells(10, 3).Value = lnumber
'Goal Seek System Vol.
Range("C28").GoalSeek Goal:=Range("H4"), _
ChangingCell:=Range("C9")
Dim vlnumber As Double
vlnumber = RUp(Cells(9, 3).Value, 0)
Cells(9, 3).Value = vlnumber
End If
End Sub