MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 14th, 2002, 12:21 PM   #1
davidtrickett
New Member
 
Join Date: Apr 2002
Posts: 33
Default

I have a workbook with an open event macro which in a simple form goes like this:

Private Sub Workbook_Open()
ActiveCell.Offset(5, 0).Select
ActiveCell.Value = "anything"
End Sub

This of course works fine when the workbook is opened manually, but when it is opened by a macro in another workbook the cursor movement is ignored but it still cheerfully enters the value in the active cell!

I have tested this in several workbooks and for other event macros - e.g. Workbook_Activate - with the same result.

In a rather more complex macro it does everything it is supposed to do, but stubbornly refuses to move the cursor - ie in the above example
Activecell.Offset(5,0).Value="anything"
works.

Does anyone have any explanation for this weird behaviour, and better still any idea as to how I can persuade it to behave?

Thanks
davidtrickett is offline   Reply With Quote
Old May 14th, 2002, 01:04 PM   #2
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

Cells aren't active under this scenario, they're selected, but dormant. Persuasion:


Private Sub Workbook_Open()
Selection.Offset(5, 0).Select
ActiveCell.Value = "anything"
End Sub


This will move the entire selection in the event that multiple cells are selection. The following will limit the offset to a single cell selection:


Private Sub Workbook_Open()
Selection.Activate
ActiveCell.Offset(5, 0).Select
ActiveCell.Value = "anything"
End Sub


Hope this helps.

_________________
Cheers, NateO

[ This Message was edited by: NateO on 2002-05-14 12:05 ]
NateO is offline   Reply With Quote
Old May 15th, 2002, 03:22 AM   #3
davidtrickett
New Member
 
Join Date: Apr 2002
Posts: 33
Default

Many thanks for your help (again!), but I still can't persuade it to move the cursor - in both of your examples it resolutely sticks to A1! I am of course storing the macro in "This workbook" - I assume that this is the only place for it.

Any other ideas, or am I stuck with it?

Thanks
davidtrickett is offline   Reply With Quote
Old May 15th, 2002, 03:36 AM   #4
Colo
MrExcel MVP
 
Colo's Avatar
 
Join Date: Mar 2002
Location: Kobe, Japan
Posts: 1,420
Default

Hi, an easy way is...
Using "Auto_Open" on standerd module instead of "Private Sub Workbook_Open"

Here is a sample code.


'Standerd modele of Book1.xls
Sub Auto_Open()
ActiveCell.Offset(5, 0).Select
ActiveCell.Value = "anything"
End Sub

'Standerd modele of Book2.xls
Sub OpenAnotheBook()
Dim strPath As String
'Need change following path.
strPath = "C:Documents and SettingssomewhereBook1.xls"
Workbooks.Open(FileName:=strPath).RunAutoMacros Which:=xlAutoOpen
End Sub

__________________
Regards,

Masaru Kaji aka Colo - Ex Microsoft MVP Since 2004 -2009
Colo is offline   Reply With Quote
Old May 15th, 2002, 06:49 AM   #5
Vas
Board Regular
 
Join Date: May 2002
Location: Gothenburg, Sweden
Posts: 74
Default

I have the same problem, but with other operations. I can't open a workbook in the Workbook_Open event, when opening via VBA.

That sounded weird... OK, this is what I mean
1. I open a workbook via VBA code.
2. That workbook opens another Workbook in Workbook_Open.
3. It doesn't work.

What's more, it changes some of the links I have in the second workbook. I usually don't open workbooks via VBA anymore. I use Hyperlinks instead.
__________________
/Niklas Jansson
Vas is offline   Reply With Quote
Old May 15th, 2002, 06:10 PM   #6
Colo
MrExcel MVP
 
Colo's Avatar
 
Join Date: Mar 2002
Location: Kobe, Japan
Posts: 1,420
Default

Hi Vas,
I guess, maybe you are Microsoft Excel 97 user.
In it, there are two methods you can use to run a macro automatically when you open a workbook.
But Excel 97 have a problem with above.
The case function procedure execute or workbook is opened via VBA, open event may not be performed.

This is a reason I recommended "Sub Auto_Open" to davidtrickett.

I've known about it on MS Knowledge Base Japan. Sorry I could not find English site.
__________________
Regards,

Masaru Kaji aka Colo - Ex Microsoft MVP Since 2004 -2009
Colo is offline   Reply With Quote
Old May 15th, 2002, 06:40 PM   #7
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

Colo, don't mean to be argumentative, but I thought it was the other way, where auto macros, if launched via vba, won't fire unless you add a command. Nice add-in incidentally!

Dave, I am using 2000, where it works (I'll test in '97 tonight). You want to open the workbook in a normal module, if you use the workbook_open event, you must have it in the 'ThisWorkbook' module. The code I posted earlier worked for me, but let's define the range and see if that helps. In the file to be opened, in the 'ThisWorkbook' module:


Private Sub workbook_open()
Dim n As Range
Set n = Range(Selection.Address)
n.Offset(5).Value = "anything"
n.Offset(5).Select
End Sub


Make sure your events are enabled. In a normal module in any workbook, if the above fails, try:


sub st()
application.enableevents = true
end sub


If this does not work we can try an auto macro, like Colo mentioned. In a normal module in the target file place:


Private Sub auto_open()
Dim n As Range
Set n = Range(Selection.Address)
n.Offset(5).Value = "anything"
n.Offset(5).Select
End Sub


And in the file that you're opening the code with, do:


Workbooks.Open ("Whatever you have here")
ActiveWorkbook.RunAutoMacros xlAutoOpen

 

Hopefully one of these two methods gets you on track.

_________________
Cheers, NateO

[ This Message was edited by: NateO on 2002-05-15 19:18 ]
NateO is offline   Reply With Quote
Old May 15th, 2002, 08:11 PM   #8
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Quote:
On 2002-05-15 02:36, Colo wrote:
Hi, an easy way is...
Using "Auto_Open" on standerd module instead of "Private Sub Workbook_Open"

Here is a sample code.


'Standerd modele of Book1.xls
Sub Auto_Open()
ActiveCell.Offset(5, 0).Select
ActiveCell.Value = "anything"
End Sub

'Standerd modele of Book2.xls
Sub OpenAnotheBook()
Dim strPath As String
'Need change following path.
strPath = "C:Documents and SettingssomewhereBook1.xls"
Workbooks.Open(FileName:=strPath).RunAutoMacros Which:=xlAutoOpen
End Sub

I'm glad you suggested Auto_Open Colo. I asked if people used in versions of Excel 97 or above a few weeks ago. I could think of a few reasons I would use it, but was curious if anyway else still used this.
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old May 16th, 2002, 02:50 AM   #9
davidtrickett
New Member
 
Join Date: Apr 2002
Posts: 33
Default

NateO and all

Many thanks for your further help. I did of course know about the "Auto_open" option - I was just trying to be clever (and lazy - less code to write) by using the Workbook_open event - the situation is that I have a "Control" workbook which opens other workbooks selected by the user and runs their macros. My workround incidentally is to forget about automation and simply use the

Workbooks("any").Open
Application.Run('"any'!initialisemacro")

option. It just seems weird that if the workbook is opened by another macro everything - and some of my procedures are pretty complex - works except cursor movement (and as I have now found out thanks to Niklas opening another workbook). Another MS "undocumented feature"?
davidtrickett is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 09:37 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes