Cannot call subs in different sheets

Posted on 2014-01-06
Medium Priority
Last Modified: 2014-01-10
Hi All,

I am "self voting" this my most provincial question to date but I can't figure it out.  I have tried youtube and the usual places to get this kind of question asked.

I have a huge spreadsheet filled with VBA code from one end to the other.

In sheet19 I am trying to call a sub (not private) that is in sheet2.

My code goes something like this:

my sub()

'code here
'code here
'code here

call SubS2        'Sub from Sheet2 and it is not private so it should work

End Sub

So I know that I am doing something so silly that it hurts.

I have tried call sheet2.SubS2....and that does not work.

Any help is much appreciated.

Any cheap shots are surely warranted.   :)

thanks for your help!
Question by:BostonBob
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 1000 total points
ID: 39761311
Here is the syntax:

Application.Run "sheet2.SubS2"
LVL 54

Assisted Solution

Rgonzo1971 earned 1000 total points
ID: 39761317

You have to use the Sheet.CodeName

Maybe Sheet2.SubS2

LVL 15

Expert Comment

by:Berkson Wein
ID: 39762274
Attach the workbook.

Not sure why you'd have code attached to every sheet, but it should still work.  I prefer to put nearly everything in modules, but that's just a preference.

I also suggest prefixing public functions with public, just for readability.

If in sheet1 you have

public function DoThisFromSheetOne()
    msgbox "From sheet 1"
end function

Open in new window

and in sheet 2 you have
public function CallTheThingFromOne()
end function

Open in new window

if you fun CallTheThingFromOne, it should display "From Sheet 1" in a message box.

Test that out for us?

If that doesn't work, or if you need more help, attach a sample.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

624 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