Sumar varios label en formulario

Tengo lo siguiente: 8 TextBox (Cantidad), 8 TextBox (precio), 8 Label (Importe) y 1 Label (Total)

Necesito la suma total en Label (Total) de los Label (importe)

Al momento de capturar en (cantidad) y (precio) dando como resultado en (importe) números menores a 1,000.00 el resultado en (Total) lo muestra correctamente, pero superando cantidad de 1,000.00 en (importe) me da como resultado en (Total) 1.00

Private Sub Precio1_AfterUpdate()
Precio1 = Format(Precio1, "#,##0.00")
Importe1 = Cantidad1 * Precio1
Importe1 = Format(Importe1, "#,##0.00")
Total.Caption = Val(Importe1.Caption)
Total = Format(Total, "#,##0.00")
End Sub

Private Sub Precio2_AfterUpdate()

Precio2 = Format(Precio2, "#,##0.00")
Importe2 = Cantidad2 * Precio2
Importe2 = Format(Importe2, "#,##0.00")
Total.Caption = Val(Importe1.Caption) + Val(Importe2.Caption)
Total = Format(Total, "#,##0.00")
End Sub

Private Sub Precio3_AfterUpdate()
Precio3 = Format(Precio3, "#,##0.00")
Importe3 = Cantidad3 * Precio3
Importe3 = Format(Importe3, "#,##0.00")
Total.Caption = Val(Importe1.Caption) + Val(Importe2.Caption) + Val(Importe3.Caption)
Total = Format(Total, "#,##0.00")
End Sub

Private Sub Precio4_AfterUpdate()
Precio4 = Format(Precio4, "#,##0.00")
Importe4 = Cantidad4 * Precio4
Importe4 = Format(Importe4, "#,##0.00")
Total.Caption = Val(Importe1.Caption) + Val(Importe2.Caption) + Val(Importe3.Caption) + Val(Importe4.Caption)
Total = Format(Total, "#,##0.00")
End Sub

Private Sub Precio5_AfterUpdate()
Precio5 = Format(Precio5, "#,##0.00")
Importe5 = Cantidad5 * Precio5
Importe5 = Format(Importe5, "#,##0.00")
Total.Caption = Val(Importe1.Caption) + Val(Importe2.Caption) + Val(Importe3.Caption) + Val(Importe4.Caption) + Val(Importe5.Caption)
Total = Format(Total, "#,##0.00")
End Sub

Private Sub Precio6_AfterUpdate()
Precio6 = Format(Precio6, "#,##0.00")
Importe6 = Cantidad6 * Precio6
Importe6 = Format(Importe6, "#,##0.00")
Total.Caption = Val(Importe1.Caption) + Val(Importe2.Caption) + Val(Importe3.Caption) + Val(Importe4.Caption) + Val(Importe5.Caption) + Val(Importe6.Caption)
Total = Format(Total, "#,##0.00")
End Sub

Private Sub Precio7_AfterUpdate()
Precio7 = Format(Precio7, "#,##0.00")
Importe7 = Cantidad7 * Precio7
Importe7 = Format(Importe7, "#,##0.00")
Total.Caption = Val(Importe1.Caption) + Val(Importe2.Caption) + Val(Importe3.Caption) + Val(Importe4.Caption) + Val(Importe5.Caption) + Val(Importe6.Caption) + Val(Importe7.Caption)
Total = Format(Total, "#,##0.00")
End Sub

Private Sub Precio8_AfterUpdate()
Precio8 = Format(Precio8, "#,##0.00")
Importe8 = Cantidad8 * Precio8
Importe8 = Format(Importe8, "#,##0.00")
Total.Caption = Val(Importe1.Caption) + Val(Importe2.Caption) + Val(Importe3.Caption) + Val(Importe4.Caption) + Val(Importe5.Caption) + Val(Importe6.Caption) + Val(Importe7.Caption) + Val(Importe8.Caption)
Total = Format(Total, "#,##0.00")
End Sub

1 respuesta

Respuesta
2

El detalle, es que si usas Val(Importe1) como el importe está formateado, ahora es un texto, entonces el val de un texto es cerro

Primero se tiene que convertir el importe a número con Cdbl(Importe)

Te anexo la macro con los cambios, cambia tu código por lo siguiente:

Sub Actualizar_Totales(num)
'Por.Dante Amor
    Me.Controls("Precio" & num) = Format(Me.Controls("Precio" & num), "#,##0.00")
    If Me.Controls("Cantidad" & num) = "" Then cant = 0 Else cant = Val(Me.Controls("Cantidad" & num))
    If Me.Controls("Precio" & num) = "" Then prec = 0 Else prec = CDbl(Me.Controls("Precio" & num))
    '
    Me.Controls("Importe" & num) = cant * prec
    Me.Controls("Importe" & num) = Format(Me.Controls("Importe" & num), "#,##0.00")
    Total = ""
    For i = 1 To 8
        If Me.Controls("Importe" & i) = "" Then impo = 0 Else impo = CDbl(Me.Controls("Importe" & i))
        wtotal = wtotal + impo
    Next
    Total.Caption = Format(wtotal, "#,##0.00")
End Sub
'
Private Sub Precio1_AfterUpdate()
    Call Actualizar_Totales(1)
End Sub
Private Sub Precio2_AfterUpdate()
    Call Actualizar_Totales(2)
End Sub
Private Sub Precio3_AfterUpdate()
    Call Actualizar_Totales(3)
End Sub
Private Sub Precio4_AfterUpdate()
    Call Actualizar_Totales(4)
End Sub
Private Sub Precio5_AfterUpdate()
    Call Actualizar_Totales(5)
End Sub
Private Sub Precio6_AfterUpdate()
    Call Actualizar_Totales(6)
End Sub
Private Sub Precio7_AfterUpdate()
    Call Actualizar_Totales(7)
End Sub
Private Sub Precio8_AfterUpdate()
    Call Actualizar_Totales(8)
End Sub

.

'S aludos. Dante Amor. Recuerda valorar la respuesta. G racias

.

Avísame cualquier duda

.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas