Hello,
I have a seemingly strange problem. It seems like Application.Undo doesn't have any impact when I run my code in Excel 2003 (while in Excel 2007, it runs fine).
What I want to do: I want to avoid the user of my worksheet to shift the header row from its row 1 position. So, the user should not be able to insert any rows above the header. If he/she does, I want to undo that operation. For this, I am using a named cell in the header row and checking if it has got shifted after any change in the worksheet. My code is on these lines -
While this works peacefully in Excel 2007, in Excel 2003, the undo has no impact! Though the code gets parsed when I insert a row above the header row, there is no undo action seen. In fact, after this runs, when the cursor is back to Excel workbook, when I do undo manually, the row insert gets undone!
Note: I did see that some posts said that the Undo should be the first statement in the macro. My understanding is that it is true if we are using Application.OnUndo to tag the macro to the undo operation done from the UI.
Any inputs towards solving this would be highly appreciated.
-sat
I have a seemingly strange problem. It seems like Application.Undo doesn't have any impact when I run my code in Excel 2003 (while in Excel 2007, it runs fine).
What I want to do: I want to avoid the user of my worksheet to shift the header row from its row 1 position. So, the user should not be able to insert any rows above the header. If he/she does, I want to undo that operation. For this, I am using a named cell in the header row and checking if it has got shifted after any change in the worksheet. My code is on these lines -
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target Is Nothing Then
Exit Sub
End If
If Target.Cells.Count > 1 Then
AvoidHeaderShifting Target
End If
End Sub
Private Sub AvoidHeaderShifting (ByVal Target As Range)
Dim iRow As Long
HEADER_ROW_POSITION = 1
iRow = Sheets("Wk1").Range("my_header_name").row
If iRow > HEADER_ROW_POSITION Then 'if iRow is > 1, it means that the header row has got shifted
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
MsgBox "Header cannot be moved please..."
End If
End Sub
While this works peacefully in Excel 2007, in Excel 2003, the undo has no impact! Though the code gets parsed when I insert a row above the header row, there is no undo action seen. In fact, after this runs, when the cursor is back to Excel workbook, when I do undo manually, the row insert gets undone!
Note: I did see that some posts said that the Undo should be the first statement in the macro. My understanding is that it is true if we are using Application.OnUndo to tag the macro to the undo operation done from the UI.
Any inputs towards solving this would be highly appreciated.
-sat