¿Cómo mostrar datos de una celda en TextBox mediante los botones:primero,anterior,siguiente,ultimo?

Disculpen la molestia pero necesito su ayuda... Mi problema es el siguiente: tengo una hoja llamada Clientes y tengo una tabla cuyo rango de celdas están con los siguientes datos:

Ojo: los datos están a partir de la celda a3 ya q en a2 y a1 están los títulos.

_________________________________________

_|_ A _|___B___|_______C _________|_____D____|

|3| 001 | ANDRE | AV. ARENALES #984| 056-236589 |

|4| 002 | JOSE | AV. ARENALES #985| 056-235489 |

|5| 003 |CARLOS| AV. ARENALES #986| 056-285589 |

|6| 004 | JUAN | AV. ARENALES #987| 056-223589 |.......

... ASI HASTA LA FILA 15

El trabajo que me han dejado consiste en crear un formulario con 4 texbox y cuatro botones de comando. Lo q debo hacer es mediante cada botón me muestre los valores de las celdas en los textox, esto es lo q tienen q hacer los botones.

Por ejemplo:

- El botón primero al darle click deberá de seleccionar la fila A3 (tmb podría seleccionarse desde A3 hasta D3 no hay problema) y mostrar los datos de A3 en el textbox1, B3 en el textbox2, C3 en el textbox3 y D3 en el textbox4.

- El botón siguiente al darle click deberá de avanzar de la celda A3 hasta hasta la A15 (o del ranfo A3:D3 hasta A15:D15) q es donde terminan los datos q tengo y mientras va avanzando ira mostrando los datos de la celda seleccionada en los textbox1, 2,3 y 4. (ojo: si llegua a la ultima celda con datos osea A15 debera detenerse y ya no seguir avanzando)

- El boton anterior lo q hara sera lo mismo q el boton siguiente solo q en vez de avanzar tendra q retroceder e ir mostrando los datos en los texbox y si llegua hasta la celda A3 se debera detener.

- El boton ultimo hara lo mismo q el boton primero solo q mostrara ya no el primer registro sino el ultimo dle A15, B15, C15, D15 en los diferentes texbox.

2 respuestas

Respuesta
1

Estas son las 4 macros que necesitas para los 4 botones:

Atención la primera fila también tienes que copiarla, en ella declaramos pública la variable fila.

Public fila
Private Sub CommandButton1_Click() 'boton primera fila
TextBox1.Value = Range("a3").Value
TextBox2.Value = Range("b3").Value
TextBox3.Value = Range("c3").Value
TextBox4.Value = Range("d3").Value
fila = 3
End Sub
Private Sub CommandButton2_Click() 'boton siguiente
Cells(fila + 1, 1).Select
TextBox1.Value = ActiveCell
TextBox2.Value = ActiveCell.Offset(0, 1)
TextBox3.Value = ActiveCell.Offset(0, 2)
TextBox4.Value = ActiveCell.Offset(0, 3)
fila = ActiveCell.Row
End Sub
Private Sub CommandButton3_Click() 'boton anterior
Cells(fila - 1, 1).Select
TextBox1.Value = ActiveCell
TextBox2.Value = ActiveCell.Offset(0, 1)
TextBox3.Value = ActiveCell.Offset(0, 2)
TextBox4.Value = ActiveCell.Offset(0, 3)
fila = ActiveCell.Row
End Sub
Private Sub CommandButton4_Click() 'boton ultima
ultima = Range("a65000").End(xlUp).Row
Cells(ultima, 1).Select
TextBox1.Value = ActiveCell
TextBox2.Value = ActiveCell.Offset(0, 1)
TextBox3.Value = ActiveCell.Offset(0, 2)
TextBox4.Value = ActiveCell.Offset(0, 3)
fila = ActiveCell.Row
End Sub

recuerda finalizar y puntuar

Gracias luismondelo por la ayuda pero si no fuera mucha molestia quisiera q me ayudaras en algo q no esta funcionando bien, mira los botones primero y ultimo funcionan muy bien, pero los botones anterior siguiente funcionan tmb bien avanzan normalmente y muestran los datos solo que lo que me gustaría que hagan tmb es q el botón anterior retroceda mostrando los datos en los textbox pero al lleguar al primer registro q esta en A3 se detenga o muestra un mensaje(ud se encuentra en el primer registro) cosa q no hace y q el ultimo avanze tmb pero q se detenga en la ultima celda con datos o q muestre un mensaje tmb (ud se encuentra en el ultimo registro).

Entones las macros de ambos botones son estas:

Private Sub CommandButton2_Click() 'boton siguiente
ultima = range("a65000").end(xlup).row
Cells(fila + 1, 1).Select
if activecell.row = ultima then
msgbox "está en la última"
exit sub
end if
TextBox1.Value = ActiveCell
TextBox2.Value = ActiveCell.Offset(0, 1)
TextBox3.Value = ActiveCell.Offset(0, 2)
TextBox4.Value = ActiveCell.Offset(0, 3)
fila = ActiveCell.Row
End Sub
Private Sub CommandButton3_Click() 'boton anterior
Cells(fila - 1, 1).Select
if activecell.row =3 then
msgbox "está en la primera"
exit sub
end if
TextBox1.Value = ActiveCell
TextBox2.Value = ActiveCell.Offset(0, 1)
TextBox3.Value = ActiveCell.Offset(0, 2)
TextBox4.Value = ActiveCell.Offset(0, 3)
fila = ActiveCell.Row
End Sub

