将3个值中的1个传递给Sub [英] Passing 1 of 3 values to a Sub
问题描述
我想更新Sub
中的单个(3个中的1个)变量.
I want to update a single (1 of 3) variable in a Sub
.
已经分配了3个变量,但是我想更新其中一个变量.
The 3 variables have already been assigned, but I would like to update one of the variables.
有关在不同Macros
在Macro1中
Sub Sub1()
Text1 = "First Text"
Text2 = "Second Text"
Text3 = "Third Text"
Call Macro2.Sub2(Text1, Text2, Text3)
End Sub
在Macro2中
Public PassedText1, PassedText2, PassedText3 As String
Sub Sub2(ByVal r1 As String, ByVal r2 As String, ByVal r3 As String)
PassedText1 = r1
PassedText2 = r2
PassedText3 = r3
End Sub
'=======================
我知道我可以在Macro2
中创建另一个子,然后可以在其中传递string
并将其分配给Public变量.
I know I can create another sub in Macro2
where I can then pass a string
and then assign it to the Public variable.
如果没有其他方法?
我虽然喜欢:
Sub Sub3()
SomeText = "New Text"
Call Sub2(r1:=SomeText)
End Sub
推荐答案
如果您以此方式定义子项目,则可以使用您提出的内容:
If you define your sub like this, you can use what you proposed :
Sub Sub2(Optional ByVal r1 As Variant, Optional ByVal r2 As Variant, Optional ByVal r3 As Variant)
因此,每个参数Optional ByVal r1 As Variant
都有2处更改:
So, 2 changed things for each argument Optional ByVal r1 As Variant
:
-
Optional
不言而喻
As Variant
,因为您需要测试使用IsMissing()
方法在调用中是否存在该参数,但是为此,该参数需要定义为Variant
As Variant
because you'll need to test is the argument is present in your call with the IsMissing()
method, but for that the argument need to be defined as a Variant
对于proc中的3个参数,如下所示:
So like this for the 3 arguments in your proc :
If Not IsMissing(r1) Then PassedText1 = r1
这篇关于将3个值中的1个传递给Sub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!