Resolver codigo

BUENAS TARDES TEGO UN FORMULARIO EN UNA MACRO CON UN BOTON PARA MODIFICAR, EL BOTON SI ME TRAE EL DATO A MODIFICAR PRO RL PROBLEMA ES QUE ME LO GUARDA EN LA  CELDA SIGUIENTE Y NO DONDE DEVERIA QUE SERIA EN LA MISMA FILA EN LA QUE ESTOY MODIFICANDO
CODIGO COMPLETO
Dim valor1 As String
Dim VALOR2 As String
Dim SW As Boolean
Dim I As Integer
Private Sub bntEliminar_Click()
valor1 = InputBox("que registro desea eliminar")
eliminaRegistro
End Sub
Private Sub btnModificar_Click()
'
VALOR2 = InputBox("que registro desea modificar")
Sheets("Hoja1").Select
'
Dim J As Integer
'
'
 J = 20
For I = 1 To J
    If Range("A" + Trim(Str(I))).Value = VALOR2 Then
txtcedula.Text = Range("A" + Trim(Str(I))).Value
txtNombre.Text = Range("B" + Trim(Str(I))).Value
txtTelefono.Text = Range("C" + Trim(Str(I))).Value
txtDireccion.Text = Range("D" + Trim(Str(I))).Value
    J = 21
'
   End If
'
'
'
'
Next I
'
'
End Sub
Private Sub CommandButton1_Click()
    guardar
End Sub
Private Sub guardar()
'If SW = True Then
    If txtcedula.Text = "" Then
        MsgBox ("Por favor ingrese Cédula")
        txtcedula.SetFocus
    Else
            If txtNombre.Text = "" Then
                MsgBox ("Por favor ingrese Nombre")
                txtNombre.SetFocus
            Else
                If txtTelefono.Text = "" Then
                    MsgBox ("Por favor ingrese Telefono")
                    txtTelefono.SetFocus
                Else
                    If txtDireccion.Text = "" Then
                        MsgBox ("Por favor ingrese Dirección")
                        txtDireccion.SetFocus
                    Else
                        Sheets("HOJA1").Select
                        'seleccionamos la ceda donde se debe alojar el dato
                        Range("A1").Select
                        'guardamos el dato en la celda
                        Set ws = Worksheets("HOJA1")
                        iFila = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                        ws.Cells(iFila, 1).Value = txtcedula.Value
                        txtcedula.Text = ""
                        Set ws = Worksheets("HOJA1")
                        iFila = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                        ws.Cells(iFila - 1, 2).Value = txtNombre.Value
                        txtNombre.Text = ""
                        Set ws = Worksheets("HOJA1")
                        iFila = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                        ws.Cells(iFila - 1, 3).Value = txtTelefono.Value
                        txtTelefono.Text = ""
                        Set ws = Worksheets("HOJA1")
                        iFila = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                        ws.Cells(iFila - 1, 4).Value = txtDireccion.Value
                        txtDireccion.Text = ""
                        MsgBox ("GUARDADDO")
                    End If
                End If
            End If
    End If
'    Else
End Sub
Private Sub CommandButton2_Click()
Dim valaux As Integer
    If Not txtcedula.Text = "" Then
        valaux = MsgBox("Existen datos sin guardar, desea salir?...

2 respuestas

Respuesta
1
Creo que la dificultad se presenta en la rutina donde guardas la informacion , ya que utilizas la misma forma para guardar un registro nuevo que uno modificado, supongo que tu clave primaria es la cedula, entonces en la instrucucion donde validas  If txtcedula.Text = "" Then, en el "else" llamaria una subrutina que le de vaor a iFila a travez de un bucle algo asi:
dim iFila as long
sub Searchpos()
dim flag as boolean
flag=false
Set ws = Worksheets("HOJA1")
F= 2' variable provisional
do while ws.cells( f,1)<>""
if ws.cells( f,1).value= txtcedula.Text then
iFila=f
flag=true
end if
f=f+1
loop
if flag=false then  ifila=f
end sub
debes suprimir la instruccion de busqueda de posicion que tienes actualmente
espero te sirva
en que parte remplazaria el codigo exactamente????
Creo que debe suprimir la instruccion
iFila = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
de esta rutina
 Else
                        Sheets("HOJA1").Select
                        'seleccionamos la ceda donde se debe alojar el dato
                        Range("A1").Select
                        'guardamos el dato en la celda
                        Set ws = Worksheets("HOJA1")
                       ' iFila = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                        ws.Cells(iFila, 1).Value = txtcedula.Value
                        txtcedula.Text = ""
                        Set ws = Worksheets("HOJA1")
                        'iFila = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                        ws.Cells(iFila - 1, 2).Value = txtNombre.Value
                        txtNombre.Text = ""
                        Set ws = Worksheets("HOJA1")
                       ' iFila = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                        ws.Cells(iFila - 1, 3).Value = txtTelefono.Value
                        txtTelefono.Text = ""
                        Set ws = Worksheets("HOJA1")
                        'iFila = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                        ws.Cells(iFila - 1, 4).Value = txtDireccion.Value
                        txtDireccion.Text = ""
                        MsgBox ("GUARDADDO")
                    End If
no me funciona te puedo mandar opor correo la macro para que la revises y finalizamos la pregunta gracias
Respuesta
1
Antes de entrar a verificar el código ya probaste los siguiente...
...
Sub VerFormulario()
    Sheets("Hoja1").Select
    Range("A1").Select
    ActiveSheet.ShowDataForm
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas