Can someone please assist me with a vba code for the following:
I want to assign a code to the load button in sheet2 to extract rows from "Sheet1" into "Sheet 2" based on the the date in cell Sheet2!C2.
Exampleimage)
Unsuccessful in modifying the below code to suit my requirement:
The below code extracts all values in sheet1!Range (A:C) to sheet2!Range (A:C), but I just need to extract the rows that match with the date in Sheet2!C2.
Code:
Private Sub Click()
Dim Sh1 As Worksheet
Dim Sh2 As Worksheet
Set Sh1 = Worksheets("Sheet1")
Set Sh2 = Worksheets("Sheet2")
Dim lr As Long, lr2 As Long, r As Long
Sh2.Range("A4:C20").Clear
With Sh1
lr = Sh1.Cells(Rows.Count, "A").End(xlUp).Row
lr2 = Sh2.Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
If IsDate(Sh1.Range("A" & r).Value) Then
Sh1.Range("A" & r & ":C" & r).Copy Destination:=Sh2.Range("A" & lr2 + 1)
lr2 = Sh2.Cells(Rows.Count, "A").End(xlUp).Row
End If
Next r
End With
End Sub
I want to assign a code to the load button in sheet2 to extract rows from "Sheet1" into "Sheet 2" based on the the date in cell Sheet2!C2.
Exampleimage)
Unsuccessful in modifying the below code to suit my requirement:
The below code extracts all values in sheet1!Range (A:C) to sheet2!Range (A:C), but I just need to extract the rows that match with the date in Sheet2!C2.
Code:
Private Sub Click()
Dim Sh1 As Worksheet
Dim Sh2 As Worksheet
Set Sh1 = Worksheets("Sheet1")
Set Sh2 = Worksheets("Sheet2")
Dim lr As Long, lr2 As Long, r As Long
Sh2.Range("A4:C20").Clear
With Sh1
lr = Sh1.Cells(Rows.Count, "A").End(xlUp).Row
lr2 = Sh2.Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
If IsDate(Sh1.Range("A" & r).Value) Then
Sh1.Range("A" & r & ":C" & r).Copy Destination:=Sh2.Range("A" & lr2 + 1)
lr2 = Sh2.Cells(Rows.Count, "A").End(xlUp).Row
End If
Next r
End With
End Sub