vba 如何转到不同对象中的线标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7535561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:05:58  来源:igfitidea点击:

How do you GoTo a line label in a different object?

excelvba

提问by Felix

E.g. Given Sheet 1 contains:

例如,给定的工作表 1 包含:

Ref: Do things

Ref: Do things

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

如何将模块 1 中的代码定向到GoTo Ref?如果我在Sheet1代码模式中,那么我可以简单地使用 Goto Ref

But this doesn't work across different modules

但这不适用于不同的模块

回答by aevanko

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

你的问题不清楚,你没有提供任何代码,所以这是一个猜测。

GoTois 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.

GoTo用于跳转到同一子/功能内的不同位置。您不能使用它跳转到其他子例程或函数的部分,这听起来您可能正在尝试这样做。

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

此外,"NapDone:" 不称为参考,它正式称为line label。:)

回答by TheFuzzyGiggler

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

为了帮助扩展其他答案..就像他们说的那样,除了错误处理之外,您不应该在 VBA 中使用 GoTo 进行任何操作。

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

您应该做的是从另一个模块调用公共子/函数。例如,在模块 1 中,您将拥有以下内容

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

在模块 2 上你有

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.

所以现在你知道如何避免它了。GoTo 不是很好的编程实践,因为你会到处乱飞。尝试将您重复的代码分解为多个 Sub 并在需要时调用它们,或者在任何情况下运行。然后你将进入类,这些类只是被包装起来代表一个对象,它会为那个对象做所有的工作。

This should get you on the right track.

这应该让你走上正轨。