¿ Como hacer que un Combobox cambie al siguiente item automáticamente en VBA?

Les cuento.. Estoy haciendo un formulario que me permite sacar el inventario y venta de cigarrillos, tengo todo el código listo, pero para hacer más eficiente mi formulario, quisiera que el Combobox cambie al siguiente nombre de cigarrillo al momento de guardar la cantidad ingresada a través de un textbox, para así ahorrarse tiempo y posibles equivocaciones a la hora de cambiar manualmente al siguiente paquete de cigarros y solo digitar la cantidad de cada uno de estos.

Es por esto que les pido expertos si conocen algún código que me sirva para que esta acción se realice automáticamente. Les dejo una imagen de mi formulario para que se hagan una idea. De antemano muchas gracias.

1 Respuesta

Respuesta
1

Te anexo el código para el botón Guardar. En el código te puse un comentario para que ahí pongas tu código para guardar la información en la hoja.

Private Sub CommandButton1_Click()
'Por.Dante Amor
    If ComboBox1.ListIndex = -1 Then
        MsgBox "Debes seleccionar un tipo de cigarrillo"
        Exit Sub
    End If
    If ComboBox1.Value = "" Then
        MsgBox "Captura un tipo de cigarrillo"
        Exit Sub
    End If
    If TextBox1.Value = "" Then
        MsgBox "Captura una cantidad"
        Exit Sub
    End If
    numlist = ComboBox1.ListIndex
    '
    'Aquí debes poner tu código para pasar los datos a la hoja
    '
    'En esta parte se cambia al siguiente dato del combo
 'si está en el último dato del combo, entonces te pone el primer dato
    If numlist < ComboBox1.ListCount - 1 Then
        ComboBox1.Value = ComboBox1.List(ComboBox1.ListIndex + 1)
    Else
        ComboBox1.Value = ComboBox1.List(0)
    End If
    TextBox1.Value = ""
    TextBox1.SetFocus
End Sub
'S aludos. Dante Amor. Recuerda valorar la respuesta. G racias

Muchísimas gracias Dante! Corre perfecto :)

Pero cuando llega al último ítem y lo guardo me genera un error que dice " no se puede obtener la propiedad list. Indice de matriz de propiedades no válido"... y me pide depurar esta línea de código

ComboBox1.Value = ComboBox1.List(ComboBox1.ListIndex + 1)

Necesito revisar el código que pusiste, tal vez estás borrando el dato del combo y por eso no tenemos información.

Puedes poner tu código completo.

Sí, ahí va.

Private Sub CommandButton4_Click()

If ComboBox1 = "KENT 1 20" Then
ActiveSheet.Range("C5").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "KENT 4 20" Then
ActiveSheet.Range("C6").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "LUCKY BLUE 20" Then
ActiveSheet.Range("C7").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "LUCKY ROJO 20" Then
ActiveSheet.Range("C8").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "LUCKY CLIC 20" Then
ActiveSheet.Range("C9").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "KENT BELMONT 20" Then
ActiveSheet.Range("C10").Value = TextBox1.Text
TextBox1.FontSize = 12
TextBox1.Text = Empty
End If

If ComboBox1 = "LUCKY AMARILLO 20" Then
ActiveSheet.Range("C11").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "LUCKY BERRIES 20" Then
ActiveSheet.Range("C12").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "DUNHILL 20" Then
ActiveSheet.Range("C13").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "PALL MALL ROJO 20" Then
ActiveSheet.Range("C14").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "PALL MALL GRIS 20" Then
ActiveSheet.Range("C15").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "PALL MALL AZUL 20" Then
ActiveSheet.Range("C16").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "PALL MALL CLIC 20" Then
ActiveSheet.Range("C17").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "PALL MALL VERDE 20" Then
ActiveSheet.Range("C18").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "LUCKY INDIGO 20" Then
ActiveSheet.Range("C19").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "LUCKY ROJO BLANDO 20" Then
ActiveSheet.Range("C20").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "PALL MALL AZUL 10" Then
ActiveSheet.Range("C21").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "BELMONT LIGHT 10" Then
ActiveSheet.Range("C22").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "PALL MALL VERDE 10" Then
ActiveSheet.Range("C23").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "PALL MALL CLIC 10" Then
ActiveSheet.Range("C24").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "LUCKY AMARILLO 10" Then
ActiveSheet.Range("C25").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "LUCKY BERRIES 10" Then
ActiveSheet.Range("C26").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "KENT 4 10" Then
ActiveSheet.Range("C27").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "LUCKY BLUE 10" Then
ActiveSheet.Range("C28").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "LUCKY CLIC 10" Then
ActiveSheet.Range("C29").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "PHILIPS MORRIS" Then
ActiveSheet.Range("C30").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "CARNIVAL" Then
ActiveSheet.Range("C31").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "FOX" Then
ActiveSheet.Range("C32").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "LUCKY INDIGO 10" Then
ActiveSheet.Range("C33").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If ComboBox1 = "PALL MALL PLOMO 10" Then
ActiveSheet.Range("C34").Value = TextBox1.Text
TextBox1.Text = Empty
End If

If numlist < ComboBox1.ListCount - 1 Then
ComboBox1.Value = ComboBox1.List(ComboBox1.ListIndex + 1)
Else
ComboBox1.Value = ComboBox1.List(0)
End If
TextBox1.Value = ""
TextBox1.SetFocus


Columns("C:C").Select
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
End With
Selection.Font.Bold = False
End Sub

Tampoco me corrió con los cuadros de mensajes, por eso no los puse.

Pero tienes que poner la información como te la puse.

Omitiste esta línea:

numlist = ComboBox1. ListIndex

Claro que funciona con los cuadros de mensajes.

El éxito de las macros se debe a la buena validación de los datos, si no se validan los datos, más adelante tendrás problemas

Antes de esta línea:

If ComboBox1 = "KENT 1 20" Then

Tienes que poner estas líneas:

'Por.Dante Amor
    If ComboBox1.ListIndex = -1 Then
        MsgBox "Debes seleccionar un tipo de cigarrillo"
        Exit Sub
    End If
    If ComboBox1.Value = "" Then
        MsgBox "Captura un tipo de cigarrillo"
        Exit Sub
    End If
    If TextBox1.Value = "" Then
        MsgBox "Captura una cantidad"
        Exit Sub
    End If
    numlist = ComboBox1. ListIndex

Lo que hacen estas líneas es validar que elijas un dato del combo y que pongas una cantidad, si no elijes un dato del combo, no se puede saber en cuál dato del combo te encuentras y por lo tanto, no podrías pasar al siguiente dato.


Prueba nuevamente y dime en dónde tienes dificultades.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas