Búsqueda en rango de 2 columnas con msgbox entre cambio de columna.

Dante Amor amigo ayúdame con esto que me esta volviendo loca; (necesito agregar una condición más te la escribo al final)...

Private Sub CommandButton1_Click()
Application.ScreenUpdating = False

Dim COLUM As Range
Set COLUM = ThisWorkbook.Sheets("STOCK").Range("B6:B5000")
Set BUSCA = COLUM.Find(TextBox1.Value, lookat:=xlWhole)

'***VALIDACIONES
'1-SI EL COD ES INVALIDO O ESTA VACIO
If TextBox1.Value = "" Then
MsgBox "Introduce el numero de orden."
TextBox1.SetFocus
Exit Sub
ElseIf Not IsNumeric(TextBox1.Value) Then
MsgBox "Orden invalida."
TextBox1.Value = ""
ComboBox1.Value = ""
TextBox1.SetFocus
Exit Sub
'2-Valida que halla observacion (opciones listadas en el combobox)
ElseIf ComboBox1.Value = "" Then
MsgBox "Seleccione una observacion."
Exit Sub

'***RESPUESTAS POSITIVAS (si BUSCA encuentra el valor)
'1-Copia y pega la fecha
ElseIf Not BUSCA Is Nothing Then
ThisWorkbook.Sheets("STOCK").Range("E1").Copy
BUSCA.Select
ActiveCell.Offset(0, 3).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'2-CAMBIO EL ESTATUS DE LA CORDEN POR INGRESADO MOVIENDOLO DE COLUMNA
ActiveCell.Select
ActiveCell.Offset(0, -2).Select
ActiveCell.Copy
ActiveCell.Offset(0, 1).Select
ActiveCell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=True, Transpose:=True
ActiveCell.Offset(0, -1).ClearContents
'3-INGRESO  OBSERVACIONES
ActiveCell.Offset(0, 3).Value = Me.ComboBox1.Value
Call SOLICITUD_LOGISTICA ´(es un subcomando que envia un correo)
Exit Sub

'***NEGATIVAS
'1-SI NO LO ENCUENTRA
Else
MsgBox "La orden no existe."
TextBox1.Value = ""
ComboBox1.Value = ""
Exit Sub
End If

AHORA TODO LO QUE ESCRIBO ABAJO NO SE COMO INCORPORARLO, YA HE INTENTADO EN TODOS LADOS (QUIZÁS SEA QUE ESTOY ERRANDO EN LA FORMA DEL CÓDIGO) Lo que quiero hacer es que cuando BUSCA encuentre el valor revise la columna siguiente de la derecha y si esta vacía me de el mensaje de abajo y si no esta vacía que haga lo que dice arriba (ElseIf Not BUSCA Is Nothing Then)

help please!! 

'Set DDD = ActiveCell.Offset(0, 1)
' BUSCA.Select
' If DDD = "" Then
' MsgBox "La Orden ya fue ingresada anteriormente."
' TextBox1.Value = ""
' ComboBox1.Value = ""
' Exit Sub
' End If
End Sub

1 Respuesta

Respuesta
1

Te ayudo con todo el código, para organizarlo y estructurarlo un poco.

- En ocasiones es conveniente utilizar Exit Sub, para que el código no continúe. Por ejemplo en las validaciones, puedes hacer una validación, si no se cumple, entonces terminas el proceso.

    If TextBox1.Value = "" Then
        MsgBox "Introduce el numero de orden."
        TextBox1.SetFocus
        Exit Sub
    End If

De esa forma no es necesario poner Else, ni tampoco ElseIf, porque ya no se van a realizar más acciones, además, al manejar anidaciones de If hace más complejo el código.


- Sin embargo, en ocasiones es necesario utilizar la estructura If - Else - End If, por ejemplo, si la celda está vacía realiza unas acciones, si no está vacía realiza otras acciones.

        'revisa celda de la derecha
        If h1.Cells(b.Row, "C").Value = "" Then
            MsgBox "La Orden ya fue ingresada anteriormente."
            TextBox1.Value = ""
            ComboBox1.Value = ""
        Else
            h1.Cells(b.Row, "E").Value = h1.Range("E1").Value
            h1.Cells(b.Row, "D").Value = h1.Cells(b.Row, "C").Value
            h1.Cells(b.Row, "C").Value = ""
            h1.Cells(b.Row, "G").Value = ComboBox1.Value
        End If

También te aconsejo no utilizar las sentencias para seleccionar la celda, ni tampoco utilices offset. Cuando buscas, en el objeto b tienes el objeto celda, es decir, sabes en cuál fila encontró al dato, conoces la columna, la hoja, el formato de la celda, etc.

Revisa en la macro cómo utilizo el objeto b una vez que ya encontré el dato.


Esta es la macro completa:

Private Sub CommandButton1_Click()
'Act.Por Dante Amor
'
    '***VALIDACIONES
    '1-SI EL COD ES INVALIDO O ESTA VACIO
    If TextBox1.Value = "" Then
        MsgBox "Introduce el numero de orden."
        TextBox1.SetFocus
        Exit Sub
    End If
    If Not IsNumeric(TextBox1.Value) Then
        MsgBox "Orden invalida."
        TextBox1.Value = ""
        ComboBox1.Value = ""
        TextBox1.SetFocus
        Exit Sub
    End If
    '
    '2-Valida que haya observacion (opciones listadas en el combobox)
    If ComboBox1.Value = "" Or ComboBox1.ListIndex = 1 Then
        MsgBox "Seleccione una observacion."
        Exit Sub
    End If
    '
    Set l1 = ThisWorkbook           'Establece Libro
    Set h1 = l1.Sheets("STOCK")     'Establece Hoja de l1
    Set r = h1.Columns("B")         'Establece Rango de h1
    Set b = r.Find(TextBox1.Value, lookat:=xlWhole)
    '
    If Not b Is Nothing Then
        'revisa celda de la derecha
        If h1.Cells(b.Row, "C").Value = "" Then
            MsgBox "La Orden ya fue ingresada anteriormente."
            TextBox1.Value = ""
            ComboBox1.Value = ""
        Else
            h1.Cells(b.Row, "E").Value = h1.Range("E1").Value
            h1.Cells(b.Row, "D").Value = h1.Cells(b.Row, "C").Value
            h1.Cells(b.Row, "C").Value = ""
            h1.Cells(b.Row, "G").Value = ComboBox1.Value
            MsgBox "Orden Actualizada", vbInformation
        End If
    Else
        MsgBox "La orden no existe."
        TextBox1.Value = ""
        ComboBox1.Value = ""
    End If
End Sub
'


'.[Sal u dos. Dante Amor. No olvides valorar la respuesta. 
'.[Avísame cualquier duda

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas