Hi all,
I have issue with my excel vba. I would like to create excel sheet in which when there is new data entry in sheet1, it will automatically copy and paste into Sheet2 and show the date. For tomorrow, the date will disappear for yesterday's new data and show tomorrow's date when new data entry being copied in Sheet2. Please see attached for illustration and below for my code.
In Module 1:
In Sheet1
In Sheet2
In ThisWorkbook
I have issue with my excel vba. I would like to create excel sheet in which when there is new data entry in sheet1, it will automatically copy and paste into Sheet2 and show the date. For tomorrow, the date will disappear for yesterday's new data and show tomorrow's date when new data entry being copied in Sheet2. Please see attached for illustration and below for my code.
In Module 1:
Code:
Sub Run()
Dim lastRow As Long
Dim y As Worksheet
Dim t As Worksheet
'lastRow = Sheets("y").Range("A100000").End(xlUp).Row + 1 ' then next free row in sheet2
Sheets("Sheet1").Range("C:V").Copy Destination:=Sheets("Sheet2").Range("C:V")
End Sub
Public Sub CreationDate(ByRef Target As Range)
Const CreateColumn As String = "B"
With Target
With .Worksheet.Cells(.Row, CreateColumn)
If Not IsDate(.Value) Then .Value = Date
.Offset(0, 1).Select
End With
End With
End Sub
In Sheet1
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim C As Range, B As Range, Inte As Range, r As Range, myvalue As String
Set C = Range("C:C")
Set Inte = Intersect(C, Target)
myvalue = "New"
If Inte Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each r In Inte
r.Offset(0, -1).Value = myvalue
Next r
Application.EnableEvents = True
Sheets("Sheet1").Range("C:V").Copy Destination:=Sheets("Sheet2").Range("C:V")
End Sub
In Sheet2
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Const TriggerColumn As String = "C"
Const HeaderRows As Long = 1
With Target
If .Column = Asc(TriggerColumn) - 64 Then
If .Row > HeaderRows And _
Trim(.Value) <> vbNullString Then _
CreationDate Target
End If
End With
End Sub
In ThisWorkbook
Code:
Private Sub Workbook_Open()
Sheets("Sheet1").Range("A1").Value = Format(Date, ("dd/mm/yyyy"))
Sheets("Sheet2").Range("A1").Value = Date - 1
If Sheets("Sheet1").Range("B:B").Text = "New" Then
Sheets("Sheet2").Range("B:B").Value = Date
Save
End If
End Sub