How do you GoTo a line label in a different object

2019-07-25 21:14发布

E.g. Given Sheet 1 contains:

Ref: Do things

How can I direct a code in Module 1 to GoTo Ref? If I were in the Sheet1 code moduke then I could simply use a Goto Ref

But this doesn't work across different modules

标签: excel vba
2条回答
该账号已被封号
2楼-- · 2019-07-25 21:54

Your question is not clear and you didn't provide any code, so this is a guess.

GoTo is used to jump to different locations within the same sub/function. You cannot use it to jump to parts of other sub routines or functions, which it sounds like you might be trying to do.

Also, "NapDone:" is not called a reference, it's formally called a line label. :)

查看更多
Summer. ? 凉城
3楼-- · 2019-07-25 22:01

To help expand on the other answers.. Like they said you shouldn't use GoTo for anything in VBA except error handling.

What you should be doing is calling a public sub/function from another module. For example in Module 1 you would have the following

Sub TestMod1()
Dim MyNumber As Integer

MyNumber = GetSquare(6)
'MyNumber returns from the function with a value of 36
End Sub

and on Module 2 you have

Public Function GetSquare(ByVal MyNumber As Integer)

GetSquare = MyNumber * MyNumber

End Function

So now you know how to avoid it. GoTo is not very good programming practice as you'll have things flying all over the place. Try to break down code you're repeating into multiple Subs and just call them when needed, or functions whatever be the case. Then you'll get into classes, which are just wrapped up to represent an object and it'll do all the work for that object.

This should get you on the right track.

查看更多
登录 后发表回答