Divide a number into two parts so that the resulting sum is equal to the input

  • I want to divide a number in two parts so the sum of the parts is equal to the input number.


    For example,
    3.00 divided in two parts would be 3.00 = 1.50 + 1.50
    but 4.99 divided in two parts should be 4.99 = 2.50 + 2.49


    Input:
    3.00
    4.99


    After running a macro should be:
    ColumnA ColumnB
    3.00 ....... 1.50
    (blank cell) 1.50
    4.99 ....... 2.50
    (blank cell) 2.49


    Also the second part should be inserted below the first part.

  • Re: Divide a number into two parts so that the resulting sum is equal to the input


    I dont know what logic to use for 4.99 to show as 2.5 & 2.49. I can get this to show as 2.495 and 2.495.


    If that helps, use the below code and let me know if it works for you.


    Code
    Sub M()
    For i = 2 To 100 Step 2
    If Range("A" & i).Value = "" Then
    Exit For
    End If
    Range("B" & i) = Range("A" & i) / 2
    Range("B" & i).Offset(1, 0).EntireRow.Insert
    Range("B" & i).Offset(1, 0) = Range("A" & i) / 2
    Next
    End Sub
  • Re: Divide a number into two parts so that the resulting sum is equal to the input


    Thank you, its ok until now but the problem its that I need to show 2.50 and 2.49 instead of 2.495 and 2.495. I can use only 2 decimals. I need to split a cost in two parts with only two decimals.

  • Re: Divide a number into two parts so that the resulting sum is equal to the input


    Quote from zorin8488;750089

    Thank you, its ok until now but the problem its that I need to show 2.50 and 2.49 instead of 2.495 and 2.495. I can use only 2 decimals. I need to split a cost in two parts with only two decimals.


    Try this and let me know if it works for you.


    Code
    Sub M()
        For i = 2 To 100 Step 2
            If Range("A" & i).Value = "" Then
                Exit For
            End If
            Range("B" & i) = Application.WorksheetFunction.RoundDown(Range("A" & i) / 2, 2)
            Range("B" & i).Offset(1, 0).EntireRow.Insert
            Range("B" & i).Offset(1, 0) = Application.WorksheetFunction.RoundUp(Range("A" & i) / 2, 2)
        Next
    End Sub
  • Re: Divide a number into two parts so that the resulting sum is equal to the input


    Quote from zorin8488;750091

    YESS :) PERFECT!
    Thank you very much and all the best!


    Cheers, have fun :)

  • Re: Divide a number into two parts so that the resulting sum is equal to the input


    An alternative
    Data Column "A" results "B"

  • Re: Divide a number into two parts so that the resulting sum is equal to the input


    Thank you to you also MickG, your code its quite good too :)
    Wish you a good day!

  • Re: Divide a number into two parts so that the resulting sum is equal to the input


    Here's another variation

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!