VBA Clear Contents of Cells Less than Today's Date

mdeni0528

New Member
Joined
Aug 11, 2021
Messages
16
Office Version
  1. 2010
Platform
  1. Windows
Hi!

I'm hoping you can help me with a VBA formula that clears the contents of dates in cells based on an end date that is less than today's date. My worksheet has a start date and an end date for multiple segments and these dates are associated with one another. The clear contents command needs to be triggered by the end date when it's less than today's date. The end date needs to be cleared from the worksheet as well as the start date associated with it. I have multiple columns of start dates and end dates, so I need to apply this command to multiple columns. I need a formula similar to the one below but for entire columns since this only applies to one cell and is not written correctly. My start and end dates that should be deleted are highlighted in red using a conditional format. Please help! I don't know VBA language at all! Thank you!


If Date > Range("C9").Value then Range("C9").ClearContents
If Range("B9")> isnotblank and Range("C9")Then Range("B9").ClearContents
Capture_VBA Help Example.PNG
 
Hi Dante! I password protected my sheet and I'm getting this run-time error below. Do you know why? When I hit End, it goes away...

1628814592706.png
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I password protected my sheet

The sheet must be unprotected.
Change "abc" in the 2 lines of the macro your password:

Rich (BB code):
Sub auto_open()
  Dim i As Long, j As Long
  Application.ScreenUpdating = False
  ActiveSheet.Unprotect "abc"
  For i = 9 To Range("A" & Rows.Count).End(3).Row
    For j = 3 To Cells(i, Columns.Count).End(1).Column Step 2
      If Date > Cells(i, j).Value Then
        Cells(i, j - 1).Resize(1, 2).Value = ""
      End If
    Next
  Next
  ActiveSheet.Protect "abc"
End Sub
 
Upvote 0
Hi Dante!

I'm wanting my users to be able to use filters in the worksheet. Do I add to the code to allow for this capability? Thanks again!
 
Upvote 0
Change this line:
VBA Code:
ActiveSheet.Protect "abc"

For this:
VBA Code:
ActiveSheet.Protect "abc", DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True
 
Upvote 0
Hi Dante,
Can you please help again with this formula? I have 3 worksheets in my workbook/file and the vba formula should only be clearing contents from the first worksheet not any of the others. However, it is clearing contents from the 2nd worksheet if I close the file when the 2nd worksheet is active. Can you please help? The formula is showing below :) Thank you!!

Sub auto_open()
Dim i As Long, j As Long
Application.ScreenUpdating = False
ActiveSheet.Unprotect "****"
For i = 9 To Range("A" & Rows.Count).End(3).Row
For j = 3 To Cells(i, Columns.Count).End(1).Column Step 2
If Date > Cells(i, j).Value Then
Cells(i, j - 1).Resize(1, 2).Value = ""
End If
Next
Next
ActiveSheet.Protect "****", DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True
End Sub
 
Upvote 0
I have 3 worksheets in my workbook/file and the vba formula should only be clearing contents from the first worksheet not any of the others.
Try this:

VBA Code:
Sub auto_open()
  Dim i As Long, j As Long
  Dim sh As Worksheet
  
  Application.ScreenUpdating = False
  For Each sh In Sheets
    sh.Unprotect "***"
    For i = 9 To sh.Range("A" & Rows.Count).End(3).Row
      For j = 3 To sh.Cells(i, Columns.Count).End(1).Column Step 2
        If Date > sh.Cells(i, j).Value Then
          sh.Cells(i, j - 1).Resize(1, 2).Value = ""
        End If
      Next
    Next
    sh.Protect "***", DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True
  Next
End Sub
 
Upvote 0
Sorry, that didn't work. It still deleted dates on the 2nd worksheet.
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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