Como utilizar un código para registrar en una hoja, que me sirva para varias?

Tengo un código con el que registro en una hoja (Datos), pero me gustaría aprovechar el mismo código para 3 o 4 hojas más. ¿Cómo podría hacer?

Private Sub CommandButton1_Click()
Dim h1 As Worksheet, f As Long
If TextBox1 = "" Then Exit Sub
Set buscar = Sheets("Datos").Range("A:A").Find(TextBox1, LookIn:=xlValues, lookat:=xlWhole)
  If Not buscar Is Nothing Then
     MsgBox "Este código ya existe."
     TextBox1 = "": TextBox1.SetFocus
     Cancel = True
     Else
Set h1 = Sheets("Datos")
    f = h1.Range("A" & Rows.Count).End(3).Row + 1
    h1.Cells(f, "A").Value = TextBox1.Value
    h1.Cells(f, "B").Value = TextBox2.Value
     MsgBox "Registro realizado con éxito"
End If
End Sub

1 Respuesta

Respuesta
1

¿Cuál es tu idea, registrar en una, después registrar en la segunda hoja, después registrar en la tercera hoja?

¿O quieres pasar como parámetro el nombre de la hoja y que haga el registro en la hoja que pasaste como parámetro?

Hola Dante Amor. Gracias por responder.

Mi idea es como te muestro a continuación con imágenes. Lo he intentado como ves en el código, pero no sé si está bien planteado. 

Private Sub CommandButton1_Click()
If OptionButton1 = False Or OptionButton2 = False Then
   MsgBox "Seleccione la hoja de registro", vbInformation
   Exit Sub
End If
If OptionButton1.Value = True Then
Call RegistroExcel
End If
End Sub
Sub RegistroExcel()
If TextBox1 = "" Then Exit Sub
Set buscar = Sheets("Excel").Range("A:A").Find(TextBox1, LookIn:=xlValues, lookat:=xlWhole)
  If Not buscar Is Nothing Then
     MsgBox "Este código ya existe."
     TextBox1 = "": TextBox1.SetFocus
     Cancel = True
     Else
Set h1 = Sheets("Excel")
    f = h1.Range("A" & Rows.Count).End(3).Row + 1
    h1.Cells(f, "A").Value = TextBox1.Value
    h1.Cells(f, "B").Value = TextBox2.Value
    MsgBox "Registro realizado con éxito"
End If
End Sub

Puedo hacer cambios en el form. si me recomiendas otra cosa.

Prueba lo siguiente:

Private Sub CommandButton1_Click()
  Dim h1 As Worksheet
  Dim buscar As Range
  Dim f As Long
  '
  If OptionButton1 = False And OptionButton2 = False Then
    MsgBox "Seleccione la hoja de registro", vbInformation
    Exit Sub
  End If
  If TextBox1 = "" Then
    MsgBox "Captura el dato en textbox1"
    TextBox1.SetFocus
    Exit Sub
  End If
  '
  If OptionButton1.Value = True Then
    Set h1 = Sheets("Lista")
  Else
    Set h1 = Sheets("Datos")
  End If
  '
  Set buscar = h1.Range("A:A").Find(TextBox1, , xlValues, xlWhole, , , False)
  If Not buscar Is Nothing Then
    MsgBox "Este código ya existe."
    TextBox1 = ""
    TextBox1.SetFocus
  Else
    f = h1.Range("A" & Rows.Count).End(3).Row + 1
    h1.Cells(f, "A").Value = TextBox1.Value
    h1.Cells(f, "B").Value = TextBox2.Value
    MsgBox "Registro realizado con éxito"
    TextBox1.Value = ""
    TextBox2.Value = ""
    OptionButton1.Value = False
    OptionButton2.Value = False
  End If
End Sub

¡Gracias! Dante Amor. No la he probado, pero segura que va a funcionar.

Ahh. Me olvidé comentar que lo más probable es que añada más hojas y más optionbatton, en ese caso que parte del código podría modificar?

En ese caso, cambia estas líneas:

  If OptionButton1.Value = True Then
    Set h1 = Sheets("Lista")
  Else
    Set h1 = Sheets("Datos")
  End If

Por estas:

  If OptionButton1.Value = True Then Set h1 = Sheets("Lista")
  If OptionButton2.Value = True Then Set h1 = Sheets("Datos")
  If OptionButton3.Value = True Then Set h1 = Sheets("Otra hoja")

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas