Solved

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

Posted on 2010-11-20
11
490 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
11 Comments
 
LVL 20

Assisted Solution

by:pari123
pari123 earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 23

Accepted Solution

by:
Michael74 earned 400 total points
Comment Utility
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
Comment Utility
here is a test file
Book1.xlsm
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 59

Expert Comment

by:Chris Bottomley
Comment Utility
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
Comment Utility
*****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
Comment Utility



    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
Comment Utility
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
Comment Utility
Main question answered in "Best Solution"
0
 

Author Comment

by:Steve_Brady
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now