open file and activate Macro but

Need an experts help [again]

I want my file to start the macro when the file is opened and clear cells L4 to L22 in the sheet called Macro but if the date in cell D2 is today exit the sub [do nothing]

Can someone provide me the code please.

Thanks
JagwarmanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ProfessorJimJamMicrosoft Excel ExpertCommented:
put this code, in "ThisWorkbook"  

Private Sub Workbook_Open()
Sheets("Macro").Select
If Cells(2, 4).Value = Date Then
Exit Sub
Else
Sheets("Macro").Select
Range("L4:L22").Select
    Selection.ClearContents
    End If
End Sub

Open in new window

0
ProfessorJimJamMicrosoft Excel ExpertCommented:
also check it in the attached file.
Book1.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Saqib Husain, SyedEngineerCommented:
All you really need is

Private Sub Workbook_Open()
If Cells(2, 4).Value = Date Then Exit Sub
Sheets("Macro").Range("L4:L22").ClearContents
End Sub
0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

ProfessorJimJamMicrosoft Excel ExpertCommented:
Saqib,

that is what i have suggested, in my first post.
0
JagwarmanAuthor Commented:
brilliant thanks
0
Saqib Husain, SyedEngineerCommented:
What I was pointing out is that you did not need to select anything and you did not need the else part of the If statement. I reproduced your code and eliminated the unnecessary parts.
0
ProfessorJimJamMicrosoft Excel ExpertCommented:
thank you Saqib,

but you may agree with me that if you do not put the first selection code of Macro worksheet. then your code, first run will be on any last active workbook when it was closed, meaning that if a sheet other than "macro" is active while workbook is opened, then the data in L4 to L22 would still get deleted, although that the Macro sheet would still have today's date.  so it wont work for the OP.
0
Saqib Husain, SyedEngineerCommented:
Ok I did not see that but that can be taken care of with

If Sheets("Macro").Cells(2, 4).Value = Date Then exit sub

Selection is not needed
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.