Copying a row to another tab when condition is met on expiry date

saileen

New Member
Joined
Nov 30, 2023
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi
I am trying to copy rows in multiple tabs that once they reach < = 30 days of expiry date into a new tab. There are up to 4 expiration dates per row/sheet (rows J, L, N, P). Am I able to move onto the new sheet with those conditions, and do I need to create an additional column to reference which tab the row is being copied from to help identify from? Also if the expiry date is updated on the original tab will this automatically remove the copied row since it won't meet the condition?

All the sheets have the same column names and the sheet I would be copying them to is called S1
1701782401757.png
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I that case, we can just remove the deletion lines:
VBA Code:
Option Explicit
Sub test_V3()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("CRASH CART")
    Set ws2 = Worksheets("S1")
    
    Dim LRow As Long, LCol As Long
    LRow = ws1.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    LCol = ws1.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column
    
    Dim a, b
    a = ws1.Range(ws1.Cells(9, 10), ws1.Cells(LRow, 16))
    ReDim b(1 To UBound(a, 1), 1 To 1)
    
    Dim i As Long, j As Long
    For i = 1 To UBound(a, 1)
        For j = 1 To 7 Step 2
            If IsDate(a(i, j)) And a(i, j) <= Date + 30 Then b(i, 1) = 1
        Next j
    Next i
    ws1.Cells(9, LCol + 1).Resize(UBound(b, 1)).Value = b
    If WorksheetFunction.Sum(ws1.Columns(LCol + 1)) > 0 Then
    With ws1
        .ListObjects(1).AutoFilter.ShowAllData
        With .ListObjects(1)
            .AutoFilter.ShowAllData
            .Range.AutoFilter LCol + 1, 1
            With .DataBodyRange.SpecialCells(xlCellTypeVisible)
                .Copy ws2.Cells(Rows.Count, 1).End(xlUp).Offset(1)
            End With
            .AutoFilter.ShowAllData
        End With
    End With
    End If
    ws1.Columns(LCol + 1).Delete
    ws2.Columns(LCol + 1).Delete
End Sub
 
Upvote 0
Solution
I that case, we can just remove the deletion lines:
VBA Code:
Option Explicit
Sub test_V3()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("CRASH CART")
    Set ws2 = Worksheets("S1")
  
    Dim LRow As Long, LCol As Long
    LRow = ws1.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    LCol = ws1.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column
  
    Dim a, b
    a = ws1.Range(ws1.Cells(9, 10), ws1.Cells(LRow, 16))
    ReDim b(1 To UBound(a, 1), 1 To 1)
  
    Dim i As Long, j As Long
    For i = 1 To UBound(a, 1)
        For j = 1 To 7 Step 2
            If IsDate(a(i, j)) And a(i, j) <= Date + 30 Then b(i, 1) = 1
        Next j
    Next i
    ws1.Cells(9, LCol + 1).Resize(UBound(b, 1)).Value = b
    If WorksheetFunction.Sum(ws1.Columns(LCol + 1)) > 0 Then
    With ws1
        .ListObjects(1).AutoFilter.ShowAllData
        With .ListObjects(1)
            .AutoFilter.ShowAllData
            .Range.AutoFilter LCol + 1, 1
            With .DataBodyRange.SpecialCells(xlCellTypeVisible)
                .Copy ws2.Cells(Rows.Count, 1).End(xlUp).Offset(1)
            End With
            .AutoFilter.ShowAllData
        End With
    End With
    End If
    ws1.Columns(LCol + 1).Delete
    ws2.Columns(LCol + 1).Delete
End Sub
I am sure this is working when you do it, so I am wondering does it matter which sheet I enter this code into? Either S1 or Crash Cart? I removed the 2 delete lines:
VBA Code:
ws1.Columns(LCol + 1).Delete
ws2.Columns(LCol + 1).Delete
Nothing appears on the S1 tab and then on Crash Cart the rows that are <=30 days/today are available to view and the other rows are hidden.
 
Upvote 0
I already removed the relevant lines for you in post #13. That's the code you should be using. The code goes into a standard module - not a sheet module. The code is working fine for me.
 
Upvote 0
Sorry for the delay - I was sick. This is working!

Thanks for all your help
I already removed the relevant lines for you in post #13. That's the code you should be using. The code goes into a standard module - not a sheet module. The code is working fine for me.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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