If... Then Loop problems in Worksheet Event

T

TB

I can't seem to get the following code to work properly:

Option Explicit
Private Sub Worksheet_Change(ByVal target as Excel.Range))
Dim strDay as String
On Error Resume Next
Application.EnableEvents = False
If Range("E10").Cells.Value + Range("B10").Cells.Value = 16 Then
Range("H10").Cells.Value = Range("H10").Cells.Value + 4
Range("H17").Cells.Value = Range("H17").Cells.Value + 4
strDay = " (" & ActiveCell.Offset(-1,-4).Value & ") "
Sheets(1).Range("B26").Value = Sheets(1).Range("B26").Value_
& strDay & " Working floating holiday @ time and a half."
End If
If Range("E11").Cells.Value + Range("B11").Cells.Value = 16 Then
Range("H10").Cells.Value = Range("H10").Cells.Value + 4
Range("H17").Cells.Value = Range("H17").Cells.Value + 4
strDay = " (" & ActiveCell.Offset(-1,-4).Value & ") "
Sheets(1).Range("B26").Value = Sheets(1).Range("B26").Value_
& strDay & " Working floating holiday @ time and a half."
End If
Application.EnableEvents = True
End Sub

There are a total of seven If... Then statements for E10 through E16. If the
conditions in any of these cells are met, the following line of text is
inserted into cell B26 (Example for E10):

(Sunday) Working floating holiday @ time and a half.

If data is entered into a second cell, (i.e. cell E11), the following
double entry is generated:

(Sunday) Working floating holiday @ time and a half.
(Monday) Working floating holiday @ time and a half.
(Monday) Working floating holiday @ time and a half. (extra line generated)

A third entry generates a triple entry, etc. Can anyone help me with this?

I'd also appreciate a routine for deleting the appropriate line of text if
the user goes back and deletes the data in one of the cells.
 
T

TB

Bob, I put an example of the output after the code. It outputs the text
correctly if only one cell in column E has data in it (data is either "8" or
and empty cell). Entering a second "8" causes the routine to output two
lines of text, a third causes three lines, etc. For example, assuming that
columns B10 through B14 all have "8" entered in them, and I insert an "8" in
E10, E11, and E12, I will get the following output:

(Sunday) Working floating holiday @ time and a half.
(Monday) Working floating holiday @ time and a half.
(Monday) Working floating holiday @ time and a half. (extra unwanted line)
(Tuesday) Working floating holiday @ time and a half
(Tuesday) Working floating holiday @ time and a half (extra unwanted line)
(Tuesday) Working floating holiday @ time and a half (extra unwanted line)

Looking at the code, I know why it's doing it. I just don't know of a way to
stop it from reading the previous entries in that column more than once. Any
help is welcome.

Regards
Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top