Condicion IF tipo bucle registro de mercancia

Requiero de su ayuda para realizar el siguiente codigo, les adjunto imagen de como esta el objeto y les explico que quiero hacer, por cada concepto de consumible si tiene cantidad me la agregue en una tabla, pero si no tiene cantidad no agregara nada. Pero quisiera saber si puede ser tipo bucle ya que eh creado 1 codigo por cada combobox. Ejemplo:

Private Sub consumible1()

If TextBox7 > 0 Then
With Hoja16
.Select
Range("A2").Select
Selection.EntireRow.Insert

ActiveSheet.Cells(2, 1) = tecnico
ActiveSheet.Cells(2, 2) = Format(fecha.Value, "mm/dd/yyyy")
ActiveSheet.Cells(2, 3) = econo.Text
ActiveSheet.Cells(2, 4) = client
ActiveSheet.Cells(2, 5) = department
ActiveSheet.Cells(2, 6) = modelo
ActiveSheet.Cells(2, 7) = folio.Text
ActiveSheet.Cells(2, 8) = ComboBox1.Text
ActiveSheet.Cells(2, 9) = TextBox7.Text
End With
End If
End Sub

Private Sub consumible2()

If TextBox8 > 1 Then
With Hoja16
.Select
Range("A2").Select
Selection.EntireRow.Insert

ActiveSheet.Cells(2, 1) = tecnico
ActiveSheet.Cells(2, 2) = Format(fecha.Value, "mm/dd/yyyy")
ActiveSheet.Cells(2, 3) = econo.Text
ActiveSheet.Cells(2, 4) = client
ActiveSheet.Cells(2, 5) = department
ActiveSheet.Cells(2, 6) = modelo
ActiveSheet.Cells(2, 7) = folio.Text
ActiveSheet.Cells(2, 8) = ComboBox2.Text
ActiveSheet.Cells(2, 9) = TextBox8.Text
End With
End If
End Sub

Private Sub consumible3()

With Hoja16
.Select
Range("A2").Select
Selection.EntireRow.Insert

ActiveSheet.Cells(2, 1) = tecnico
ActiveSheet.Cells(2, 2) = Format(fecha.Value, "mm/dd/yyyy")
ActiveSheet.Cells(2, 3) = econo.Text
ActiveSheet.Cells(2, 4) = client
ActiveSheet.Cells(2, 5) = department
ActiveSheet.Cells(2, 6) = modelo
ActiveSheet.Cells(2, 7) = folio.Text
ActiveSheet.Cells(2, 8) = ComboBox3.Text
ActiveSheet.Cells(2, 9) = TextBox9.Text
End With
End Sub

Y asi sucesivamente con los 9 combobox lo cual considero es mucho codigo para lo que realmente deseo hacer, porfavor me podrian asesorar se podria crear un solo codigo que valide cada casilla de cantidad y si tiene informacion registre el consumible y la cantidad.

2 respuestas

Respuesta
1

En tu código tienes esto:

Private Sub consumible1()

Es una subrutina? cómo la mandas llamar o en qué momento la ejecutas?

Es un formulario de registro de un servicio donde se coloca un ID identificador del cliente y se carga la información, se llenan los campos y se le da click a un boton de registro para que guarde toda la informacion capturada, entre esas estan estos combobox. el codigo actual esta de la siguiente manera:

Private Sub guardar_Click()

If client = "" Then
MsgBox "Debe llenar formulario primero", vbExclamation, "AVISO"

Else
resultado = MsgBox("¿ESTAN CORRECTOS LOS DATOS?", vbYesNo + vbExclamation, "CAPTURA DE REPORTE")
Select Case resultado

Case vbYes:

With Hoja1
.Select
Range("A2").Select
Selection.EntireRow.Insert

