Search & move sheet

DanK

New Member
Joined
Jul 20, 2009
Messages
10
So far in my many variations i can't get this to work so maybe the guru's here can pinpoint my obvious flaw in logic. This is an on open script that is supposed to scan the sheets within another workbook and skip a specific named sheet then add a formula to see if the date in the cell is greater then a year old (problem with the cell being blank returns true). If so then moves the scanned sheet into the starting workbook and then deletes the scanned sheet. [Summary: checks sheets to see if it is old then moves into another workbook and removes the original]

Thanx for any wisdom you can share.

' This code is used from one workbook "Archives 1.0.xls" on open and searches another workbooks sheets
Dim SlotArchive
Archive = Workbooks("Cabinet ID 1.0.xls").Worksheets.Count
For i = 1 to Archive
' Looks for the sheet name and skips if "Index"
if Sheets(i).Name <> "Index" then
' Unlocks the sheet
ActiveSheets.Unprotect Password = (Workbooks("Slot Array 3.5.xls").Worksheets("Intro").Range("U2")

' Selects cell S1 and places a formula to calculate if the number of days from the date in cell C4 to today exceeds 1 year
Sheets(i).Range("S1").Select
Selection.Clearcontents
Range("S1").Formula = "=Days360(C4,Today())"

' if the days are longer then 1 year, problem is if the cell is blank it always answers TRUE
and if Sheets(i).Range("S1").Value > "365" then

' Clears the previous formula
Range("S1").Select
Selection.ClearContents

' Copies the sheet and moves it to another workbook
ActiveWorkbook.Sheets(i).Copy
ActiveSheet.Move After:=Workbook("Archives 1.0.xls").Sheets("Index")

' Delets the old sheet from the previous workbook
Application.DisplayAlerts = FALSE
Workbooks("Cabinet ID 1.0.xls").Worksheets(i).Delete
Application.DisplayAlerts = TRUE
end if
end if
next i
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
All that selecting is unnecessary and inefficient. I think that you need to remove the quotes

Code:
If Sheets(i).Range("S1").Value > 365 Then

Please use code tags when posting code.
 
Upvote 0
Sorry, that makes no sense. If you are checking for a numeric value you do not need ". In fact, that will give erroneous results since a value in quotes is always greater than a numeric value.
 
Upvote 0
ok i see what you are saying, that makes sense. The other problem is that if the value it is checking against "Days360(C4,today())" if C4 is blank it gives the value from 1/1/1900 so in the 48000 range. I really didn't want to add extra if statements to check for blanks.
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,543
Members
452,924
Latest member
JackiG

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