cell shift

khalil

Board Regular
Joined
Jun 2, 2011
Messages
100
hello all,
i need help:):)

I have this code and it is working great in inserting and shifting cells,

i need another code to go with this code To undo insert. using custom button in the active sheet.
__________________________________________________________

' Synchronous inserting of cell(s) to the activesheet and to AFT
Sub SynchroInsert()
With Selection
Sheets("AFT").Range(.Address).EntireRow.Columns("F").Resize(, .Columns.Count).Insert Shift:=xlToRight
.Insert Shift:=xlToRight
End With
End Sub

___________________________________________________________

:)thanks
Khalil
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi Khalil,
Here is the solution for one-step undo:
Rich (BB code):

' Code of Module1
Dim RngUndo(1 To 2) As Range

Sub SynchroInsert()
  ' Insert with shifting
  With Selection
    Sheets("AFT").Range(.Address).EntireRow.Columns("F").Resize(, .Columns.Count).Insert Shift:=xlToRight
    .Insert Shift:=xlToRight
  End With
  ' Save inserted ranges for undo
  With Selection
    Set RngUndo(1) = .Cells
    Set RngUndo(2) = Sheets("AFT").Range(.Address).EntireRow.Columns("F").Resize(, .Columns.Count)
  End With
  ' Charge undo buffer
  Application.OnUndo "Undo synchro-ins.", "InsertingUndo"
End Sub

' Undo subruotine
Sub InsertingUndo()
  On Error Resume Next
  RngUndo(1).Delete Shift:=xlShiftToLeft
  RngUndo(2).Delete Shift:=xlShiftToLeft
End Sub
Regards,
 
Upvote 0
Re: undo insert

good morning

this code works great in my project with one-step undo insert , but it needs little modification to do several undo inserts ,
................................................

' Code of Module1
Dim RngUndo(1 To 2) As Range

Sub SynchroInsert()
' Insert with shifting
With Selection
Sheets("AFT").Range(.Address).EntireRow.Columns("F").Resize(, .Columns.Count).Insert Shift:=xlToRight
.Insert Shift:=xlToRight
End With
' Save inserted ranges for undo
With Selection
Set RngUndo(1) = .Cells
Set RngUndo(2) = Sheets("AFT").Range(.Address).EntireRow.Columns("F").Resize(, .Columns.Count)
End With
' Charge undo buffer
Application.OnUndo "Undo synchro-ins.", "InsertingUndo"
End Sub

' Undo subruotine
Sub InsertingUndo()
On Error Resume Next
RngUndo(1).Delete Shift:=xlShiftToLeft
RngUndo(2).Delete Shift:=xlShiftToLeft
End Sub

................................................................................................

i hope this time i posted in the right thread, also i changed the title name

thanks allot , khalil :):):)
 
Upvote 0
There is no internal VBA method for more than one step undo.
But for your case below is requested "little modification" for 10 undo steps with two buttons "Synchro inserting" and "Undo of synchro inserting":
Rich (BB code):

' Code of Module1
Const UndoSteps = 10      ' <-- undo buffer max steps for syncro inserted ranges
Dim UndoCol As New Collection

' Code of "Synchro inserting" button
Sub SynchroInsertButton()
  Dim Rng(1 To 2) As Range
  ' Insert with shifting
  With Selection
    Sheets("AFT").Range(.Address).EntireRow.Columns("F").Resize(, .Columns.Count).Insert Shift:=xlToRight
    .Insert Shift:=xlToRight
  End With
  ' Save references of inserted ranges in Rng()
  With Selection
    Set Rng(1) = .Cells
    Set Rng(2) = Sheets("AFT").Range(.Address).EntireRow.Columns("F").Resize(, .Columns.Count)
  End With
  ' Save Rng in collection
  UndoCol.Add Rng
  ' Delete an extra steps of undo buffer
  If UndoCol.Count > UndoSteps Then
    UndoCol.Remove 1
  End If
End Sub

' Code of "Undo of synchro inserting" button
Sub UndoButton()
  Dim a
  If UndoCol.Count = 0 Then
    MsgBox "Undo buffer of synchro inserted cells is empty"
    Exit Sub
  End If
  On Error Resume Next
  a = UndoCol.Item(UndoCol.Count)
  a(1).Parent.Select
  a(1).Select
  a(1).Delete Shift:=xlShiftToLeft
  a(2).Delete Shift:=xlShiftToLeft
  UndoCol.Remove UndoCol.Count
End Sub
 
Upvote 0
I'm glad it helped, KHalil :)
Kind regards,
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top