Using VBA to reference the date in cell in a different sheet

Captain_Conman

Board Regular
Joined
Jun 14, 2018
Messages
54
Hello!

I am very new with VBA and am currently working on writing a macro that deletes all the rows in a sheet, that hold a date that falls on or before 6/1/18. Here is what I have so far...

Sub Macro2()
With ActiveSheet
.AutoFilterMode = False
With Range("g1", Range("g" & Rows.Count).End(xlUp))
.AutoFilter 1, "<=6/1/18"
On Error Resume Next
.Offset(1).SpecialCells(12).EntireRow.Delete
End With
.AutoFilterMode = False
End With
End Sub

Everything works great, however, instead of typing the date into the code, I would like the macro to reference the date in the last row of column "G" on a sheet titled "Current". Any tips on how to make this reference would be appreciated! Thank you.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I doubt this will be the best way of coding this, but try using...

Code:
.AutoFilter 1, "<=" & Sheets("Current").Range("G" & Sheets("Current").Cells(Sheets("Current").Rows.Count, "G").End(xlUp).Row).Text
 
Upvote 0
Try this:
Code:
Sub Macro2()
'Modified 6/14/18 10:30 AM EDT
Dim Lastrow As Long
Lastrow = Sheets("Current").Cells(Rows.Count, "G").End(xlUp).Row
ans = Sheets("Current").Cells(Lastrow, "G").Value
With ActiveSheet
.AutoFilterMode = False
With Range("g1", Range("g" & Rows.Count).End(xlUp))
    .AutoFilter 1, "<=" & ans
    counter = .Columns("G").SpecialCells(xlCellTypeVisible).Count
    If counter > 1 Then
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    Else
        MsgBox "No values found"
    End If
End With
.AutoFilterMode = False
End With
End Sub
 
Upvote 0
THANK YOU SO MUCH!! :) It worked perfectly, which is an absolute day maker. I am so excited now! lol

THANK YOU THANK YOU THANK YOU!
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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