+ Reply to Thread
Results 1 to 10 of 10

Division by zero

Hybrid View

  1. #1
    Registered User
    Join Date
    07-21-2015
    Location
    Monterrey, México
    MS-Off Ver
    MAC 2011
    Posts
    6

    Division by zero

    Sup,

    I have a problem in this code:

    ReDim SMT(1 To Per) As Variant
    Dim cont As Integer
    cont = 1
        For i = 1 To segmentos
            For j = 1 To PS(i)
                If s(i) = 1 Then SMT(cont) = salario_inicial(i) / 70.1 / 30.4
                If s(i) = 2 Then SMT(cont) = ((salario_inicial(i) - salario_final(i)) / ((PS(i) - j) + salario_final(i))) / 30.4 / 70.1
                'Para este caso se traslada el Punto de Origen a: [PS(i-1), salario_final(i)]
                If s(i) = 3 Then
                    If j = 1 Then
                        SMT(cont) = salario_inicial(i) / 30.4 / 70.1
                    Else
                        H = PS(i)
                        k = (salario_final(i))
                        a = (-H / ((SMT(cont - 1) * 30.4 * 70.1) - k) ^ 2)
                        fga = 1
                        fgb = -2 * k
                        fgc = k ^ 2 + (H - j) / a
                        SMTP = (-fgb + ((fgb) ^ 2 - 4 * fga * fgc) ^ 0.5) / 2
                        SMTN = (-fgb - ((fgb) ^ 2 - 4 * fga * fgc) ^ 0.5) / 2
                            
                            If Round(SMTP, 2) > Round(SMTN, 2) Then
                                SMT(cont) = SMTP / 30.4 / 70.1
                            Else
                                SMT(cont) = SMTN / 30.4 / 70.1
                            End If
                    End If
                End If
        
        cont = cont + 1
    
           Next j
        Next i
    It returns values from the general equation formula, but when the inner loop is finished once, it says that there is a division by zero and the program highlights the j variable. Using the watch feature, I get j's value as 2.

    Any help will be really grateful.

    Thanks
    Last edited by raulge91; 07-21-2015 at 06:47 PM. Reason: Noob

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Division by zero

    Welcome to the forum, raulje.

    Please take a few minutes to read the forum rules, and then edit your post to add CODE tags.

    Thanks.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    07-21-2015
    Location
    Monterrey, México
    MS-Off Ver
    MAC 2011
    Posts
    6

    Re: Division by zero

    I am really, sorry. Thanks for the comments.

    I am editing the post.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Division by zero

    Thank you.

    You don't include your variable declarations or initialization, so it's impossible to guess where the problem lies. On what line does the error occur, and what are the values of all relevant variables?

  5. #5
    Registered User
    Join Date
    07-21-2015
    Location
    Monterrey, México
    MS-Off Ver
    MAC 2011
    Posts
    6

    Re: Division by zero

    I suppose that the relevant variables here are:

    PS(i); PS(1)= 1 & PS(2)=4;
    Segmentos=2 ;
    Per= 5.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Division by zero

    And ... ?

  7. #7
    Registered User
    Join Date
    07-21-2015
    Location
    Monterrey, México
    MS-Off Ver
    MAC 2011
    Posts
    6

    Re: Division by zero

    The error is given at line: 163.

    Right after the first loop is completed. It is strange, before the last time I ran the code it didn't show me the error. I am running it and happened now, but not the last time...

    Is there any other information that I can give you for you to give me a better advice?

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Division by zero

    Raul, from what you have posted, how would I know what line 163 is?

  9. #9
    Registered User
    Join Date
    07-21-2015
    Location
    Monterrey, México
    MS-Off Ver
    MAC 2011
    Posts
    6

    Re: Division by zero

    Sorry, I am getting used to forums. I'll try my best to be more thorough. Here is the complete code:

    Sub cotizacion2()
    
    'iniciamos el código para generar la cotización a partir de pocos datos
    MsgBox ("¡Bienvenido al Asistente de Cotización!")
    Dim NSM, Nom, MaS, MeS, PR, areat, CSP, Asim, CF, P, H, actual As Double
    
    Dim Per, PP, R As Integer
    Dim curva As Boolean
    
    'Se declara la información necesaria para realizar la Cotización
    Nom = InputBox("Da el valor de la Nómina mensual:")
    Per = InputBox("¿Con cuánto personal cuenta la empresa?")
    PP = (Per * 0.8) - 1
    MaS = InputBox("Da el valor del Salario más alto:")
    MeS = InputBox("Da el valor del Salario más bajo:")
    PR = InputBox("Indica la Prima de Riesgo en Nivel: 1, 2, 3, 4 ó 5")
    
    'Restricciones
        While Round(MeS, 2) > Round(MaS, 2) Or 0 > PR > 5 Or Nom < 0 Or Per < 0
            MsgBox ("Verifica bien los datos ingresados y vuelve a intentar.")
            Nom = InputBox("Da el valor de la Nómina mensual:")
            Per = InputBox("¿Con cuánto personal cuenta la empresa?")
            PP = (Per * 0.8) - 1
            MaS = InputBox("Da el valor del Salario más alto:")
            MeS = InputBox("Da el valor del Salario más bajo:")
            PR = InputBox("Indica la Prima de Riesgo en Nivel: 1, 2, 3, 4 ó 5")
        Wend
        
      'Se declara el valor de PR
        If PR = 1 Then PR = 0.0054355
        If PR = 2 Then PR = 0.0113065
        If PR = 3 Then PR = 0.025984
        If PR = 4 Then PR = 0.0465325
        If PR = 5 Then PR = 0.0758875
        
    'Lógica de la cotización
    MsgBox ("Empieza la Cotización:" & vbNewLine & "El Cálculo se realiza de personal con mayor sueldo al menor.")
    curva = Application.InputBox("¿Esta Cotización tiene que segmentarse?" & vbNewLine & "True or False", Type:=4)
    
    'Inicia el Cálculo de Área
    If curva = True Then
        Dim segmentos, i As Integer
        segmentos = InputBox("Indica el número de segmentos a calcular.")
            While segmentos < 0
                segmentos = InputBox("Verifica el número de segmentos a calcular e ingrésalo de nuevo.")
            Wend
        's=tipo de curva; PS= personal segmentado; salario= salario del segmento i; area= nómina del segmento i
        ReDim s(0 To segmentos), PS(0 To segmentos), salario_inicial(0 To segmentos), salario_final(0 To segmentos), area(0 To segmentos) As Variant
        PS(0) = 0
        salario_inicial(0) = MaS
        salario_inicial(segmentos) = MeS
        salario_final(segmentos) = MeS
        salario_final(0) = MaS
        R = Per
            For i = 1 To segmentos
                s(i) = InputBox("Indica el tipo de aproximación que buscas:" & vbNewLine & "Nómina Promedio #1" & vbNewLine & "Nómina Creciente #2" & vbNewLine & "Nómina Creciente Variable #3")
                PS(i) = InputBox("Indica la cantidad de personal que se encuentra en el segmento " & i & vbNewLine & "Personal restante:" & R)
                salario_inicial(i) = InputBox("Indica el salario inicial a utilizar en el segmento " & i)
                salario_final(i) = InputBox("Indica el salario final del segmento " & i)
                While 0 > Round(s(i), 2) > 4 Or PS(i) > R Or Round(salario_inicial(i), 2) > Round(salario_inicial(i - 1), 2) Or Round(salario_final(i - 1), 2) < Round(salario_final(i), 2)
                    PS(i) = InputBox("Verifica la cantidad de personal del segmento " & i & " y vuelve a ingresarla." & vbNewLine & "Personal restante:" & R)
                    s(i) = InputBox("Verifica el tipo de aproximación que buscas:" & vbNewLine & "Nómina Promedio #1" & vbNewLine & "Nómina Creciente #2" & vbNewLine & "Nómina Creciente Variable #3")
                    salario_inicial(i) = InputBox("Verifica el salario inicial a utilizar en el segmento " & i & " y vuelve a ingresarlo.")
                    salario_final(i) = InputBox("Verifica el salario final a utilizar en el segmento " & i & " y vuelve a ingresarlo.")
    
                Wend
                 R = R - PS(i)
                
               If s(i) = 1 Then area(i) = salario_inicial(i) * PS(i)
               If s(i) = 2 Then area(i) = ((salario_inicial(i) - salario_final(i)) * PS(i) / 2) + salario_final(i) * PS(i)
               If s(i) = 3 Then area(i) = ((salario_inicial(i) - salario_final(i)) * PS(i) * 2 / 3) + salario_final(i) * PS(i)
               
               areat = areat + area(i)
                
                MsgBox (area(i))
                
            Next i
            
            MsgBox ("Nómina mensual " & areat)
            
      Else
            segmentos = 1
            s(1) = InputBox("Indica el tipo de aproximación que buscas:" & vbNewLine & "Nómina Promedio #1" & vbNewLine & "Nómina Creciente #2" & vbNewLine & "Nómina Creciente Variable #3")
            While 0 > seg > 4
                    seg = InputBox("Verifica el tipo de aproximación que buscas:" & vbNewLine & "Nómina Promedio #1" & vbNewLine & "Nómina Creciente #2" & vbNewLine & "Nómina Creciente Variable #3")
                Wend
               If s(1) = 1 Then areat = MaS * Per
               If s(1) = 2 Then areat = (MaS - MeS) * Per / 2 + MeS * Per
               If s(1) = 3 Then areat = (MaS - MeS) * Per * (2 / 3) + MeS * Per
               
        MsgBox ("Nómina mensual " & areat)
      
      
    End If
    
    'Inicia el Ajuste de Nómina
    While Round(areat, 2) <> Round(Nom, 2)
        If Round(areat, 2) > Round(Nom, 2) And segmentos = 2 Then
            area(2) = area(2) - (areat - Nom)
            areat = 0
            For i = 1 To segmentos
                areat = areat + area(i)
                Next i
            MsgBox ("Se modificó el Segmento 2 a: " & area(2))
    'Se modifican los parámetros de la segunda área
    
            If s(2) = 1 Then salario_inicial(2) = area(2) / PS(2)
            If s(2) = 2 Then salario_inicial(2) = ((area(2) - (PS(2) * salario_final(2))) * 2 / PS(2) + salario_final(2))
            If s(2) = 3 Then salario_inicial(2) = ((area(2) - (PS(2) * salario_final(2))) * (3 / 2) / PS(2)) + salario_final(2)
            
        Else
            area(segmentos - 1) = area(segmentos - 1) + (Nom - areat)
            areat = 0
            For i = 1 To segmentos
                areat = areat + area(i)
                Next i
            MsgBox ("Se modificó el Segmento " & (segmentos - 1) & " a " & area(segmentos - 1))
    'Se modifican los parámetros del última área
    
            If s(segmentos - 1) = 1 Then salario_inicial(segmentos - 1) = (area(segmentos - 1) / PS(segmentos - 1))
            If s(segmentos - 1) = 2 Then salario_inicial(segmentos - 1) = ((area(segmentos - 1) - (PS(segmentos - 1) * salario_final(segmentos - 1))) * 2 / PS(segmentos - 1)) + salario_final(segmentos - 1)
            If s(segmentos - 1) = 3 Then salario_inicial(segmentos - 1) = ((3 / 2) * (area(segmentos - 1) - (PS(segmentos - 1) * salario_final(segmentos - 1))) / PS(segmentos - 1)) + salario_final(segmentos - 1)
            
        End If
    
    Wend
    
    MsgBox ("Nómina Ajustada: " & areat & " = " & Nom & vbNewLine & "Inicia el Cálculo del Costo Óptimo.")
    'La Nómina queda ajustada al dato ingresado. Se continúa con el cálculo del salario por trabajador z.
    
    ReDim SMT(1 To Per) As Variant
    Dim cont As Integer
    cont = 1
        For i = 1 To segmentos
            For j = 1 To PS(i)
                If s(i) = 1 Then SMT(cont) = salario_inicial(i) / 70.1 / 30.4
                If s(i) = 2 Then SMT(cont) = ((salario_inicial(i) - salario_final(i)) / ((PS(i) - j) + salario_final(i))) / 30.4 / 70.1
                'Para este caso se traslada el Punto de Origen a: [PS(i-1), salario_final(i)]
                If s(i) = 3 Then
                    If j = 1 Then
                        SMT(cont) = salario_inicial(i) / 30.4 / 70.1
                    Else
                        H = PS(i)
                        k = (salario_final(i))
                        a = (-H / ((SMT(cont - 1) * 30.4 * 70.1) - k) ^ 2)
                        fga = 1
                        fgb = -2 * k
                        fgc = k ^ 2 + (H - j) / a
                        SMTP = (-fgb + ((fgb) ^ 2 - 4 * fga * fgc) ^ 0.5) / 2
                        SMTN = (-fgb - ((fgb) ^ 2 - 4 * fga * fgc) ^ 0.5) / 2
                            
                            If Round(SMTP, 2) > Round(SMTN, 2) Then
                                SMT(cont) = SMTP / 30.4 / 70.1
                            Else
                                SMT(cont) = SMTN / 30.4 / 70.1
                            End If
                    End If
                End If
        
        MsgBox ("Salarios Mínimos del Trabajador " & cont & ": " & SMT(cont))
        cont = cont + 1
    
           Next j
        Next i
    Thanks

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Division by zero

    A Next statement cannot generate a run-time error.

    What happens when you step through the code?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. #DIV/0! without any division!!!
    By JudiK in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-04-2013, 08:43 PM
  2. division gives wrong answer (division)
    By Brice in forum Excel General
    Replies: 5
    Last Post: 12-24-2010, 10:13 PM
  3. Division
    By samjs in forum Excel General
    Replies: 3
    Last Post: 02-22-2010, 02:01 AM
  4. Help with division
    By Twyko in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-11-2009, 04:48 PM
  5. Division by Zero
    By scotfitz in forum Excel General
    Replies: 5
    Last Post: 06-15-2008, 06:20 PM
  6. Need help with division, please!!!
    By swag143 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-16-2006, 12:05 PM
  7. [SOLVED] Division
    By David in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 07-26-2006, 06:05 AM
  8. Division By Zero
    By carl in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-27-2005, 01:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1