Eliminar error en tiempo de ejecución 13

Estoy trabajando con macros en excel 2010 y tengo un userform para el cual utilizo el código abajo descrito, todos los textbox me los llena bien pero al momento de aceptar y enviar los datos a las celdas respectivas me manda el error "error en tiempo de ejecución 13" y detiene la macro, pasa bien los datos pero ya no me deja trabajar, espero me puedan ayudar... Gracias
Private Sub Aceptar_Click()
Dim fila1 As Long
Dim ws As Worksheet
Set ws = Worksheets(2)
Range("B2").Select
'encontrar sig. Fila vacia
If Sheets("Egr").Range("B3") = "" Then
fila1 = ws.Cells(Rows.Count, 9).End(xlUp).Offset(0, 0).Row
Else
fila1 = ws.Cells(Rows.Count, 9).End(xlUp).Offset(1, 0).Row
End If
'Desproteger Hoja Activa
 ActiveSheet.Unprotect "2704"
'pasar datos a bdclientes
 ws.Cells(fila1, 2).Value = Me.TextBox1.Value
 ws.Cells(fila1, 3).Value = Me.TextBox2.Value
 ws.Cells(fila1, 4).Value = Me.ComboBox1.Value
 ws.Cells(fila1, 5).Value = Me.ComboBox2.Value
 ws.Cells(fila1, 6).Value = Me.TextBox6.Value
 ws.Cells(fila1, 7).Value = Me.TextBox7.Value
 ws.Cells(fila1, 8).Value = Me.TextBox19.Value
 ws.Cells(fila1, 9).Value = Me.TextBox8.Value
 ws.Cells(fila1, 10).Value = Me.TextBox10.Value
 ws.Cells(fila1, 11).Value = Me.TextBox11.Value
 ws.Cells(fila1, 12).Value = Me.TextBox12.Value
 ws.Cells(fila1, 13).Value = Me.TextBox13.Value
 ws.Cells(fila1, 14).Value = Me.TextBox14.Value
 ws.Cells(fila1, 15).Value = Me.ComboBox3.Value
 ws.Cells(fila1, 16).Value = Me.TextBox17.Value
 ws.Cells(fila1, 17).Value = Me.ComboBox4.Value
'Proteger Hoja Activa
 ActiveSheet.Protect "2704", DrawingObjects:=True, Contents:=True, Scenarios:= _
True, AllowFiltering:=True
'limpiar formulario
 Me.TextBox1.Value = ""
 Me.TextBox2.Value = ""
 Me.ComboBox1.Value = ""
 Me.ComboBox2.Value = ""
 Me.TextBox6.Value = ""
 Me.TextBox7.Value = ""
 Me.TextBox8.Value = ""
 Me.TextBox9.Value = ""
 Me.TextBox19.Value = ""
 Me.TextBox10.Value = ""
 Me.TextBox11.Value = ""
 Me.TextBox12.Value = ""
 Me.TextBox13.Value = ""
 Me.TextBox14.Value = ""
 Me.ComboBox3.Value = ""
 Me.TextBox17.Value = ""
 Me.ComboBox4.Value = ""
 Me.TextBox1.SetFocus
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
On Error Resume Next
TextBox2.Text = Format(CDate(TextBox2), "dd/mm/yy")
If Err <> 0 Then
MsgBox ("Debe ingresar los valores con formato dd/mm/aa"), vbRetryCancel, ATENCION
Cancel = True
End If
End Sub
Private Sub TextBox7_AfterUpdate()
TextBox7 = FormatNumber(CDbl(TextBox7.Value), 2)
End Sub
Private Sub TextBox10_AfterUpdate()
TextBox10 = FormatNumber(CDbl(TextBox10.Value), 2)
End Sub
Private Sub TextBox11_AfterUpdate()
TextBox11 = FormatNumber(CDbl(TextBox11.Value), 2)
End Sub
Private Sub TextBox12_AfterUpdate()
TextBox12 = FormatNumber(CDbl(TextBox12.Value), 2)
End Sub
Private Sub TextBox13_AfterUpdate()
TextBox13 = FormatNumber(CDbl(TextBox13.Value), 2)
End Sub
Private Sub ComboBox1_Enter()
 ComboBox1.RowSource = "TbProveedores"
End Sub
Private Sub ComboBox2_Enter()
 ComboBox2.RowSource = "TbComprasGastos"
