Hi, I am new to VBA and have use the below code to copy rows that meet the criteria entered when prompted. Now I am trying to tweak the code to pull in dates from column K that are less than the date entered when prompted. The code works, but is also pulling in 1/1/16+ dates also. Please let me know what needs updated so excel formats the dates correctly. When the data tab is formatted for Dates in column K, still run into the same issue.
If Range("K" & CStr(LSearchRow)).Value <= LSearchValue Then
Thanks!
Sub Update()
With Sheets("NEW")
.Rows(2 & ":" & .Rows.Count).Delete
End With
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
Dim LSearchValue As String
On Error GoTo Err_Execute
LSearchValue = InputBox("Please enter end Date.", "Enter value MM/DD/YY")
'Start search in row 2
LSearchRow = 2
'Start copying data to row 2 in NEW (row counter variable)
LCopyToRow = 2
While Len(Range("A" & CStr(LSearchRow)).Value) > 0
'If value in column I = LSearchValue, copy entire row to NEW
If Range("K" & CStr(LSearchRow)).Value <= LSearchValue Then
'Select row in Sheet1 to copy
Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
Selection.Copy
'Paste row into NEW in next row
Sheets("NEW").Select
Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
'Move counter to next row
LCopyToRow = LCopyToRow + 1
'Go back to Sheet1 to continue searching
Sheets("Data").Select
End If
LSearchRow = LSearchRow + 1
Wend
'Position on cell A3
Application.CutCopyMode = False
Range("A1").Select
MsgBox "All matching data has been copied."
Exit Sub
Err_Execute:
MsgBox "An error occurred."
End Sub
If Range("K" & CStr(LSearchRow)).Value <= LSearchValue Then
Thanks!
Sub Update()
With Sheets("NEW")
.Rows(2 & ":" & .Rows.Count).Delete
End With
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
Dim LSearchValue As String
On Error GoTo Err_Execute
LSearchValue = InputBox("Please enter end Date.", "Enter value MM/DD/YY")
'Start search in row 2
LSearchRow = 2
'Start copying data to row 2 in NEW (row counter variable)
LCopyToRow = 2
While Len(Range("A" & CStr(LSearchRow)).Value) > 0
'If value in column I = LSearchValue, copy entire row to NEW
If Range("K" & CStr(LSearchRow)).Value <= LSearchValue Then
'Select row in Sheet1 to copy
Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
Selection.Copy
'Paste row into NEW in next row
Sheets("NEW").Select
Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
'Move counter to next row
LCopyToRow = LCopyToRow + 1
'Go back to Sheet1 to continue searching
Sheets("Data").Select
End If
LSearchRow = LSearchRow + 1
Wend
'Position on cell A3
Application.CutCopyMode = False
Range("A1").Select
MsgBox "All matching data has been copied."
Exit Sub
Err_Execute:
MsgBox "An error occurred."
End Sub