T
The Cube
Hi all
Is there a VBA instruction to reverse the effect of Application.Undo?
I wish to achieve the following effect:
If I change any values in Worksheet("Codes").Range("Short_name_range")
then those changes must be reflected by identical changes in
Worksheet("Analysis").Range("Account_1_Range") and in
Worksheet("Analysis").Range("Account_2_Range") wherever there is a match
between the original values. For various reasons I cannot link these cells
by formulae and I therefore have to make the changes by an event handler.
Code follows, followed by the observed problem when this triggers in
practice:
Private Sub Worksheet_Change(ByVal Target As Range) 'Worksheet("Codes")
Dim lRowCount As Long
Dim lRow As Long
Dim lTestRow As Long
Dim rUndoValues As Range
Dim rShortNameRange As Range
Dim rUndoSelection As Range
Set prSelection = Intersect(Target, Range("short_name_range")) 'Public
If prSelection Is Nothing Then Exit Sub
pbMeWriting = True 'Public
lRowCount = prSelection.Rows.Count
With Application
.EnableEvents = False
.Undo
With .ThisWorkbook.Worksheets("Codes")
Set rShortNameRange = .Range("short_Name_Range")
Set rUndoValues = .Range("UndoValues_Range")
Set rUndoSelection = prSelection.Offset(.Range("UndoRow"), _
.Range("UndoColumn"))
rShortNameRange.Copy
rUndoValues.PasteSpecial Paste:=xlPasteValues
End With '.ThisWorkbook.Worksheets("Codes")
.CutCopyMode = False
.EnableEvents = True
With .ThisWorkbook.Worksheets("Analysis")
For lRow = 1 To lRowCount
For lTestRow = 1 To .Range("Depth_Range").Value
If rUndoSelection.Rows(lRow).Value <> _
.Range("Account_1_Range").Rows(lTestRow).Value Then GoTo Test2
.Range("Account_1_Range").Rows(lTestRow).Value = _
prSelection.Rows(lRow).Value
Test2:
If rUndoSelection.Rows(lRow).Value <>
.Range("Account_2_Range").Rows(lTestRow).Value Then GoTo NextTestRow
.Range("Account_2_Range").Rows(lTestRow).Value = _
prSelection.Rows(lRow).Value
NextTestRow:
Next lTestRow
Next lRow
End With '.ThisWorkbook.Worksheets("Analysis")
End With 'Application
End Sub 'Worksheet_Change(ByVal Target As Range)
PROBLEM:
The effect of Application.Undo appears to be permanent. Somehow I need to
store the changes before they are undone, so that the changes made to the
worksheet "Codes" ultimately take effect. But any attempt to store these
values by copy/paste before Application.Undo causes Undo to crash with error
1004 and I then have to "Clean" the project before the event handler will
kick in at all.
Thanks
-Cube
Is there a VBA instruction to reverse the effect of Application.Undo?
I wish to achieve the following effect:
If I change any values in Worksheet("Codes").Range("Short_name_range")
then those changes must be reflected by identical changes in
Worksheet("Analysis").Range("Account_1_Range") and in
Worksheet("Analysis").Range("Account_2_Range") wherever there is a match
between the original values. For various reasons I cannot link these cells
by formulae and I therefore have to make the changes by an event handler.
Code follows, followed by the observed problem when this triggers in
practice:
Private Sub Worksheet_Change(ByVal Target As Range) 'Worksheet("Codes")
Dim lRowCount As Long
Dim lRow As Long
Dim lTestRow As Long
Dim rUndoValues As Range
Dim rShortNameRange As Range
Dim rUndoSelection As Range
Set prSelection = Intersect(Target, Range("short_name_range")) 'Public
If prSelection Is Nothing Then Exit Sub
pbMeWriting = True 'Public
lRowCount = prSelection.Rows.Count
With Application
.EnableEvents = False
.Undo
With .ThisWorkbook.Worksheets("Codes")
Set rShortNameRange = .Range("short_Name_Range")
Set rUndoValues = .Range("UndoValues_Range")
Set rUndoSelection = prSelection.Offset(.Range("UndoRow"), _
.Range("UndoColumn"))
rShortNameRange.Copy
rUndoValues.PasteSpecial Paste:=xlPasteValues
End With '.ThisWorkbook.Worksheets("Codes")
.CutCopyMode = False
.EnableEvents = True
With .ThisWorkbook.Worksheets("Analysis")
For lRow = 1 To lRowCount
For lTestRow = 1 To .Range("Depth_Range").Value
If rUndoSelection.Rows(lRow).Value <> _
.Range("Account_1_Range").Rows(lTestRow).Value Then GoTo Test2
.Range("Account_1_Range").Rows(lTestRow).Value = _
prSelection.Rows(lRow).Value
Test2:
If rUndoSelection.Rows(lRow).Value <>
.Range("Account_2_Range").Rows(lTestRow).Value Then GoTo NextTestRow
.Range("Account_2_Range").Rows(lTestRow).Value = _
prSelection.Rows(lRow).Value
NextTestRow:
Next lTestRow
Next lRow
End With '.ThisWorkbook.Worksheets("Analysis")
End With 'Application
End Sub 'Worksheet_Change(ByVal Target As Range)
PROBLEM:
The effect of Application.Undo appears to be permanent. Somehow I need to
store the changes before they are undone, so that the changes made to the
worksheet "Codes" ultimately take effect. But any attempt to store these
values by copy/paste before Application.Undo causes Undo to crash with error
1004 and I then have to "Clean" the project before the event handler will
kick in at all.
Thanks
-Cube