Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

VB6 - Divided cost to get a correct total cost at the end

Hi,

I would like to be able to divide a cost per any amount so that the total once divided equals the cost not divided.

Ex:
If the Cost = 357.25$
If the divided value = 3
If we do 357.25 / 3 = 119.083333333333

So i cannot say that the value would be 119.08 per value cause 119.08*3 = 357.24.
So it would have to put 119.09 on the first and 119.08 on the 2 last value for a total of 357.25.

How can i do that?

Before the division:
User generated image
After
User generated image
Thanks for your help
Cost-devided.zip
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wilder1626

ASKER

That looks very good already. Let me do some test and i will also have to tweak it a little.
Is it me or i'm missing something here.

When i try below code, it puts the same value on each row.

    Dim i As Integer
    Dim z As Integer

    Dim total As Double, no As Integer, ave As Double, sumTotal As Double
    Dim tmpArr() As Double

    total = Val(total_cost.Text)
    no = Val(load_count.Text)
    ReDim tmpArr(no)
    ave = total / no

    For i = 0 To no - 1
        tmpArr(i) = Round(ave, 2)
        sumTotal = sumTotal + tmpArr(i)
    Next i
    If sumTotal < total Then
        tmpArr(0) = tmpArr(0) + (total - sumTotal)
    End If

    For z = 1 To MSHFlexGrid1.Rows - 1
        For i = 0 To no - 1
            If MSHFlexGrid1.TextMatrix(z, 1) = "" And MSHFlexGrid1.TextMatrix(z, 0) <> "" Then
                MSHFlexGrid1.TextMatrix(z, 1) = tmpArr(i)
            End If
        Next i
    Next z

Open in new window

All good now.
Thank you for your help

 [code]
Dim i As Integer
    Dim z As Integer

    Dim total As Double, no As Integer, ave As Double, sumTotal As Double
    Dim tmpArr() As Double

    total = Val(total_cost.Text)
    no = Val(load_count.Text)
    ReDim tmpArr(no)
    ave = total / no

    For i = 0 To no - 1
        tmpArr(i) = Round(ave, 2)
        sumTotal = sumTotal + tmpArr(i)
    Next i

    If sumTotal < total Then
        tmpArr(0) = tmpArr(0) + (total - sumTotal)
    End If


    For i = 0 To no - 1
        For z = 1 To MSHFlexGrid1.Rows - 1
            If MSHFlexGrid1.TextMatrix(z, 4) = "" Then

                MSHFlexGrid1.TextMatrix(z, 4) = tmpArr(i)
                Exit For
           
            End If
        Next z

    Next i
[/code]