![]() |
If... Then Loop problems in Worksheet Event
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. |
If... Then Loop problems in Worksheet Event
What exactly is not working, what do you get?
-- HTH Bob Phillips "TB" wrote in message ... 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. |
If... Then Loop problems in Worksheet Event
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 "Bob Phillips" wrote in message ... What exactly is not working, what do you get? -- HTH Bob Phillips "TB" wrote in message ... 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. |
All times are GMT +1. The time now is 06:02 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com