Limpieza y Error en Formularios

jues

New Member
Joined
Mar 17, 2023
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Saludos amigos espero esten bien

En mi proyecto una vez cargo los datos y doy click en el Botón Guardar (CmdGuardar) ocurren los siguientes eventos:

1.- El sistema me guarda los datos en la Sheet10 (MATRZ), pero inmediatamente aparece el Formulario Nivel (frmnivel) el cual al tratar de borrarlo en la X me remite al mensaje de que falta información en la casilla correspondiente al Grado Aprobado (TextBox5) y conforme se ve en la imagen 1 solo limpia la información correspondiente al Nombre y Apellido (TextBox1), Cedula (TextBox2), Nacionalidad (ComboBox1), Edad (TextBox3), Genero (ComboBox2), Mano Dominante (ComboBox3), Estado Civil (ComboBox4), Número de Hijos (TextBox4) y Grado Aprobado (TextBox5).

VBA Code:
Option Explicit
Dim crt As Boolean ' para controlar la seleccion de los option

Private Sub CmdREGISTRAR_Click()

If Me.CombLISTA.Value <> "" And crt <> False Then ' si el combo esta lleno y la variabla control esta true , continua

Dim men As String
If Me.OptAÑO.Value = True And Me.OptAÑO.Enabled = True Then
 men = Me.OptAÑO.Caption
ElseIf Me.OptGRADO.Value = True And Me.OptGRADO.Enabled = True Then
 men = Me.OptGRADO.Caption
ElseIf Me.OptSEMESTRE.Value = True And Me.OptSEMESTRE.Enabled = True Then
 men = Me.OptSEMESTRE.Caption
ElseIf Me.OptTRIMESTRE.Value = True And Me.OptTRIMESTRE.Enabled = True Then
 men = Me.OptTRIMESTRE.Caption
Else
 men = ""
End If

frmhome.TextBox5 = Me.CombLISTA.Text & IIf(men <> "", " " & men, "")

Unload frmnivel ' cierra formulario

Else

MsgBox "Datos Incompletos: Falta Informacion del Grado Aprobado", vbExclamation

End If

End Sub

Private Sub CombLISTA_Change()
    Dim nivel As String
    nivel = frmhome.ComboBox5.Value

    OptGRADO.Enabled = False
    OptTRIMESTRE.Enabled = False
    OptSEMESTRE.Enabled = False
    OptAÑO.Enabled = False

    If CombLISTA.Value = "Completa" Or CombLISTA.Value = "0" Then
    ' Deshabilitar opciones adicionales
    crt = True
    Else
        If nivel = "Primaria" Or nivel = "Secundaria" Or nivel = "Técnica" Then
            OptGRADO.Enabled = True
        End If

        If nivel = "Superior" Then
            OptTRIMESTRE.Enabled = True
            OptSEMESTRE.Enabled = True
            OptAÑO.Enabled = True
        End If
    End If
End Sub

Private Sub OptAÑO_Click()
crt = OptAÑO.Value
End Sub

Private Sub OptGRADO_Click()
crt = OptGRADO.Value
End Sub

Private Sub OptSEMESTRE_Click()
crt = OptSEMESTRE.Value
End Sub

Private Sub OptTRIMESTRE_Click()
crt = OptTRIMESTRE.Value
End Sub

