Error al limpiar un combobox

Tengo el siguiente código

Private Sub ComboBox1_change()
Rem capturamos los daos de la hoja
Cells(ComboBox1.ListIndex + 1, 1).Select ( me da error )
TextBox2 = ActiveCell.Offset(0, 1)
TextBox3 = ActiveCell.Offset(0, 2)
TextBox4 = ActiveCell.Offset(0, 3)
TextBox5 = ActiveCell.Offset(0, 4)
TextBox7 = ActiveCell.Offset(0, 5)
TextBox6 = ActiveCell.Offset(0, 6)
End Sub

Private Sub CommandButton1_Click()
Rem grabar datos modificados
Cells(ComboBox1.ListIndex + 1, 1).Select
ActiveCell.Offset(0, 4) = (TextBox5)
ActiveCell.Offset(0, 5) = (TextBox7)
ActiveCell.Offset(0, 6) = (TextBox6)

Rem ComboBox1.Clear
TextBox2 = ""
TextBox3 = ""
TextBox4 = ""
TextBox5 = ""
TextBox6 = ""
ComboBox1.SetFocus

End Sub

Private Sub TextBox3_Change()
TextBox3.Text = Format$(TextBox3.Text, "##,##0.00")
End Sub

Private Sub TextBox5_Change()
If TextBox5 = 0 Then
TextBox7 = Val(24)
End If
End Sub

El problema es que al intentar actualizar otro dat me da erro en la linea

Cells(ComboBox1.ListIndex + 1, 1).Select ( me da error )

No entiendo la razón, si alguien pudiera ayudarme

1 Respuesta

Respuesta
1

H0la Ignacio:

Cuando borras el contenido del cuadro de texto el valor del ListIndex es -1 por lo tanto, si caes en este cas, estarías tratando de seleccionar la celda Cells(0,1), lo más probable es que eso sea el error.

Revísalo y si no val por ahí, me lo comentas.

S@lu2

No continua con el mismo problema aunque lo mando a otra celda 

¿Has revisado cuál es el valor de ListIndex en el depurador al momento del error?

¿Puedes subir una captura del error?

te mando imagen que me pides muchas gracias

Tratemos de atrapar el error.

Agrega estas líneas al código, a ver qué te dice

If ComboBox1.ListIndex>=0 Then
    Cells(ComboBox1.ListIndex + 1, 1).Select
Else
    Msgbox "El índice es inválido: " & ComboBox1.ListIndex
End If

Si en lugar de seleccionar la celda aparece el mensaje, entonces tenemos que ver cómo solucionar ese caso.

S@lu2

Este es el error nuevo me sigue mandando a la fila -1 al introducir tu código

Te remito todo el código

Private Sub UserForm_Initialize()
TextBox6 = Date

For i = 1 To 600
If IsEmpty(Cells(1, i)) Then
Cells(1, i).Value = ("0")
End If
Next i

End Sub

Private Sub ComboBox1_Enter()
Rem cargar datos al cobobox
On Error Resume Next
ComboBox1.Clear
Sheets("Mov").Select
Range("a1").Select
Do While Not IsEmpty(ActiveCell)
ComboBox1.AddItem ActiveCell.Value
ActiveCell.Offset(1, 0).Select
Loop

End Sub
Private Sub ComboBox1_change()
Rem capturamos los daos de la hoja

 Cells(ComboBox1.ListIndex + 1, 1).Select
TextBox2 = ActiveCell.Offset(0, 1)
TextBox3 = ActiveCell.Offset(0, 2)
TextBox4 = ActiveCell.Offset(0, 3)
TextBox5 = ActiveCell.Offset(0, 4)
TextBox7 = ActiveCell.Offset(0, 5)
TextBox6 = ActiveCell.Offset(0, 6)
End Sub

Private Sub CommandButton1_Click()
Rem grabar datos modificados
Cells(ComboBox1.ListIndex + 1, 1).Select
ActiveCell.Offset(0, 4) = (TextBox5)
ActiveCell.Offset(0, 5) = (TextBox7)
ActiveCell.Offset(0, 6) = (TextBox6)

ComboBox1.Clear
TextBox2 = ""
TextBox3 = ""
TextBox4 = ""
TextBox5 = ""
TextBox6 = ""
ComboBox1.SetFocus

End Sub

Private Sub TextBox3_Change()
TextBox3.Text = Format$(TextBox3.Text, "##,##0.00")
End Sub


Private Sub TextBox5_Change()
If TextBox5 = 0 Then
TextBox7 = Val(24)
End If
End Sub

Prueba reemplazando estos dos eventos.

Private Sub ComboBox1_change()
    Rem capturamos los daos de la hoja
    If (ComboBox1.ListIndex >= 0) Then
        Cells(ComboBox1.ListIndex + 1, 1).Select
        TextBox2 = ActiveCell.Offset(0, 1)
        TextBox3 = ActiveCell.Offset(0, 2)
        TextBox4 = ActiveCell.Offset(0, 3)
        TextBox5 = ActiveCell.Offset(0, 4)
        TextBox7 = ActiveCell.Offset(0, 5)
        TextBox6 = ActiveCell.Offset(0, 6)
    Else
        TextBox2 = ""
        TextBox3 = ""
        TextBox4 = ""
        TextBox5 = ""
        TextBox7 = ""
        TextBox6 = ""
    End If
End Sub
Private Sub CommandButton1_Click()
    Rem grabar datos modificados
    Cells(ComboBox1.ListIndex + 1, 1).Select
    ActiveCell.Offset(0, 4) = (TextBox5)
    ActiveCell.Offset(0, 5) = (TextBox7)
    ActiveCell.Offset(0, 6) = (TextBox6)
    ComboBox1.Clear
    ComboBox1.SetFocus
    'Esto lo haremos en el interior del Combobox1'
    'TextBox2 = ""
    'TextBox3 = ""
    'TextBox4 = ""
    'TextBox5 = ""
    'TextBox6 = ""
End Sub

Básicamente, en el evento del combobox estoy validando cuando esté vacío, si esto ocurre, limpio todos los textboxs. Es por eso que puse con comentarios esas líneas en el evento Click del botón.

S@lu2

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas