View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
Boiler-Todd Boiler-Todd is offline
external usenet poster
 
Posts: 9
Default Copy Sheet to new Sheet and clear cells on original sheets

Awesome Dave!

I think this will work. Thanks for the quick response.

"Dave Peterson" wrote:

Maybe...

Sub Workbook_Open()
dim iCtr as long
dim myCell as range

'copy the sheets each time--no matter what
Sheets(1).Copy After:=Sheets(Sheets.Count)
Sheets(2).Copy After:=Sheets(Sheets.Count)
Sheets(3).Copy After:=Sheets(Sheets.Count)

'check to see if the original sheets should be cleared
for ictr = 1 to 3
with sheets(ictr)
If Date .Range("A1").Value - 2 Then
For Each mycell In .Range("A1:H10").cells
If Not mycell.Locked Then
mycell.ClearContents
end if
Next mycell
End If
end with
Next ictr
End Sub

The with/end with structure means that all those objects with the leading dots
(.Ranges()'s) belong to the object in the previous with statement. In this
case, Sheets(ictr) (sheets(1), sheets(2) and sheets(3)).

I made some minor changes just because.

I like the multiline "if" statement better than the single line. I find it
easier to change and debug (lining up If's and End if's).


Boiler-Todd wrote:

I am new to using VBA so let me explain what I am trying to do...
I want to be able to copy 3 tabs(sheets) in a workbook into 3 new
tabs(sheets) into the same workbook. Also if the date is two days later then
I want to delete values on the orignal sheets. I also want to do this
everytime the sheet is opened.

My current code loops to copy each sheet three times and then deletes data
on the copied sheets but not the original sheets. See below. Thanks for all
help!

Sub Workbook_Open()
' This Macro will copy sheets 1-2-3 to new sheets A-B-C then
' if date is 2 days past clear out the UNLOCKED cells of a sheets (1-3)
'
Dim cell As Range
For Each sh In ActiveWorkbook.Sheets
If Date sh.Range("A1").Value - 2 Then
Sheets(1).Copy After:=Sheets(Sheets.Count)
Sheets(2).Copy After:=Sheets(Sheets.Count)
Sheets(3).Copy After:=Sheets(Sheets.Count)
For Each cell In Range("A1:H10")
If Not cell.Locked Then cell.ClearContents
Next cell
End If
Next
End Sub


--

Dave Peterson