Modificación de macro que copia y pega datos en otra hoja (dan)

Tengo este código realizado por el experto dan... El cual funciona ala perfección..

Pero según mis necesidades salio un detalle...

El código al poner una cantidad en el rango este se ejecuta copiando el dato y pegándolo en otra hoja (excelente)

Ahora al borrar el dato que previamente introduje este me sale un userfom el cual me permite dice que seleccione el producto a eliminar (excelente)

Ahora bien el userform tiene 2 botones

1 de eliminar y 2 de cancelar, recalco el userform se activa siempre y cuando yo elimine el dato que yo introduje,

El punto es que si el dato es 5 y lo elimino (aparece el userform) y en el userform doy clic al botón 2 cancelar, este no me borra ningún dato de la hoja en donde se pega, pero si me borra el dato 5 que anteriormente puse... Entonces quisiera que si yo borro el dato este me pregunte primero si deseo eliminar el dato en caso de decir que si entonces me salga mi userform, en caso de decir que NO entonces que me mantenga el dato que tenga...

O bien que al abrir mi userform y le de clic al botón cancelar, este me regrese el dato que en la celda con dato tenia,,,

En este caso si puse 5 y lo borre (me sale userform) le doy clic al botón cancelar entonces me regrese el num 5 en la celda ya que le di cancelar

Anexo el código que tengo en el evento hoja para ser modificado

Private Sub Worksheet_Change(ByVal Target As Range)
'Act.Por.Dante Amor
'ActiveWindow.DisplayVerticalScrollBar = False
'ActiveWindow.DisplayHorizontalScrollBar = False
'ExecuteExcel4Macro ("show.toolbar(""ribbon"",1)")
Application.ScreenUpdating = False
If Not Intersect(Target, Range("E7:HC934")) Is Nothing Then
For Each D In Target
If D.Value <> "" Then
If Not IsNumeric(D.Value) Then
Application.EnableEvents = False
D.Value = ""
D.Select
celda = celda & D.Address & " "
datoerrD = True
Application.EnableEvents = True
End If
End If
Next
If datoerrD Then
MsgBox "Intentaron poner letras en las celdas " & celda, vbexclamantion, "INVENTARIOS EN PIEZAS"
End If
End If

If Intersect(Target, Range("j7:j934,v7:v934,ah7:ah934,at7:at934,bf7:bf934,br7:br934,cd7:cd934,CP7:CP934,DB7:DB934,DN7:DN934,DZ7:DZ934,EL7:EL934,EX7:EX934,FJ7:FJ934")) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
If Target.Value = "" Then
ELIMINACIONES2.Show
Exit Sub
End If
If Not IsNumeric(Target) Then Exit Sub
'
Target.Select
Set h1 = Sheets("REP X TURNO")
existe = False
For i = 10 To 23
If h1.Cells(i, "U") = "" Then
existe = True
Exit For
End If
Next
If existe = False Then
MsgBox "Limite de Degustaciones Alcanzado", vbCritical, "ERROR"
Exit Sub
End If
CANTIDAD = Application.InputBox("Cantidad a Degustar: ", "Ingresar", Target)
If CANTIDAD = 0 Or CANTIDAD = "" Then
Application.EnableEvents = False
Target = ""
Application.EnableEvents = True
Exit Sub
End If
'
fecha = Application.InputBox("fecha de Degustación: ", "INGRESAR", Format(Cells(965, Target.Column), "MM/DD/yyyy"))
If fecha = 0 Or fecha = "" Then
Application.EnableEvents = False
Target = ""
Application.EnableEvents = True
Exit Sub
End If
'
h1.Unprotect
h1.Cells(i, "U") = CANTIDAD & " " & Cells(Target.Row, "B") 'Cantidad y producto
h1.Cells(i, "V") = fecha 'Fecha
h1.Cells(i, "W") = Cells(Target.Row, "C") * CANTIDAD 'Cantidad * precio
h1.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True

End Sub

anexo el codigo de mi userform que elimina datos

'Eliminar el registro
Private Sub CommandButton4_Click()

Pregunta = MsgBox("Está seguro de eliminar el registro?", vbYesNo + vbQuestion, "INFO")
If Pregunta <> vbNo Then

Fila = Me.ListBox1.ListIndex + 10 'UNO ANTES DEL ENCABEZADO PARA QUE AGARRE LOS VALORES
Sheets("REP X TURNO"). Unprotect
Sheets("REP X TURNO"). Cells(Fila, 9). Clearcontents 'columna y celda apartir de la posicion individual para borrar (1) es desde donde estas
Sheets("REP X TURNO"). Cells(Fila, 10). ClearContents
Sheets("REP X TURNO"). Cells(Fila, 11). ClearContents
'Cells(Fila, 24). ClearContents
Sheets("REP X TURNO"). Protect
Application.ScreenUpdating = True
Unload Me
End If
End Sub

