vba- round then convert number

bloodybrit90

Board Regular
Joined
Jul 18, 2011
Messages
111
Hey,

the below code uses Solver determine to determine what a number needs to be in order to make 20%. It works well, however after the macro run I need it to round to the nearest whole number then convert it to end in 990.

Example.

From 154,859.054548
To 154,990

Sub MultiSolve()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set MyTestRange = Range("C5")
For cp = 0 To 8 'gives column offsets C to K
If MyTestRange.Offset(0, cp).Value >= 0 Then
MySet = MyTestRange.Offset(53, cp).Address
MyChange = MyTestRange.Offset(11, cp).Address
MyVal = "0.2"
SolverOk SetCell:=MySet, MaxMinVal:=3, ValueOf:=MyVal, ByChange:=MyChange
SolverSolve UserFinish:=True

End If
Next cp
Application.DisplayAlerts = True
Application.ScreenUpdating = False
End Sub


Any ideas?
 

Some videos you may like

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Rosen

Active Member
Joined
Dec 1, 2010
Messages
293
Try this.
Code:
Function NumberConverterX(Value As Double) As Long
    Dim sValue As String: sValue = CStr(Value)
    Dim nPos As Long: nPos = Strings.InStr(1, sValue, ".")
    If nPos > 0 Then sValue = Strings.Left(sValue, nPos - 1)
    If Strings.Len(sValue) > 3 Then
        sValue = Strings.Left(sValue, Strings.Len(sValue) - 3)
        sValue = sValue & "990"
        NumberConverterX = CLng(sValue)
    Else
        NumberConverterX = 990
    End If
End Function
I tested in on the supplied number and it worked, but you might want to test it with other numbers.

Hope that helps!
 

Rosen

Active Member
Joined
Dec 1, 2010
Messages
293
Unfortunately I am not familier with Solver, assuming that MySet is the location of the data output. You could picket up after running Solver and adjust it.

Code:
Sub MultiSolve()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Set MyTestRange = Range("C5")
    For cp = 0 To 8 'gives column offsets C to K
       If MyTestRange.Offset(0, cp).Value >= 0 Then
           MySet = MyTestRange.Offset(53, cp).Address
           MyChange = MyTestRange.Offset(11, cp).Address
           MyVal = "0.2"
           SolverOk SetCell:=MySet, MaxMinVal:=3, ValueOf:=MyVal, ByChange:=MyChange
           SolverSolve UserFinish:=True
           Range(MySet).Value = NumberConverterX(Range(MySet).Value)
        End If
    Next cp
    Application.DisplayAlerts = True
    Application.ScreenUpdating = False
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,102,576
Messages
5,487,656
Members
407,608
Latest member
kjw

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top