Como condicionar una operación de una macro

Tengo varios textbox el cual ya coloque condición de que si no estaba lleno arrojara un mensaje y lo colocara en el mismo textbox así a varios textbox, entonces cuando ninguno esta aparecen todos los mensajes; adicional a esto coloque que fuera a una hoja y me agregara una fila; la pregunta es: que código tengo que usar para que si en el caso que no llenaran un textbox no hiciera la operación de agregar filas en la otra hoja

Este es el código que tengo

Private Sub CommandButton1_Click()
Dim ren As Long
If tipo_operacion.Text = "" Then
MsgBox ("INGRESA TIPO DE OPERACION")
tipo_operacion.SetFocus
End If
If orden_p.Text = "" Then
MsgBox ("INGRESA LA ORDEN DE PRODUCCION")
orden_p.SetFocus
End If
If origen.Text = "" Then
MsgBox ("INGRESA TIPO DE ORIGEN")
origen.SetFocus
End If
If maq.Text = "" Then
MsgBox ("INGRESA LA MAQUINA")
maq.SetFocus
End If
If cod_ref.Text = "" Then
MsgBox ("INGRESA EL CODIGO DE LA REFERENCIA")
cod_ref.SetFocus
End If
If prod_ref.Text = "" Then
MsgBox ("INGRESA EL PRODUCTO DE REFERENCIA")
prod_ref.SetFocus
End If
If c_total.Text = "" Then
MsgBox ("INGRESA LA CANTIDAD PRODUCIDA")
c_total.SetFocus
End If
If s_total.Text = "" Then
MsgBox ("INGRESA EL SCRAP")
s_total.SetFocus
End If

Application.ScreenUpdating = False

Sheets("BD INGRESO PRODUCCION").Select
Range("A3:X3").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("C3").Select

1 Respuesta

Respuesta
1

H o l a: Estoy agregando la variable werr para activarla en caso de faltar algún dato. Después valido si la variable es verdadera, si es así, entonces se sale de la rutina. Si no hay errores de captura entonces continúa con el código para agregar la fila.

Private Sub CommandButton1_Click()
    Dim ren As Long
    Dim werr As Boolean
    werr = False
    If tipo_operacion.Text = "" Then
        MsgBox ("INGRESA TIPO DE OPERACION")
        tipo_operacion.SetFocus
        werr = True
    End If
    If orden_p.Text = "" Then
        MsgBox ("INGRESA LA ORDEN DE PRODUCCION")
        orden_p.SetFocus
        werr = True
    End If
    If origen.Text = "" Then
        MsgBox ("INGRESA TIPO DE ORIGEN")
        origen.SetFocus
        werr = True
    End If
    If maq.Text = "" Then
        MsgBox ("INGRESA LA MAQUINA")
        maq.SetFocus
        werr = True
    End If
    If cod_ref.Text = "" Then
        MsgBox ("INGRESA EL CODIGO DE LA REFERENCIA")
        cod_ref.SetFocus
        werr = True
    End If
    If prod_ref.Text = "" Then
        MsgBox ("INGRESA EL PRODUCTO DE REFERENCIA")
        prod_ref.SetFocus
        werr = True
    End If
    If c_total.Text = "" Then
        MsgBox ("INGRESA LA CANTIDAD PRODUCIDA")
        c_total.SetFocus
        werr = True
    End If
    If s_total.Text = "" Then
        MsgBox ("INGRESA EL SCRAP")
        s_total.SetFocus
        werr = True
    End If
    '
    If werr Then Exit Sub
    '
    Application.ScreenUpdating = False
    Sheets("BD INGRESO PRODUCCION").Select
    Range("A3:X3").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    Range("C3").Select
End Sub
'S aludos. Dante Amor. Recuerda valorar la respuesta. G racias

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas