• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 110
  • Last Modified:

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
0
Jagwarman
Asked:
Jagwarman
  • 4
  • 3
1 Solution
 
ProfessorJimJamCommented:
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
 
ProfessorJimJamCommented:
also check it in the attached file.
Book1.xlsm
0
 
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
ProfessorJimJamCommented:
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
 
ProfessorJimJamCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now