Mismo código para varias hojas en un libro

Private Sub CommandButton1_Click()
Sheets("ene17").Activate
If TextBox2 = "" Or TextBox3 = "" Or TextBox5 = "" _
Or TextBox7 = "" Then
MsgBox "campo vacio "
TextBox3.SetFocus
Else
Range("A" & Cells.Rows.Count).End(xlUp).Offset(1).Select
ActiveCell = TextBox2.Value
ActiveCell.Offset(0, 1) = TextBox3.Value
ActiveCell.Offset(0, 2) = TextBox5.Value
ActiveCell.Offset(0, 3) = TextBox15.Value
ActiveCell.Offset(0, 4) = TextBox7.Value
ActiveCell.Offset(0, 5) = TextBox13.Value
ActiveCell.Offset(0, 6) = TextBox14.Value
MsgBox "ingreso exitoso", vbInformation, "Tin Logistic"
TextBox2 = ""
TextBox3 = ""
TextBox5 = ""
TextBox15 = ""
TextBox7 = ""
TextBox13 = ""
TextBox14 = ""
TextBox2.SetFocus
End If
End Sub

Puedo utilizar este código para las hojas siguientes feb17, mar17 así sucesivamente hasta dic17 y en que parte de la línea las incluiría, soy un principiante en esto de los códigos se los agradezco mucho por su atención

Juan manuel

1 respuesta

Respuesta
1

Te pongo un ejemplo, quizás los expertos tengan una solución mejor, pero creo que esta te valdrá:

Private Sub CommandButton1_Click()
For Each hoja In Worksheets  '' --> Recorre todas las hojas del libro
     If hoja.Name = "ene17" _  '' --> Si la hoja existe, pasa al codigo.
       Or hoja.Name = "fe17" _
       Or hoja.Name = "mar17" Then
Sheets("" & hoja.Name).Activate
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If TextBox2 = "" Or TextBox3 = "" Or TextBox5 = "" _
Or TextBox7 = "" Then
MsgBox "campo vacio "
TextBox3.SetFocus
Else
Range("A" & Cells.Rows.Count).End(xlUp).Offset(1).Select
ActiveCell = TextBox2.Value
ActiveCell.Offset(0, 1) = TextBox3.Value
ActiveCell.Offset(0, 2) = TextBox5.Value
ActiveCell.Offset(0, 3) = TextBox15.Value
ActiveCell.Offset(0, 4) = TextBox7.Value
ActiveCell.Offset(0, 5) = TextBox13.Value
ActiveCell.Offset(0, 6) = TextBox14.Value
MsgBox "ingreso exitoso", vbInformation, "Tin Logistic"
TextBox2 = ""
TextBox3 = ""
TextBox5 = ""
TextBox15 = ""
TextBox7 = ""
TextBox13 = ""
TextBox14 = ""
TextBox2.SetFocus
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End If
Next
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas