Excel 2007 Vba-code voor
Voorbeeld
In rij 10 staat tekst vermeld, deze moet in rij 10 blijven staan ook als er rijen worden toegevoegd of rijen worden verwijderd.
Dank bij voorbaat
Excel 2007 Vba-code voor
Voorbeeld
In rij 10 staat tekst vermeld, deze moet in rij 10 blijven staan ook als er rijen worden toegevoegd of rijen worden verwijderd.
Dank bij voorbaat
Seppe.
Your Question is in Dutch but (with the help of Google Translate) I guess you want VBA code so that set text in row 10 stays in row 10 if entire rows are deleted. I'm not sure what you want to do when rows are added or single cells are added/deleted (see Discussion under the Question above).
I'm not aware of a direct way to do that but in the attached demo file, there is sample text in rows 1 to 9 and "Protected Text" in row 10. In hidden column O (15), numbers 1 to 10 are in sequence in rows 1 to 10- these are used to detect which rows are deleted (and could be used for those added).
The Worksheet_Change event code below acts when entire rows are deleted (and gives a message if some are added). The comments tell you what happens...
Private Sub Worksheet_Change(ByVal Target As Range)
' check last used row in hidden column 15 (=O)
LstRw = Cells(Rows.Count, 15).End(xlUp).Row
' do nothing if rows above 10 weren't changed
If LstRw = 10 Then Exit Sub
' prevent retriggering while this code works
Application.EnableEvents = False
' if row 10 was deleted, undo the change
If Cells(LstRw, 15).Value <> 10 Then
Application.Undo
MsgBox "Deletion blocked! You must not delete row 10"
Application.EnableEvents = True
Exit Sub
End If
' if rows were deleted...
If LstRw < 10 Then
' ... jump to procedure for deleted rows
Call RowsDeleted
Else
'... otherwise jump to procedure for added rows
Call RowsAdded
End If
Application.EnableEvents = True
End Sub
The bold lines are because the declaration Dim LstRw As Long is at the top so that variable is "known" to the other procedures called by the other bold lines.
Correction #1: 29 June 2024
The above code (and revised file) has an extra line (in bold below) so that events are enabled if a deletion is blocked:
' if row 10 was deleted, undo the change
If Cells(LstRw, 15).Value <> 10 Then
Application.Undo
MsgBox "Deletion blocked! You must not delete row 10"
Application.EnableEvents = True
Exit Sub
End If
The RowsDeleted procedure acts when rows (up to and including row 9) are deleted:
Private Sub RowsDeleted()
' loop down rows
For n = 1 To LstRw - 1
With Cells(n, 15)
' see which rows were deleted...
If .Value <> n Then
'... and restore row / index...
.EntireRow.Insert
.Offset(-1, 0).Value = n
' ... and reduce row count
n = n - 1
End If
End With
Next n
End Sub
If a row is deleted, it just becomes blank (apart from in hidden column O) and row 10 stays where it was.
The code for insertions just gives a message for now:
Private Sub RowsAdded()
'to be defined
MsgBox "Rows added- what to do now?"
End Sub
Hope this makes sense and helps- if so, please remember to mark this Answer as Selected.