Private Sub UserForm_Initialize()
    OptGRADO.Enabled = False
    OptTRIMESTRE.Enabled = False
    OptSEMESTRE.Enabled = False
    OptAÑO.Enabled = False
    crt = False
    
    Dim nivel As String
    nivel = frmhome.ComboBox5.Value
    Select Case nivel
        
        Case "Iletrado"
            CombLISTA.AddItem "0"
            ' Deshabilitar opciones adicionales
        
        Case "Primaria"
            CombLISTA.AddItem "1er"
            CombLISTA.AddItem "2do"
            CombLISTA.AddItem "3er"
            CombLISTA.AddItem "4to"
            CombLISTA.AddItem "5to"
            CombLISTA.AddItem "6to"
            CombLISTA.AddItem "Completa"
            ' Habilitar opciones específicas para Primaria si es necesario
        
        Case "Secundaria"
            CombLISTA.AddItem "7mo"
            CombLISTA.AddItem "8vo"
            CombLISTA.AddItem "9no"
            CombLISTA.AddItem "10mo"
            CombLISTA.AddItem "11er"
            CombLISTA.AddItem "Completa"
            ' Habilitar opciones específicas para Secundaria si es necesario
        
        Case "Técnica"
            CombLISTA.AddItem "7mo"
            CombLISTA.AddItem "8vo"
            CombLISTA.AddItem "9no"
            CombLISTA.AddItem "10mo"
            CombLISTA.AddItem "11er"
            CombLISTA.AddItem "12do"
            CombLISTA.AddItem "Completa"
            ' Habilitar opciones específicas para Técnica si es necesario
        
        Case "Superior"
            CombLISTA.AddItem "1er"
            CombLISTA.AddItem "2do"
            CombLISTA.AddItem "3er"
            CombLISTA.AddItem "4to"
            CombLISTA.AddItem "5to"
            CombLISTA.AddItem "6to"
            CombLISTA.AddItem "7mo"
            CombLISTA.AddItem "8vo"
            CombLISTA.AddItem "9no"
            CombLISTA.AddItem "10mo"
            CombLISTA.AddItem "11er"
            CombLISTA.AddItem "12do"
            CombLISTA.AddItem "13er"
            CombLISTA.AddItem "14to"
            CombLISTA.AddItem "15to"
            CombLISTA.AddItem "16to"
            CombLISTA.AddItem "17mo"
            CombLISTA.AddItem "18vo"
            CombLISTA.AddItem "19no"
            CombLISTA.AddItem "20mo"
            CombLISTA.AddItem "Completa"
        Case Else
    End Select
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Call CmdREGISTRAR_Click
If crt = False Then Cancel = True
End Sub

Private Sub CmdREGISTRAR_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    CmdREGISTRAR.BackColor = RGB(216, 228, 188)
End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    CmdREGISTRAR.BackColor = RGB(255, 255, 255)
End Sub

2.- Al colocar la Fecha de Nacimiento (TextBox6), Fecha de Ingreso (TextBox7) y Fecha de Accidente (TextBox8) y guardar el sistema me indica que “Se ha producido un error 13 en tiempo de ejecución: No coinciden los tipos” y conforme se ve en la imagen 2 solo limpia la información correspondiente a la Fecha de Nacimiento (TextBox6) en este caso el sistema también me guarda los datos en la Sheet10 (MATRZ).

Code:
Option Explicit
Dim IndiceEstado As Integer
Dim IndiceMunicipio As Integer

Private Sub ComboBox5_Change()
TextBox5 = ""
frmnivel.Show
End Sub
Private Sub CommandButton2_Click(): Calendario.Show: End Sub
Private Sub CommandButton3_Click(): Calendario1.Show: End Sub
Private Sub CommandButton4_Click(): Calendario2.Show: End Sub

Private Sub CommandButton5_Click()
    