ActiveSheet.Cells(2, 1) = cant.Text
ActiveSheet.Cells(2, 2) = tecnico
ActiveSheet.Cells(2, 3) = tecasignado
ActiveSheet.Cells(2, 4) = Format(fecha.Value, "mm/dd/yyyy")
ActiveSheet.Cells(2, 5) = reporte.Text
ActiveSheet.Cells(2, 6) = folio.Text
ActiveSheet.Cells(2, 7) = econo.Text
ActiveSheet.Cells(2, 8) = client
ActiveSheet.Cells(2, 9) = lectura.Text
ActiveSheet.Cells(2, 10) = department
ActiveSheet.Cells(2, 11) = modelo
ActiveSheet.Cells(2, 12) = hllegada
ActiveSheet.Cells(2, 13) = TextBox6
ActiveSheet.Cells(2, 14) = fexpuesta
If opt1.Value = True Then ActiveSheet.Cells(2, 15) = 0.25
If opt2.Value = True Then ActiveSheet.Cells(2, 15) = 0.5
If opt3.Value = True Then ActiveSheet.Cells(2, 15) = 0.75
If opt4.Value = True Then ActiveSheet.Cells(2, 15) = 1
If opt12.Value = True Then ActiveSheet.Cells(2, 15) = 1.25
If opt13.Value = True Then ActiveSheet.Cells(2, 15) = 1.5
If opt14.Value = True Then ActiveSheet.Cells(2, 15) = 1.75
If opt15.Value = True Then ActiveSheet.Cells(2, 15) = 2
If opt5.Value = True Then ActiveSheet.Cells(2, 16) = "PREVENTIVO"
If opt6.Value = True Then ActiveSheet.Cells(2, 16) = "CORRECTIVO"
If opt7.Value = True Then ActiveSheet.Cells(2, 16) = "FALLA DE USUARIO"
If opt8.Value = True Then ActiveSheet.Cells(2, 16) = "CONECTIVIDAD"
If opt9.Value = True Then ActiveSheet.Cells(2, 16) = "FALLA DE EQUIPO"
If opt10.Value = True Then ActiveSheet.Cells(2, 16) = "PENDIENTE"
If opt11.Value = True Then ActiveSheet.Cells(2, 16) = "RECARGA DE TONER"
If CheckBox1.Value = True Then ActiveSheet.Cells(2, 17) = 1
If CheckBox2.Value = True Then ActiveSheet.Cells(2, 18) = 1
If CheckBox3.Value = True Then ActiveSheet.Cells(2, 19) = 1
If CheckBox4.Value = True Then ActiveSheet.Cells(2, 20) = 1
If CheckBox5.Value = True Then ActiveSheet.Cells(2, 21) = 1
If CheckBox6.Value = True Then ActiveSheet.Cells(2, 22) = 1
If CheckBox11.Value = True Then ActiveSheet.Cells(2, 23) = 1
If CheckBox12.Value = True Then ActiveSheet.Cells(2, 24) = 1
If CheckBox13.Value = True Then ActiveSheet.Cells(2, 25) = 1
If CheckBox14.Value = True Then ActiveSheet.Cells(2, 26) = 1
ActiveSheet.Cells(2, 35) = TextBox4
ActiveSheet.Cells(2, 36) = TextBox5
ActiveSheet.Cells(2, 38) = cartucho
consumible1

consumible2

consumible3

End With

End Select
End If
End Sub

Cambia estas líneas

Consumible1
Consumible2
Consumible3

Por esto:

 'Ciclo para los 9 combobox
    For i = 1 To 9
        If Me.Controls("ComboBox" & i).Value <> "" Then
            If Me.Controls("TextBox" & i + 6).Value > 0 Then
                Call Consumible_General(i)
            End If
        End If
    Next

También elimina de tu código las subrutinas

Private Sub consumible1()
Private Sub consumible2()

Agrega la siguiente subrutina

Private Sub Consumible_General(i)
    With Hoja16
        .Select
        Range("A2").Select
        Selection.EntireRow.Insert
        ActiveSheet.Cells(2, 1) = tecnico
        ActiveSheet.Cells(2, 2) = Format(fecha.Value, "mm/dd/yyyy")
        ActiveSheet.Cells(2, 3) = econo.Text
        ActiveSheet.Cells(2, 4) = client
        ActiveSheet.Cells(2, 5) = department
        ActiveSheet.Cells(2, 6) = modelo
        ActiveSheet.Cells(2, 7) = folio.Text
        ActiveSheet.Cells(2, 8) = Me.Controls("ComboBox" & i).Text
        ActiveSheet.Cells(2, 9) = Me.Controls("TextBox" & i + 6).Text
    End With
End Sub

Para que funcione el ciclo, debe mantenerse la siguiente relación:

Combobox1 con textbox7

Combobox2 con textbox8

Combobox3 con textbox9

Combobox4 con textbox10

Combobox5 con textbox11

Combobox6 con textbox12

Combobox7 con textbox13

Combobox8 con textbox14

Combobox9 con textbox15


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

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas