I've got an Excel file, where I have to enter names in one sheet, and then the other sheets are adding or deleting rows.
For example I have the excel sheets User, NumberSheet and GradeSheet.
I am entering "John Doe", "Jane Doe" and "Phil Doe" into the User-Sheet, and in NumberSheet and GradeSheet, 3 Rows are added.
I have this code for now:
Private Sub Worksheet_Change(ByVal Target As range)
Dim KeyCells As range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
' Start Cell is A2 because Names start there
Set KeyCells = range("A2:A100")
If Not Application.Intersect(KeyCells, range(Target.Address)) _
Is Nothing Then
' Display a message when one of the designated cells has been
' changed.
' Place your code here.
'The row in the user-Sheet, where I added or removed a user
targetRow = Target.row - 1
'In the Number Sheet, the row from where the names should get listed
targetRowInNumberSheet = targetRow
'The value from the cell where I entered the name
vlue = Target.Value
If IsEmpty(vlue) Then
'Delete row in Worksheets
Worksheets("NumberSheet").Rows(targetRowInNumberSheet).Delete
Worksheets("GradeSheet").Rows(targetRowInNumberSheet ).Delete
Else
'Add row in Worksheets
Worksheets("NumberSheet").Rows(targetRowInNumberSheet).Insert
Worksheets("GradeSheet").Rows(targetRowInNumberSheet ).Insert
End If
End If
End Sub
Now this code works if I add a name, delete one or insert a row somewhere in between.
But I can't delete multiple names, or delete a row. In both cases IsEmpty(vlue) will be false, and I don't know why
Targetrange contain more cells?