anexo codigo del boton cancelar del mismo userform

'Cerrar formulario
Private Sub CommandButton2_Click()
Unload Me
MsgBox "No eliminaste Ningun Producto", vbExclamation, "AVISO"
End Sub

y por ultimo anexo mis agradecimientos:

:D.

1 respuesta

Respuesta
1

H o l a:

Cambia esto:

If Intersect(Target, Range("j7:j934,v7:v934,ah7:ah934,at7:at934,bf7:bf934,br7:br934,cd7:cd934,CP7:CP934,DB7:DB934,DN7:DN934,DZ7:DZ934,EL7:EL934,EX7:EX934,FJ7:FJ934")) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
If Target.Value = "" Then
ELIMINACIONES2.Show
Exit Sub
End If
If Not IsNumeric(Target) Then Exit Sub

Por esto:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("j7:j934,v7:v934,ah7:ah934,at7:at934,bf7:bf934,br7:br934,cd7:cd934,CP7:CP934,DB7:DB934,DN7:DN934,DZ7:DZ934,EL7:EL934,EX7:EX934,FJ7:FJ934")) Is Nothing Then Exit Sub
    If Target.Count > 1 Then Exit Sub
    If Target.Value = "" Then
        With ELIMINACIONES2
            .celda = Target.Address
            .Show
        End With
        Exit Sub
    Else
        Application.EnableEvents = False
        Sheets("respaldo").Range(Target.Address) = Target.Value
        Application.EnableEvents = True
    End If
    If Not IsNumeric(Target) Then Exit Sub
End Sub

En el formulario, cambia esto:

Private Sub CommandButton4_Click()
Pregunta = MsgBox("Está seguro de eliminar el registro?", vbYesNo + vbQuestion, "INFO")
If Pregunta <> vbNo Then
Fila = Me.ListBox1.ListIndex + 10 'UNO ANTES DEL ENCABEZADO PARA QUE AGARRE LOS VALORES
Sheets("REP X TURNO"). Unprotect
Sheets("REP X TURNO"). Cells(Fila, 9). Clearcontents 'columna y celda apartir de la posicion individual para borrar (1) es desde donde estas
Sheets("REP X TURNO"). Cells(Fila, 10). ClearContents
Sheets("REP X TURNO"). Cells(Fila, 11). ClearContents
'Cells(Fila, 24). ClearContents
Sheets("REP X TURNO"). Protect
Application.ScreenUpdating = True
Unload Me
End If
End Sub

Por esto, observa que al principio del formulario estoy declarando la variable celda como pública.

Public celda
Private Sub CommandButton4_Click()
    '
    Pregunta = MsgBox("Está seguro de eliminar el registro?", vbYesNo + vbQuestion, "INFO")
    If Pregunta = vbYes Then
        '
        Sheets("respaldo").Range(celda) = ""
        fila = Me.ListBox1.ListIndex + 10 'UNO ANTES DEL ENCABEZADO PARA QUE AGARRE LOS VALORES
        Sheets("REP X TURNO"). Unprotect
        Sheets("REP X TURNO"). Cells(fila, 9). ClearContents 'columna y celda apartir de la posicion individual para borrar (1) es desde donde estas
        Sheets("REP X TURNO"). Cells(fila, 10). ClearContents
        Sheets("REP X TURNO"). Cells(fila, 11). ClearContents
            'Cells(Fila, 24). ClearContents
        Sheets("REP X TURNO"). Protect
        Application.ScreenUpdating = True
        Unload Me
    Else
        Range(celda) = Sheets("respaldo").Range(celda)
    End If
End Sub

Tienes que crear una hoja que se llame "respaldo". En esa hoja copia los valores que tienes actualmente en tu hoja. Entonces cuando borres un valor y canceles el proceso, la macro va a ir a la hoja respaldo y tomará el dato almacenado.

Cada que modifiques tu hoja, la macro actualizará la hoja "respaldo".


S a l u d o s

Tienes respuestas sin valorar. Recuerda valorar todas. G r a c i a s

no me funciono.

cuando elimino el dato que tengo este me muestra el formulario y cuando cierro el formulario para no eliminar nada (osea me arrepiento de borrar) este si lo borra

Primero tienes que copiar tus datos en la hoja respaldo como valores.

Después puedes empezar a borrar.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas