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 Mar 18th, 2003, 05:47 PM   #1
mcgew
 
Join Date: Sep 2002
Location: Bristol
Posts: 71
Default Worksheet object code

I've written some code in ms excel objects, for a worksheet, and want to call code for another objects worksheet at the end. I've tried

Sub Sheet1()
(code)
Call Sheet2

End Sub
................
Sub Sheet2()
(code)
End Sub

but when i try to run it it says there's an expected function or variable. it works when i use modules but for this code i need it in the worksheet.
any suggestions?
mcgew is offline   Reply With Quote
Old Mar 20th, 2003, 07:00 AM   #2
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 3,863
Default worksheet object code

Hi mcgew,

I don't believe the scope rules in Excel permit calling a procedure that is within a worksheet's event code module from any other module. The way around this is to put the common code you want to apparently use from both modules in a standard macro module (as you mentioned). So your example would look like:

Sub Sheet1()
(code)
Call CommonCode

End Sub
................
Sub Sheet2()
Call CommonCode
End Sub

----------------
'inside standard macro module

Sub CommonCode()
.
.
End Sub


So Sub Sheet1 can be called from events within sheet1's event code module, Sub Sheet 2 can be called from events within sheet2's event code module, and the two can share the common code.
__________________
Keep Excelling.

Damon

VBAexpert Excel Consulting
(My other lives: http://members.tripod.com/playitagaindamon ,
http://community.webshots.com/user/piadamon )
Damon Ostrander is offline   Reply With Quote
Old Mar 20th, 2003, 06:01 PM   #3
mcgew
 
Join Date: Sep 2002
Location: Bristol
Posts: 71
Default

Thanks for your help.

Grace :D
mcgew 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 +1. The time now is 10:49 PM.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.