Pull values of a column of one sheet based on dates

mshahbaz

New Member
Joined
Feb 13, 2017
Messages
36
Hi,

Sheet 1 (Named as Imports):

[TABLE="width: 500"]
<tbody>[TR]
[TD]Column A[/TD]
[TD]........[/TD]
[TD]........[/TD]
[TD]........[/TD]
[TD]......[/TD]
[TD]Column G[/TD]
[/TR]
[TR]
[TD]Material name[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Arrival date[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


Sheet 2 (Named as To dos):

Column D

I need Name of materials (arriving within in next 30 days) with their arrival date in column D of sheet 2.

VBA method is preferable.


Regards[TABLE="width: 500"]
<tbody>[TR]
[TD]Column[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Code:
Option Explicit


Sub Next30()
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("Imports")
    Set s2 = Sheets("To dos")
    Dim i As Long, lr As Long, lr2 As Long
    lr = s1.Range("G" & Rows.Count).End(xlUp).Row
    For i = 2 To lr
        lr2 = s2.Range("D" & Rows.Count).End(xlUp).Row
        If s1.Range("G" & i) > Date - 30 Then
            s1.Range("A" & i).Copy s2.Range("D" & lr2 + 1)
        End If
    Next i


End Sub
How to install your new code
Copy the Excel VBA code
Select the workbook in which you want to store the Excel VBA code
Press Alt+F11 to open the Visual Basic Editor
Choose Insert > Module
Edit > Paste the macro into the module that appeared
Close the VBEditor
Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)


To run the Excel VBA code:
Press Alt-F8 to open the macro list
Select a macro in the list
Click the Run button
 
Last edited:
Upvote 0
Thank You



Code:
Option Explicit


Sub Next30()
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("Imports")
    Set s2 = Sheets("To dos")
    Dim i As Long, lr As Long, lr2 As Long
    lr = s1.Range("G" & Rows.Count).End(xlUp).Row
    For i = 2 To lr
        lr2 = s2.Range("D" & Rows.Count).End(xlUp).Row
        If s1.Range("G" & i) > Date - 30 Then
            s1.Range("A" & i).Copy s2.Range("D" & lr2 + 1)
        End If
    Next i


End Sub
How to install your new code
Copy the Excel VBA code
Select the workbook in which you want to store the Excel VBA code
Press Alt+F11 to open the Visual Basic Editor
Choose Insert > Module
Edit > Paste the macro into the module that appeared
Close the VBEditor
Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)


To run the Excel VBA code:
Press Alt-F8 to open the macro list
Select a macro in the list
Click the Run button
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top