Solved

Help with installing VBA code in Excel please (already have the code)

Posted on 2010-11-20
11
523 Views
Last Modified: 2012-06-27
Hello,

Could some kind soul tell me how to get the following VBA instructions/code working?  (The code was posted at the end of another thread which has now been closed.)*  By the way, the overall goal is to get a continuously updating time display in Excel (essentially a clock).


*****qbanik comment #1:
>>
Add a module and dump this code
-------
Option Explicit

Public Sub DoTick()
Application.Calculate
Application.OnTime Now() + TimeValue("00:01"), DoTick()
End Sub
--------
On your workbook_open event add this code
DoTick    
<<


*****qbanik comment #2:
>>
To show the current time change the following
---
Option Explicit

Public Sub DoTick()
Application.Calculate
Application.OnTime Now() + TimeValue("00:01"), DoTick()
End Sub
----

to

----
Option Explicit

Public Sub DoTick()
Application.range("A1").value=now()
Application.OnTime Now() + TimeValue("00:01"), DoTick()
End Sub
-----
<<


*****qbanik comment #3:
>>
By the way, remember to format the [A1] cell as "h:mm:ss AM/PM;@" to see the clock in action.
<<


*****rorya comment #1:
>>
If you use OnTime, you should store the scheduled time in a public variable so that you can cancel it when you close the workbook. Otherwise, the workbook will get reopened unless you exit Excel completely.
<<


*****qbanik comment #4:
>>
Rorya is correct. In order to be clean, shut the event cleanly by setting the optional Schedule argument to False. Here is an article that tell you how to do it the right way. (See towards the end of the article).
http://www.ozgrid.com/Excel/run-macro-on-time.htm 
<<


QUESTIONS & ISSUES:

1) What does this mean?

    Add a module and dump this code

2) What does this mean?

    On your workbook_open event add this code
    DoTick    


I've tried everything I can think of and it always gives errors, e.g.:

Compile error:
    Expected Function or variable
    [DoTick () highlighted]

Compile error:
    Syntax error
    [DoTick turns red]

I've also gone through all the steps in the referenced url but that did not help.

Thanks

*The original question is here:

Conditional formatting for time of day in Excel
ID:  26616072
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26616072.html

*****Is there any way to give additional points for expert comments once a questions has been closed?
0
Comment
Question by:Steve_Brady
[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
11 Comments
 
LVL 20

Assisted Solution

by:Ardhendu Sarangi
Ardhendu Sarangi earned 100 total points
ID: 34181968
1) What does this mean?

    Add a module and dump this code
Do the following steps.
1. Open Excel and press ALT+F11
2. This will open up the VBA editor.
3.  From the Menu Bar, select Insert > Module. This will create a new module for you.
4. Copy and paste the code in the module.
5. Thats All.



2) What does this mean?

    On your workbook_open event add this code
    DoTick    
Now this can be a little trickier.
1. In the VBA Editor, double click on the "THISWORKBOOK" in the Project Explorer window. This is the first window on the left side of the screen. If you cannot find it hit CTRL+R.
2. Once you are on the ThisWorkbook page, look at the dropdown which says General. Click on the Dropdown menu and select Workbook from the DDL.
3. Now you should be in the Workbook_Open Event module.
4. Add DoTick inside the module.  
5. This is how it will look like -

Private Sub Workbook_Open()
DoTick
End Sub

Open in new window


0
 
LVL 5

Expert Comment

by:Pabilio
ID: 34181979
Ho Steve:

Question 1:

Once in excel, press Alt + F11
It will open your VBA application...
There you will see to the left side the rooth of your workbook...Sheet1, Sheet2 and so on...
Do a right Click on any sheet and select the option Add and then select New Module.
It will create a new module...you could see the modules in a File down the Sheets...
Souble click in the new module (If you don't have any modules there it will be called Module1
Copy the code that you want to add to this new module and paste it in the left window (The big one at the left side of the root)
End of question 1.

Question 2:
On the top of the left widows there are two listboxes... in the lefft one select WOrkbook and in the right one select open
It will create two lines...
a) Private Sub Workbook_Open()
b)End Sub

Place your code between those lines...

If you have any problem after that post your complete code here and any Expert will help you to debug it.

Hope it helps

Roberto.
0
 

Author Comment

by:Steve_Brady
ID: 34182034
Thanks for those great responses!...clear, concise, and well-written!  I only wish I had about 1K points to give each of you!  :)

After following your steps, I saved the file as .xlsms, closed it, and then restarted it. By the way, for the 1st step, I pasted this:

Option Explicit

Public Sub DoTick()
Application.range("A1").value=now()
Application.OnTime Now() + TimeValue("00:01"), DoTick()
End Sub

since my instructions above said to replace the first code with this one.

Unfortunately, it gave me one of the same errors I mentioned above:

    Compile error:
        Expected Function or variable

with a yellow arrow to the left of and yellow highlighting on the line:

    Public Sub DoTick()

and also blue highliting (as tho it's selected) on:

    DoTick

in the line:

    Application.OnTime Now() + TimeValue("00:01"), DoTick()

Is there something I may have done wrong?  Do I need to restart all instances of Excel?

Thanks again
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Accepted Solution

by:
Michael74 earned 400 total points
ID: 34182207
change the line
Application.OnTime Now() + TimeValue("00:01"), DoTick()

to

Application.OnTime Now() + TimeValue("00:01"), "DoTick"
0
 
LVL 23

Expert Comment

by:Michael74
ID: 34182208
here is a test file
Book1.xlsm
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34182212
Where did you paste dotick ... in thisworkbook?

If so add a module via insert | Module and move the dotick sub there.

Chris
0
 
LVL 45

Expert Comment

by:patrickab
ID: 34182942
*****Is there any way to give additional points for expert comments once a questions has been closed?

The maximum points that you can offer and award for any question is 500.

Patrick
0
 

Author Comment

by:Steve_Brady
ID: 34183630



    1) >>Michael74:
        change the line

That did it Michael. Thank you.



    2a) >>If you use OnTime, you should store the scheduled time in a public variable so that you can cancel it when you close the workbook. Otherwise, the workbook will get reopened unless you exit Excel completely.

    2b) >>In order to be clean, shut the event cleanly by setting the optional Schedule argument to False. Here is an article that tell you how to do it the right way. (See towards the end of the article).
http://www.ozgrid.com/Excel/run-macro-on-time.htm 

Now I've got it working (the time display) but can you explain what this means and how to do it (i.e. "...store the scheduled time in a public variable...")?

Thanks
0
 
LVL 23

Expert Comment

by:Michael74
ID: 34184359
Steve

A public variable is one that is available to call methods and functions. This is the line

Public dTime As Date

from the code on the website

When you close the workbook you need to completely cancel the macro. This is done by placing the line

Application.OnTime dTime, "MyMacro", , False

inside the workbook closing event.

Have a look at the attached file. To see all the code open the workbook select ALT+F11 and then double click on ThisWorkbook and "Module 1" to see the code in each location
Book1.xlsm
0
 

Author Closing Comment

by:Steve_Brady
ID: 34208598
Main question answered in "Best Solution"
0
 

Author Comment

by:Steve_Brady
ID: 34208714
FYI, some follow-up posts to this thread can be found here:

Update Excel VBA-based time display even when workbook is inactive
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26632255.html
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

739 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