In the VBA code below a newDate is created by adding the value of two cells; newDate =…
- User
In the VBA code below a newDate is created by adding the value of two cells; newDate = cell.Offset(0, 2).Value + cell.Value , and then palcing the newDate into; cell.Offset(0, 1).Value = newDate. This creation of newDate could be in any row in my range. As the code runs, it then finds the next empty row where colum B is empty; newRow = Cells(Rows.count, "B").End(xlUp).Row + 1, and copys newDate to column H of the new row; Range("H" & newRow).Value = newDate. After the last copy; Range("L" & newRow).Value = cell.Offset(0, 5).Value and before the Exit Sub, I want to move the newDate from where it was originaly; cell.Offset(0, 1).Value = newDate to; cell.Offset(0, -3) . I want this ability of remebering the original row, built into my code. Can you please do this for me. The original code is below:
Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range Dim cell As Range Set rng = Intersect(Target, Range("I:I")) If Not rng Is Nothing Then For Each cell In rng If IsDate(cell.Value) Then If cell.Offset(0, -7).Value = "Service" Then MsgBox "Thank you for entering Service Date Completed. Please proceed to enter Service Schedule as number of days." Exit Sub Else If Not doubleClickFlag Then MsgBox "Please change the Issue description ( column B ) as required." End If doubleClickFlag = False Exit Sub End If End If Next cell End If Set rng = Intersect(Target, Range("G:G")) If Not rng Is Nothing Then For Each cell In rng If IsNumeric(cell.Value) Then If IsDate(cell.Offset(0, 2).Value) = False Then MsgBox "Date Service Completed not present" Exit Sub ElseIf cell.Offset(0, -5).Value <> "Service" Then Exit Sub Else Dim newDate As Date newDate = cell.Offset(0, 2).Value + cell.Value cell.Offset(0, 1).Value = newDate cell.Offset(0, -5).Value = "Serviced" MsgBox "A New Service row will now be created" Dim newRow As Long newRow = Cells(Rows.count, "B").End(xlUp).Row + 1 Range("B" & newRow).Value = "Service" Range("C" & newRow).Value = cell.Offset(0, 2).Value Range("D" & newRow).Value = cell.Value Range("F" & newRow).Value = cell.Offset(0, -1).Value Range("H" & newRow).Value = newDate Range("J" & newRow).Value = cell.Offset(0, 3).Value Range("L" & newRow).Value = cell.Offset(0, 5).Value Exit Sub End If End If Next cell End If If Target.Column = 5 Then ColumnList Target End If If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, Range("I:I")) Is Nothing Then If IsDate(Target.Value) Then If Range("B" & Target.Row).Value <> "Remind" Then Exit Sub Else MsgBox "Please change 'Remind' to the relevant Task Issue" Range("B" & Target.Row).Select Exit Sub End If End If End If End Sub
- Assistant