Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Call a sub on WS Activation

Posted on 2011-02-11
12
Medium Priority
?
314 Views
Last Modified: 2012-05-11
Hi,

I have two Private Subs which i want to call on workbook opening.

The WS i called "Check" and is going to be very hidden.

The subs are below.

What is the best code to use to call these subs on WB opening?

Thanks
Seamus
0
Comment
Question by:Seamus2626
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 24

Expert Comment

by:StephenJR
ID: 34870858
Use the Workbook-Open event (in the ThisWorkbook module)?
0
 
LVL 8

Expert Comment

by:ragnarok89
ID: 34870919
The first sub in your workbook must be called   Auto_open()

Auto_open() will have 2 statements:
call x
call y

where x and y are the names of your private subs.

Al
0
 

Author Comment

by:Seamus2626
ID: 34870928
Ive tried Stephen, but it calls the subs undefined?

Cheers,
Seamus
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 24

Expert Comment

by:StephenJR
ID: 34870944
Seamus - can you post a sample workbook?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34871256
Why are they Private if you want to call them from another module? Make them Public instead.
0
 

Author Comment

by:Seamus2626
ID: 34871344
Hi.

I put this in the workbook opening


Public Sub Workbook_Open()
Call Workbook_BeforeClose
Workbook_Open
End Sub

I called the subs Public and it gives me the message when the file opens

"Argument Not Optional"

Thanks
Seamus
0
 
LVL 24

Assisted Solution

by:StephenJR
StephenJR earned 668 total points
ID: 34871383
Seamus - what are you trying to do? Why close the workbook when it opens and then try to open it again? (Though pretty sure you can't use workbook events like that.)
0
 

Author Comment

by:Seamus2626
ID: 34871422
Im not trying to close it Stephen, when this work book opens i want it to apply the below code.

It basically shows what time the user enters and after they close what time they left.

Thanks
Seamus
Public Sub Workbook_BeforeClose(Cancel As Boolean)
With Sheets("check")
    .Range("B64000").End(xlUp).Offset(0, 2) = Now()
End With
ActiveWorkbook.Save
End Sub

Public Sub Workbook_Open()
With Sheets("check")
   .Range("B64000").End(xlUp).Offset(1, 0) = Environ("USERNAME")
   .Range("B64000").End(xlUp).Offset(0, 1) = Now()
End With
End Sub

Open in new window

0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 668 total points
ID: 34871436
Basically, move the code you have in the BeforeClose event into a sub in a normal module and then call that sub both from the BeforeCLose event and from the Open event.

However, as Stephen asked - what are you actually trying to achieve?
0
 
LVL 31

Assisted Solution

by:Helen Feddema
Helen Feddema earned 664 total points
ID: 34872541
From the Workbook_Open event you should call a public sub, say WorkbookOpen.  Then from the Workbook_BeforeClose event run another called (say) WorkbookClose.  The code to run when the workbook is opened should be in the WorkbookOpen sub.
0
 
LVL 31

Expert Comment

by:Helen Feddema
ID: 34872556
You might have some of the same code in these two subs.  No problem, since they are both simple.  If more is involved, you could make some more public subs and call several of them (different selections) from the WorkbookOpen and WorkbookClose subs.
0
 

Author Closing Comment

by:Seamus2626
ID: 34886777
Thanks all

Got it too work
Cheers,
Seamus
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question