End Sub
Private Sub ComboBox3_Enter()
 ComboBox3.RowSource = "Tabla3"
End Sub
Private Sub ComboBox4_Enter()
 ComboBox4.RowSource = "Tabla5"
End Sub
Private Sub TextBox7_Change()
 TextBox8.Text = Format((TextBox7.Text) * Val(TextBox19) / 100, "#,##0.00")
 TextBox9.Text = Format(Val(TextBox7 * 1) + (TextBox8), "#,##0.00")
End Sub
Private Sub TextBox19_Change()
 TextBox8.Text = Format((TextBox7.Text) * Val(TextBox19) / 100, "#,##0.00")
 TextBox9.Text = Format(Val(TextBox7 * 1) + (TextBox8), "#,##0.00")
End Sub
Private Sub TextBox9_Change()
TextBox14 = Format((TextBox9) - Val(TextBox10) - Val(TextBox11) - Val(TextBox12) + Val(TextBox13), "#,##0.00")
End Sub
Private Sub TextBox10_Change()
TextBox14 = Format((TextBox9) - Val(TextBox10) - Val(TextBox11) - Val(TextBox12) + Val(TextBox13), "#,##0.00")
End Sub
Private Sub TextBox11_Change()
TextBox14 = Format((TextBox9) - Val(TextBox10) - Val(TextBox11) - Val(TextBox12) + Val(TextBox13), "#,##0.00")
End Sub
Private Sub TextBox12_Change()
TextBox14 = Format((TextBox9) - Val(TextBox10) - Val(TextBox11) - Val(TextBox12) + Val(TextBox13), "#,##0.00")
End Sub
Private Sub TextBox13_Change()
TextBox14 = Format((TextBox9) - Val(TextBox10) - Val(TextBox11) - Val(TextBox12) + Val(TextBox13), "#,##0.00")
End Sub

1 Respuesta

Respuesta
1
¿No será que tienes celdas que están protegidas en el momento de intentar cambiarlas con los macros?
Sería muy útil saber dónde queda el marcador amarillo cuando al aparecer éste mensaje uno hace click en la opción DEPURAR. Esto sería muy útil para ayudarle, dado que tiene varias líneas de código.
Al Depurar queda en amarillo la primer fila pero si me llena los datos en la hoja de Excel, ahí si los pasa bien...
Private Sub TextBox7_Change()
TextBox8.Text = Format((TextBox7.Text) * Val(TextBox19) / 100, "#,##0.00")
TextBox9.Text = Format(Val(TextBox7 * 1) + (TextBox8), "#,##0.00")
End Sub
En la linea subrayada, ahí me marca en amarillo... Gracias
Bien: Ten en cuenta lo siguiente:
Val( ) convierte Texto a número para hacer operaciones: Val("15") = 15
De la misma manera que Str() convierte número a texto para, por ejemplo concatenar: Str(15) = "15"
El problema debe ser porque los TextBox no son ni número ni cadena de caracteres(String), son TextBox, controles. Por lo tanto, cada vez que necesitas el texto de un Text Box debes poner NombreTextBox.Text ; En tu ejemplo: TextBox19.Text.
Pero .Text devuelve el tipo String (osea texto), el cual no sirve para hacer operaciones.
La recomendación es la siguiente:
Que cada vez que utilices el texto de un textbox para hacer operaciones lo escribas así:
Val(Nombre_TextBox.Text) = 'un numero
Pasar a texto lo que resulte de cualquier operación, cada vez que igualas un String a un número:
Str(Valor_o_resultado_de_operaciones) = 'un numero en formato de texto
Entonces estas líneas quedarían así:
TextBox8.Text = Str( Format(Val(TextBox7.Text) * Val(TextBox19.text) / 100, "#,##0.00") )
TextBox9.Text = Str( Format(Val(TextBox7.text ) + val(TextBox8.text), "#,##0.00") )
Hay varias correcciones para hacer en el código, pero son los mismos criterios.
Ten en cuenta que las celdas de una hoja Excel aceptan y ofrecen cualquier formato de datos (son tipo Variant), por esa razón no te van a dar tanto problema en macros como los controles, que son mas complejos.
Ya no me marco el error pero me modifico las cantidades, ya no me realizo bien las operaciones, pero ya le intente de otra forma, Gracias por tu tiempo...

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas