How to auto delete cells after a specific time period

rizwanulhasan

New Member
Joined
Jan 1, 2022
Messages
16
Office Version
  1. 2021
Platform
  1. Windows
Hi All,

I have a sheet as below where when cell C4 is set to Do Not Disturb, B4 switches to Not Available. After 29 seconds, cell C4 deletes automatically thus switching B4 back to Available.

Book1.xlsm
ABCD
1
2
3NameAvailabilityDND StatusDate
4AAvailable02-01-2022 12:00 PM
5BAvailable
6CAvailable
7DAvailable
Sheet1
Cell Formulas
RangeFormula
B4:B7B4=IF(C4="Do Not Disturb","Not Available","Available")
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C4:C7Cell Value="Do Not Disturb"textNO
B4:B7Expression=B4="Available"textNO
B4:B7Expression=B4="Not Available"textNO
Cells with Data Validation
CellAllowCriteria
B4:B7ListAvailable, Not Available
C4:C7ListDo Not Disturb



This is the VBA used and a sheet "Time Log" has been created.

VBA Code:
Private Sub Worksheet_Activate()

Dim stampcell As Range

Dim TLSh As Worksheet


Set TLSh = Worksheets("Time Log")


Application.ScreenUpdating = False

''' Check if pass the 24h period to delete content

For Each stampcell In TLSh.Range("C4:C100")

If Now > stampcell.Value2 + TimeValue("00:00:29") Then

Me.Range(stampcell.Address).ClearContents

stampcell.ClearContents

End If

Next stampcell

Application.ScreenUpdating = True


End Sub


Private Sub Worksheet_Change(ByVal Target As Range)

Dim myRng As Range

Dim stampcell As Range

Dim TLSh As Worksheet


Application.EnableEvents = False


Set TLSh = Worksheets("Time Log") ''' Time stamp sheet

Set myRng = Range("C4:C100") ''' Your targeted range



If Not Intersect(Target, myRng) Is Nothing Then '''Time-stamping

TLSh.Range(Target.Address).Value2 = Now

TLSh.Range(Target.Address).NumberFormat = "MM/DD/YYYY hh:mm:ss"

End If



Application.EnableEvents = True


End Sub
VBA Code:


However, what I want is that once C4 is set to Do Not Disturb, instead of deleting it automatically after 29 seconds, it should ask to enter the date and time in the adjacent CELL D4.

For ex. D4 = 02-01-2022 12:00 PM. Now the cell C4 should remain as Do Not Disturb until the date and time mentioned in D4. Post this, it should delete itself thus switching B4 back to Available.

Can someone please help with such kind of VBA.

Thanks in advance.

Note: same thread also posted on excelforum.

 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,215,035
Messages
6,122,785
Members
449,095
Latest member
m_smith_solihull

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