emptiness_void
New Member
- Joined
- Mar 29, 2021
- Messages
- 7
- Office Version
- 2019
- Platform
- Windows
Hello guys! Hope u are good!
Could someone please help me with this code I'm trying to edit an reach my goal?
Goal: copy rows from a Sheet1 to a Sheet2, that meet a criteria, with a Date and time record!
for example, all rows that have yellow as text in the colum G of sheet one, after I click in a button, they should be copied for the Sheet2! And adding the date and time of that action in the historic in Sheet 2!
Here goes an example of what I'm trying to achive:
Sheet1
Sheet2
Here follows the code I'm using atm:
I thought 2 ways to achieve this:
Thank you in advance
Could someone please help me with this code I'm trying to edit an reach my goal?
Goal: copy rows from a Sheet1 to a Sheet2, that meet a criteria, with a Date and time record!
for example, all rows that have yellow as text in the colum G of sheet one, after I click in a button, they should be copied for the Sheet2! And adding the date and time of that action in the historic in Sheet 2!
Here goes an example of what I'm trying to achive:
Sheet1
Sheet2
Here follows the code I'm using atm:
VBA Code:
Sub Sample()
Dim wb1 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim copyFrom As Range
Dim myArr As Variant
Dim lRow As Long
Dim strSearch As String
Dim i As Long, j As Long
Dim upper As Long, down As Long
Set wb1 = ThisWorkbook
Set ws1 = wb1.Worksheets("Sheet1")
strSearch = "yellow"
With ws1
.AutoFilterMode = False
lRow = .Range("G" & .Rows.Count).End(xlUp).Row
With .Range("G1:G" & lRow)
.AutoFilter Field:=7, Criteria1:="=*" & strSearch & "*"
Set copyFrom = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
End With
.AutoFilterMode = False
End With
Set ws2 = wb1.Worksheets("Sheet2")
With ws2
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
lRow = 1
End If
myArr = copyFrom.Value ' I can't find a way to copy the values in the Array
upper = UBound(myArr)
MsgBox (upper)
ReDim Preserve myArr(0 To upper, 0 To down)
For i = 1 To UBound(myArr)
For j = UBound(X, 2) To 2 Step -1
myArr(i, j) = myArr(i, j - 1)
Next
myArr(i, 1) = Format(Now, "mm/dd/yyyy HH:mm:ss")
Next
myArr.Copy .Rows(lRow)
End With
End Sub
I thought 2 ways to achieve this:
- Pass the values in the range to an Array and the resize the array to add the date&time to the first column; OR
- Copy the range after the first column in Sheet2, and then fill the blanks for the rows copied, with the date & time.
Thank you in advance