' Guardar en Matriz
    If TextBox1 = "" Then MsgBox "Datos Incompletos: La Casilla Nombres y Apellidos Esta Vacia", vbExclamation: Exit Sub
    Dim uf As Integer
    With Sheet10
    uf = .Range("B" & Rows.Count).End(xlUp).Row + 1
    .Cells(uf, 2) = TextBox1
    .Cells(uf, 3) = TextBox2
    .Cells(uf, 4) = ComboBox1
    .Cells(uf, 5) = TextBox3
    .Cells(uf, 6) = ComboBox2
    .Cells(uf, 7) = ComboBox3
    .Cells(uf, 8) = ComboBox4
    .Cells(uf, 9) = TextBox4
    .Cells(uf, 10) = ComboBox5
    .Cells(uf, 11) = TextBox5
    .Cells(uf, 12) = ComboBox6
    .Cells(uf, 13) = ComboBox7
    .Cells(uf, 14) = ComboBox8
    .Cells(uf, 15) = ComboBox9
    .Cells(uf, 16) = TextBox9
    .Cells(uf, 17) = TextBox10
    .Cells(uf, 18) = TextBox11
    .Cells(uf, 19) = TextBox12
    .Cells(uf, 20) = TextBox13
    .Cells(uf, 21) = TextBox14
    .Cells(uf, 22) = ComboBox10
    .Cells(uf, 23) = TextBox15
    .Cells(uf, 24) = ComboBox11
    .Cells(uf, 25) = ComboBox12
    .Cells(uf, 26) = ComboBox13
    .Cells(uf, 27) = ComboBox14
    .Cells(uf, 28) = ComboBox15
    .Cells(uf, 29) = TextBox16
    .Cells(uf, 30) = TextBox17
    .Cells(uf, 31) = TextBox18
    .Cells(uf, 32) = TextBox19
    .Cells(uf, 33) = TextBox20
    .Cells(uf, 34) = TextBox21
    .Cells(uf, 35) = TextBox22
    .Cells(uf, 36) = TextBox23
    .Cells(uf, 37) = ComboBox16
    .Cells(uf, 38) = TextBox24
    .Cells(uf, 39) = ComboBox17
    
End With
        
' Limpiar TextBox y ComboBox
  Dim i As Long
    For i = 1 To 24
    Controls("TextBox" & i).Text = Empty
    If i < 18 Then Controls("ComboBox" & i).Text = Empty
    Next
End Sub

Private Sub TextBox8_Change()

' Descomponer Fecha en Dia/Mes/Año
    TextBox10 = WorksheetFunction.Proper(WeekdayName(Weekday(TextBox8), False, vbSunday))
    TextBox11 = WorksheetFunction.Proper(MonthName(Month(TextBox8), False))
    TextBox12 = UCase(Year(TextBox8))

'Calculo de la Antiguedad
If IsDate(TextBox8) And IsDate(TextBox7) Then
   TextBox9 = Round(DateDiff("m", CDate(TextBox7), CDate(TextBox8)) / 12, 2)
End If

End Sub

Private Sub TextBox6_Change()

' Calculo de la Edad
    ' Definir una variable para la fecha de nacimiento
    Dim fechaNacimiento As Date
    
    ' Asignar la fecha de nacimiento
   fechaNacimiento = TextBox6
    
    ' Obtener la fecha actual
    Dim fechaActual As Date
    fechaActual = Date
    
    ' Calcular la Edad
    Dim edad As Integer
    edad = Year(fechaActual) - Year(fechaNacimiento)
        
    ' Ajustar la edad si aún no ha tenido su cumpleaños este año
    If fechaNacimiento > DateSerial(Year(fechaActual), Month(fechaActual), Day(fechaNacimiento)) Then
        edad = edad - 1
    End If
    
    ' Mostrar el resultado en la ventana de mensajes
    TextBox3 = edad
        
End Sub

Private Sub UserForm_Initialize()
    
