Adding Clear Contents command to a current VBA code

hn61122

New Member
Joined
Jun 11, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
New to VBA I have a code that will updated the Year automatically 6 months after expiration. But now I would like it to clear the contents of the cell once it updates to the new year. Below is my current code:

Private Sub Worksheet_Activate()
Dim N As Long, r As Range, rng As Range
Dim d As Date
N = Cells(Rows.Count, "B").End(xlUp).Row
Set rng = Range("B7:B" & N)

For Each r In rng
If IsDate(r.Value) Then
d = r.Value
If d < Date - 180 Then
r.Value = DateSerial(Year(d) + 1, Month(d), Day(d))
End If
End If
Next r
End Sub

When each cell updates to its New Year i would like for the their specified range of cells to clear contents at the same time:
Example:
When b7 Updates contents in cell B8-AO10 to clear
When b11 Updates contents in cell B12-AO14 to clear


** I have not wrote anything to clear contents because everything write doesn't work, more than likely because I don't know much about VBA's. I have researched for days and cant figure this out. Any help would be greatly appreciated.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
hn just a suggestion. Have you tried using the Record feature? That might give you a hint. Also, can you use that XL2BB software? You will get lots of solutions if you use it. Also when you say it doesn't work, what doesn't work?
 
Upvote 0
Try the following with a copy of your file. Put this line of code:
VBA Code:
r.Offset(1).Resize(3, 40).ClearContents

Immediately after this line:
VBA Code:
r.Value = DateSerial(Year(d) + 1, Month(d), Day(d))
 
Upvote 0
Try the following with a copy of your file. Put this line of code:
VBA Code:
r.Offset(1).Resize(3, 40).ClearContents

Immediately after this line:
VBA Code:
r.Value = DateSerial(Year(d) + 1, Month(d), Day(d))
Omg, this worked so well and was exactly what needed. I really appreciate your help!!!!!! i think i can finally close this workbook to night! OMG,, thank you, thank you, thank you!
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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