VBA to check between two dates, compare with today and if true then copy entire data
Hi All,
I need a VBA code that will check between two dates (cells G & H in the attachment) and compare it with today's date. If true then extract data from entire row of a found match and paste it in a specific range.
Or, alternatively, perhaps a conditional formatting in which I can paste entire data chart (for example A2-H6 in the attachment) and filter/show only data that matches today's date.
Re: VBA to check between two dates, compare with today and if true then copy entire data
Option Explicit
Sub Vacheron()
Dim i As Long, lr As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lr
If Date >= Range("G" & i) And Date <= Range("H" & i) Then
Range("A" & i).EntireRow.Interior.ColorIndex = 3
End If
Next i
End Sub
Re: VBA to check between two dates, compare with today and if true then copy entire data
Here is an alternate solution for you
Option Explicit
Sub Vach2()
Dim s1 As Worksheet, s2 As Worksheet
Set s1 = Sheets("Sheet1")
Set s2 = Sheets("Sheet2")
Dim i As Long, lr As Long, lr2 As Long
s1.Range("A1:H1").Copy s2.Range("A1")
lr = s1.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lr
If Date >= s1.Range("G" & i) And Date <= s1.Range("H" & i) Then
lr2 = s2.Range("A" & Rows.Count).End(xlUp).Row
s1.Range("A" & i & ":H" & i).Copy
s2.Range("A" & lr2 + 1).PasteSpecial xlPasteValues
End If
Next i
Application.CutCopyMode = False
End Sub
Re: VBA to check between two dates, compare with today and if true then copy entire data
Hey Alan,
This code worked for me, the first one didn't, it would just highlight everything with red.
Thanks a lot!
Sub J3v16()
Dim Arr, Data
With Sheet1.Cells(1).CurrentRegion
.Rows(1).Copy Sheet2.Cells(1)
Data = Filter(.Parent.Evaluate("transpose(if((" & .Columns(6).Address & ">=""" & Date & """)*(" & .Columns(7).Address & "<=""" & Date & """),row(1:" & .Rows.Count & ")))"), False, 0)
If UBound(Data) > -1 Then
Arr = Application.Transpose(Application.Index(.Value, Data, Application.Evaluate("row(1:" & .Columns.Count & ")")))
With Sheet2
.Cells(1).Offset(1).Resize(UBound(Arr, 1), UBound(Arr, 2)).Value = Arr
With .UsedRange: .Borders.Weight = 2: .Columns.AutoFit: End With
End With
End If
End With
End Sub
Last edited by alansidman; 10-16-2022 at 08:58 PM.
Re: VBA to check between two dates, compare with today and if true then copy entire data
Non looping option...
Sub J3v16()
Dim Arr, Data
With Sheet1.Cells(1).CurrentRegion
.Rows(1).Copy Sheet2.Cells(1)
Data = Filter(.Parent.Evaluate("transpose(if((" & .Columns(6).Address & ">=""" & Date & """)*(" & .Columns(7).Address & "<=""" & Date & """),row(1:" & .Rows.Count & ")))"), False, 0)
If UBound(Data) > -1 Then
Arr = Application.Transpose(Application.Index(.Value, Data, Application.Evaluate("row(1:" & .Columns.Count & ")")))
With Sheet2
.Cells(1).Offset(1).Resize(UBound(Arr, 1), UBound(Arr, 2)).Value = Arr
With .UsedRange: .Borders.Weight = 2: .Columns.AutoFit: End With
End With
End If
End With
End Sub
Good Luck...
I don't presume to know what I am doing, however, just like you, I too started somewhere...
One-day, One-problem at a time!!!
If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
Also....Add a comment if you like!!!!
And remember...Mark Thread as Solved...
Excel Forum Rocks!!!
Re: VBA to check between two dates, compare with today and if true then copy entire data
Hey Sintek,
Thanks, for whatever reason it is missing out some entries, some rows have the correct data range and they should be copied to the second sheet but from 33 entries it is missing 6 of them all valid as compared with today's date.
Any thoughts?
Re: VBA to check between two dates, compare with today and if true then copy entire data
Hello.
Due to the regional configuration issue on my computer, what was proposed in post #5 does not work for me. So I expose another way that -it seems to me- works in all configurations:
PHP Code:
Sub myFilter()
Dim a, Q&, i&, R&, j%
With Sheets("Sheet1").Range("A1").CurrentRegion
a = .Value: Q = UBound(a): .Rows(1).Copy Sheets("Sheet2").Range("A1")
End With
For i = 2 To Q
If a(i, 7) <= Date And Date <= a(i, 8) Then
R = 1 + R: For j = 1 To UBound(a, 2): a(R, j) = a(i, j): Next
End If
Next
With Sheets("Sheet2")
.Cells(1).CurrentRegion.Offset(1).Delete xlShiftUp
If R > 0 Then .Cells(2, 1).Resize(R, UBound(a, 2)) = a
End With
End Sub
Last edited by beyond Excel; 10-16-2022 at 06:03 PM.
Bookmarks