Una pequeña corrección, estas son las buenas:

Private Sub CommandButton2_Click() 'boton siguiente
ultima = range("a65000").end(xlup).row
Cells(fila + 1, 1).Select
TextBox1.Value = ActiveCell
TextBox2.Value = ActiveCell.Offset(0, 1)
TextBox3.Value = ActiveCell.Offset(0, 2)
TextBox4.Value = ActiveCell.Offset(0, 3)
if activecell.row = ultima then
msgbox "está en la última"
exit sub
end if
fila = ActiveCell.Row
End Sub
Private Sub CommandButton3_Click() 'boton anterior
Cells(fila - 1, 1).Select
TextBox1.Value = ActiveCell
TextBox2.Value = ActiveCell.Offset(0, 1)
TextBox3.Value = ActiveCell.Offset(0, 2)
TextBox4.Value = ActiveCell.Offset(0, 3)
if activecell.row =3 then
msgbox "está en la primera"
exit sub
end if
fila = ActiveCell.Row
End Sub

te mando un saludo.

Respuesta

He adaptado este código a mi proyecto y funciona bien pero no del todo.

Botón primero y ultimo perfecto. Botón anterior y siguiente perfecto pero con un matiz.

Si voy al botón primer registro y después al siguiente hasta el final, cuando le doy a anterior me salta un registro. Lo mismo si doy al anterior registro hasta el primer articulo, cuando le doy a siguiente me salta un articulo

Coloco el código de los 4 botones a ver si me podéis ayudar. GRACIAS:

Public fila
Private Sub cmb_anterior_Click() 'Anterior registro
Cells(fila - 1, 1).Select
cbx_codigo.Value = ActiveCell
txt_nombre.Value = ActiveCell.Offset(0, 1)
cbx_plataforma.Value = ActiveCell.Offset(0, 2)
cbx_consola.Value = ActiveCell.Offset(0, 3)
cbx_categoria.Value = ActiveCell.Offset(0, 4)
cbx_grupo.Value = ActiveCell.Offset(0, 5)
cbx_estado.Value = ActiveCell.Offset(0, 6)
cbx_condicion.Value = ActiveCell.Offset(0, 7)
txt_ubicacion.Value = ActiveCell.Offset(0, 8)
txt_precio.Value = ActiveCell.Offset(0, 9)
txt_cantidad.Value = ActiveCell.Offset(0, 10)
If ActiveCell.Row = 2 Then
MsgBox "Estás en el primer artículo", vbInformation, "MIS JUEGOS"
Exit Sub
End If
fila = ActiveCell.Row
End Sub

Private Sub cmb_primero_Click()
cbx_codigo.Value = Range("A2").Value
txt_nombre.Value = Range("B2").Value
cbx_plataforma.Value = Range("C2").Value
cbx_consola.Value = Range("D2").Value
cbx_categoria.Value = Range("E2").Value
cbx_grupo.Value = Range("F2").Value
cbx_estado.Value = Range("G2").Value
cbx_condicion.Value = Range("H2").Value
txt_ubicacion.Value = Range("I2").Value
txt_precio.Value = Range("J2").Value
txt_cantidad.Value = Range("K2").Value
fila = 2
MsgBox "Estás en el primer artículo", vbInformation, "MIS JUEGOS"
End Sub

Private Sub cmb_siguiente_Click()
ultima = Range("a65000").End(xlUp).Row
Cells(fila + 1, 1).Select
cbx_codigo.Value = ActiveCell
txt_nombre.Value = ActiveCell.Offset(0, 1)
cbx_plataforma.Value = ActiveCell.Offset(0, 2)
cbx_consola.Value = ActiveCell.Offset(0, 3)
cbx_categoria.Value = ActiveCell.Offset(0, 4)
cbx_grupo.Value = ActiveCell.Offset(0, 5)
cbx_estado.Value = ActiveCell.Offset(0, 6)
cbx_condicion.Value = ActiveCell.Offset(0, 7)
txt_ubicacion.Value = ActiveCell.Offset(0, 8)
txt_precio.Value = ActiveCell.Offset(0, 9)
txt_cantidad.Value = ActiveCell.Offset(0, 10)

If ActiveCell.Row = ultima Then
MsgBox "Estás en el último artículo", vbInformation, "MIS JUEGOS"
Exit Sub
End If
fila = ActiveCell.Row
End Sub

Private Sub cmb_ultimo_Click()
ultima = Range("a65000").End(xlUp).Row
Cells(ultima, 1).Select
cbx_codigo.Value = ActiveCell
txt_nombre.Value = ActiveCell.Offset(0, 1)
cbx_plataforma.Value = ActiveCell.Offset(0, 2)
cbx_consola.Value = ActiveCell.Offset(0, 3)
cbx_categoria.Value = ActiveCell.Offset(0, 4)
cbx_grupo.Value = ActiveCell.Offset(0, 5)
cbx_estado.Value = ActiveCell.Offset(0, 6)
cbx_condicion.Value = ActiveCell.Offset(0, 7)
txt_ubicacion.Value = ActiveCell.Offset(0, 8)
txt_precio.Value = ActiveCell.Offset(0, 9)
txt_cantidad.Value = ActiveCell.Offset(0, 10)
fila = ActiveCell.Row
MsgBox "Estás en el último artículo", vbInformation, "MIS JUEGOS"
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas