-1

I have a simple Function Test() in Module1 and a Sub SubName() in Module2. I'm trying to call SubName() from Function Test(). When I type:

Call Module2.SubName 

and exit the raw, I'm getting an error:

Compile Error: Expected: identifier

When I just type Module2 and put Stop, it offers me the name of the SubName in Module2. So it sees it, but looks like is missing something in syntax. I can't figure out what.

Erik A
  • 31,639
  • 12
  • 42
  • 67
Iouri
  • 1
  • 1
  • 1
  • 1
  • [This related question](http://stackoverflow.com/q/2695198/2065121) may have some useful information. – Roger Rowland Mar 26 '15 at 05:38
  • I tested your code and couldn't reproduce your issue. The code worked as expected. See this for guidance: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – Jean-François Corbett Mar 26 '15 at 08:25
  • 1
    OP, do you use any of those Names (i.e. *Test, Module2, SubName*) anywhere else in your code? – kolcinx Mar 26 '15 at 08:34

2 Answers2

0

Some related links :

  1. MSDN-Calling Sub and Function Procedures
  2. SO-Calling a Sub in VBA
  3. SO-What does the Call keyword do in VB6?

Solution to your question :

Either use Call SubName or just SubName.


Testing:

After some testing, I've been able to break the code. The error is NOT the same you get, but maybe this will bring us closer to solution.

Picture from VBA: Picture from VBA

Community
  • 1
  • 1
kolcinx
  • 2,183
  • 1
  • 15
  • 38
  • Why do you think the `Module2.` qualification is the cause of the error? Were you able to reproduce the error? I sure wasn't. The OP's code as posted works fine. – Jean-François Corbett Mar 26 '15 at 08:22
  • Good question. Made me test the problem from OP and it works for me. So now I don't think the `Module2.` is the problem. I wrongly jumped to the conclusion. – kolcinx Mar 26 '15 at 08:30
  • @Jean-FrançoisCorbett, do you think that the problem can be caused by wrong usage of the variable names? – kolcinx Mar 26 '15 at 08:47
  • See the picture I added, to understand what I mean. – kolcinx Mar 26 '15 at 08:53
  • True - not an answer, yet. I did leave a comment below his post and after he replies, I may be able to provide an answer. It's a work in progress. – kolcinx Mar 26 '15 at 12:49
  • Guys, thank you all. It really works just by using a sub name, even if it is in another Module. Though, if there are two Sub-s with the same name (one in the current module and one in another one), it will execute the one in it's own module. So it won't go to another module. If I create 2 external modules with the same name of Sub it will return an error "...Ambiguous..". And this is fare. The question is: how can I specify the module from which I want it to execute the sub. If I put Module.Subname, it returns a syntax error ("Expected: line or label or statement or end of statement") – Iouri Mar 30 '15 at 02:22
0

The solution is simple: just use the statement

SubName

and it'll be called.

Few clarifying remarks:

  • No need to use the Call statement. If you do however, you need to use Call SubName()
  • If you call any sub with parameters, use SubName Param1 - or call Call SubName(Param1). Don't use Subname(Param1)- if param1 is an object, this will pass it's default property to the sub, not the object (e.g. Subname (Range("A1")) will pass the value of the cell to the sub - but not the Range object - Subname Range("A1") will pass the Range object.
  • You can also use Module2.SubName - however this is only required, if you have multiple SubName sub in different modules
Peter Albert
  • 16,917
  • 5
  • 64
  • 88
  • 1
    I just tried `Call Module2.SubName` myself and it worked as expected. Why do you think the brackets `()` or the `Call` or the `Module2.` qualification is the cause of the error? "*Don't use Subname(Param1)- as this will cast Param1 to a string!*" This is not true. Try it and convince yourself. – Jean-François Corbett Mar 26 '15 at 08:20
  • To help you convince yourself: call this `Sub asub(v): MsgBox TypeName(v): End Sub` like this `Sub test2(): asub (3.1415926): asub (True): asub (5): End Sub` Not strings. – Jean-François Corbett Mar 26 '15 at 08:34
  • @Jean-FrançoisCorbett: Good point, thanks! I clarified my answer - it is not casting to string - but rather passing the default property of an object. E.g. in your example, `asub Range("A1")` would be `Range`, but `asub (Range("A1"))` would be `string` or `double`, depending on the content of A1. – Peter Albert Mar 26 '15 at 22:23