' Listas de Datos
    With Sheet9
    
        ComboBox1.RowSource = "'" & .Name & "'!B4:B" & .Range("B" & Rows.Count).End(xlUp).Row
        ComboBox2.RowSource = "'" & .Name & "'!C4:C" & .Range("C" & Rows.Count).End(xlUp).Row
        ComboBox3.RowSource = "'" & .Name & "'!D4:D" & .Range("D" & Rows.Count).End(xlUp).Row
        ComboBox4.RowSource = "'" & .Name & "'!E4:E" & .Range("E" & Rows.Count).End(xlUp).Row
        ComboBox5.RowSource = "'" & .Name & "'!F4:F" & .Range("F" & Rows.Count).End(xlUp).Row
        ComboBox6.RowSource = "'" & .Name & "'!G4:G" & .Range("G" & Rows.Count).End(xlUp).Row
        ComboBox7.RowSource = "'" & .Name & "'!I4:I" & .Range("I" & Rows.Count).End(xlUp).Row
        ComboBox10.RowSource = "'" & .Name & "'!Q4:Q" & .Range("Q" & Rows.Count).End(xlUp).Row
        ComboBox11.RowSource = "'" & .Name & "'!R4:R" & .Range("R" & Rows.Count).End(xlUp).Row
        ComboBox12.RowSource = "'" & .Name & "'!S4:S" & .Range("S" & Rows.Count).End(xlUp).Row
        ComboBox16.RowSource = "'" & .Name & "'!T4:T" & .Range("T" & Rows.Count).End(xlUp).Row
        ComboBox17.RowSource = "'" & .Name & "'!U4:U" & .Range("U" & Rows.Count).End(xlUp).Row
        
    End With
      
End Sub

Private Sub ComboBox8_Change()

'ComboBox Municipio del Lesionado
IndiceEstado = ComboBox7.ListIndex + 1
Dim i As Integer
ComboBox8.Clear
With Sheets("LISTAS")
    For i = 4 To .Range("J" & Rows.Count).End(xlUp).Row 'i = 4 = fila 4
        If .Cells(i, 10).Value = "IndiceEstado" Then 'i =10= columna 10
            ComboBox8.AddItem .Cells(i, 12).Value 'i = 11= columna 12
        End If
    Next i
End With
End Sub

Private Sub ComboBox9_Change()

'ComboBox Parroquia del Lesionado
IndiceMunicipio = ComboBox8.ListIndex + 1
Dim j As Integer
ComboBox9.Clear
With Sheets("LISTAS")
    For j = 4 To .Range("M" & Rows.Count).End(xlUp).Row 'i = 4 = fila 4
         If .Cells(j, 13).Value = IndiceEstado And .Cells(j, 14).Value = IndiceMunicipio Then 'j = 13= columna 13, 'j = 14= columna 14
          ComboBox9.AddItem .Cells(j, 16).Value 'j = 16= columna 16
        End If
    Next j
End With
End Sub

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)

' Decimales en Cedula
Me.TextBox2 = Format(Me.TextBox2, "##,##00")
End Sub

Private Sub TextBox9_Exit(ByVal Cancel As MSForms.ReturnBoolean)

' Decimales en Antiguedad
Me.TextBox9 = Format(Me.TextBox9, "##,##0.00")
End Sub

Private Sub TextBox15_Exit(ByVal Cancel As MSForms.ReturnBoolean)

' Hora Militar
If Len(TextBox15) > 0 Then TextBox15 = Format(CDate(TextBox15), "hh:mm")
End Sub

Private Sub CommandButton7_Click()

' Ver Excel
Application.Visible = True
Sheets("MATRIZ").Select
Unload Me
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

' Ver Excel
Application.Visible = True
Sheets("MATRIZ").Select
Unload Me
End Sub

Private Sub CommandButton10_Click()

' Ocultar Excel
Application.Visible = False
frmhome.Show
End Sub

En ambos casos el sistema está configurado para que al dar click en el Botón guardar (CmdGuardar) los datos se guarden y limpien simultáneamente.

@shknbk2 me ayudas? tu diagnóstico y solución en una consulta similar fue muy efectivo

Mucho les sabre agradecer la ayuda que me puedan brindar
 

Attachments

  • Imagen1.jpg
    Imagen1.jpg
    80.3 KB · Views: 6
  • Imagen2.jpg
    Imagen2.jpg
    58.3 KB · Views: 6
  • Imagen3.jpg
    Imagen3.jpg
    84.8 KB · Views: 6

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,215,097
Messages
6,123,076
Members
449,094
Latest member
mystic19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top