Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 28
Default VBA code needed for auto deletion upon opening

I need help writing a VBA code for 2007 Excel. I have a document that
multiple people have access to. Within this document there are dates and data
next to those dates. I need all dates deleted upon being opened if it's
greater than 6mos from the date that it is opened. The dates are in columns
E, H and K and the data for each date is directly to the right (F, I and L).
The information goes all the way down from row 5 to row 450. I hope this
makes sense.
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,203
Default VBA code needed for auto deletion upon opening

This code should do it for you. It is Workbook event code, so it needs to go
into the 'ThisWorkbook' object's code module. To get it there,
Make a copy of your workbook to test with.
Open the test copy of the workbook.
Press [Alt]+[F11] to open the VB Editor (VBE). In the VBE press [Ctrl]+[R]
to make sure the Project Explorer window is open.
The Project Explorer window with have a title like 'Project - VBAProject'
and in it will be a list of "Microsoft Excel Objects"; expand that list if
you have to. It will show all the worksheets in the book and at the bottom
of the list will be the "ThisWorkbook" object.
Double-click on the ThisWorkbook object to see its code module, which will
probably have no code at all in it.
Copy the code below, paste it into the ThisWorkbook code module, edit the
sheet name to be the sheet you need to clean up. Save and close the
workbook. Open the workbook to test it.

Private Sub Workbook_Open()
Dim myWS As Worksheet
Dim myRange As Range
Dim anyCell As Range
Dim testDate As Date

'change sheet name as required
Set myWS = ThisWorkbook.Worksheets("Sheet1")

testDate = Now() + 180

Set myRange = myWS.Range("E5:E450")
For Each anyCell In myRange
If anyCell testDate Then
anyCell.ClearContents
anyCell.Offset(0, 1).ClearContents
End If
Next

Set myRange = myWS.Range("H5:H450")
For Each anyCell In myRange
If anyCell testDate Then
anyCell.ClearContents
anyCell.Offset(0, 1).ClearContents
End If
Next

Set myRange = myWS.Range("K5:K450")
For Each anyCell In myRange
If anyCell testDate Then
anyCell.ClearContents
anyCell.Offset(0, 1).ClearContents
End If
Next
'housekeeping
Set myRange = Nothing
Set myWS = Nothing
End Sub


"JAbels001" wrote:

I need help writing a VBA code for 2007 Excel. I have a document that
multiple people have access to. Within this document there are dates and data
next to those dates. I need all dates deleted upon being opened if it's
greater than 6mos from the date that it is opened. The dates are in columns
E, H and K and the data for each date is directly to the right (F, I and L).
The information goes all the way down from row 5 to row 450. I hope this
makes sense.

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 28
Default VBA code needed for auto deletion upon opening

Thanks for responding.. I followed your steps to a "T" and when I opened the
file it did run the code and worked perfectly from what I could tell; however
I then placed some dates that are greater than 180 days (01/10/2009 for
example), saved and re-opened but it didn't delete that date... Any ideas?

"JLatham" wrote:

This code should do it for you. It is Workbook event code, so it needs to go
into the 'ThisWorkbook' object's code module. To get it there,
Make a copy of your workbook to test with.
Open the test copy of the workbook.
Press [Alt]+[F11] to open the VB Editor (VBE). In the VBE press [Ctrl]+[R]
to make sure the Project Explorer window is open.
The Project Explorer window with have a title like 'Project - VBAProject'
and in it will be a list of "Microsoft Excel Objects"; expand that list if
you have to. It will show all the worksheets in the book and at the bottom
of the list will be the "ThisWorkbook" object.
Double-click on the ThisWorkbook object to see its code module, which will
probably have no code at all in it.
Copy the code below, paste it into the ThisWorkbook code module, edit the
sheet name to be the sheet you need to clean up. Save and close the
workbook. Open the workbook to test it.

Private Sub Workbook_Open()
Dim myWS As Worksheet
Dim myRange As Range
Dim anyCell As Range
Dim testDate As Date

'change sheet name as required
Set myWS = ThisWorkbook.Worksheets("Sheet1")

testDate = Now() + 180

Set myRange = myWS.Range("E5:E450")
For Each anyCell In myRange
If anyCell testDate Then
anyCell.ClearContents
anyCell.Offset(0, 1).ClearContents
End If
Next

Set myRange = myWS.Range("H5:H450")
For Each anyCell In myRange
If anyCell testDate Then
anyCell.ClearContents
anyCell.Offset(0, 1).ClearContents
End If
Next

Set myRange = myWS.Range("K5:K450")
For Each anyCell In myRange
If anyCell testDate Then
anyCell.ClearContents
anyCell.Offset(0, 1).ClearContents
End If
Next
'housekeeping
Set myRange = Nothing
Set myWS = Nothing
End Sub


"JAbels001" wrote:

I need help writing a VBA code for 2007 Excel. I have a document that
multiple people have access to. Within this document there are dates and data
next to those dates. I need all dates deleted upon being opened if it's
greater than 6mos from the date that it is opened. The dates are in columns
E, H and K and the data for each date is directly to the right (F, I and L).
The information goes all the way down from row 5 to row 450. I hope this
makes sense.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code Needed John Calder New Users to Excel 10 July 15th 09 11:42 PM
Auto deletion of excel file after a certain date - expiry Som Excel Discussion (Misc queries) 7 March 20th 09 09:59 AM
How can I activate a macro of auto deletion with respect to date G.Shankar New Users to Excel 1 May 31st 08 01:30 PM
VBA code help needed Martin Excel Discussion (Misc queries) 3 April 28th 06 09:28 AM
Row deletion and auto filter RMJames Excel Discussion (Misc queries) 0 March 15th 06 10:09 AM


All times are GMT +1. The time now is 04:09 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"