Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 从另一个函数添加函数\Sub_Vba_Excel - Fatal编程技术网

Vba 从另一个函数添加函数\Sub

Vba 从另一个函数添加函数\Sub,vba,excel,Vba,Excel,我的问题可能很愚蠢。但我想知道这是否可能以及如何实现? 我可以从一个函数\sub创建另一个子函数 例如: Sub first() x = 10 if x = 10 Then 'Add Another Sub based on the Condition in another module. Sub Second() End Sub End If End Sub 我真正的问题是,我试图根据某些条件在循环中添加新的工作表。如果我能够

我的问题可能很愚蠢。但我想知道这是否可能以及如何实现? 我可以从一个函数\sub创建另一个子函数

例如:

Sub first()
    x = 10
    if x = 10 Then 
      'Add Another Sub based on the Condition in another module.
       Sub Second()
       End Sub
    End If
End Sub
我真正的问题是,我试图根据某些条件在循环中添加新的工作表。如果我能够添加工作表,那么我需要在另一个工作表中创建一些按钮,以及这些按钮的功能。
请对此提供帮助。

默认情况下,我的方法是:

Sub first()
    x = 10
    if x = 10 Then 
      'Call Another Sub based on the Condition.
       Call Second()
    End If
End Sub

Sub Second()
' do stuff
End Sub

如果您正在创建新的工作表并添加带有代码的按钮,则应完成所有代码、按钮设计等。任何定制都将通过参数进行。

默认情况下,我的方法是:

Sub first()
    x = 10
    if x = 10 Then 
      'Call Another Sub based on the Condition.
       Call Second()
    End If
End Sub

Sub Second()
' do stuff
End Sub
如果您正在创建新的工作表并添加带有代码的按钮,则应完成所有代码、按钮设计等。任何定制都是通过参数进行的。

这更多是关于代码的设计。首先,你不能把一个潜艇放在另一个潜艇里,不幸的是,这不是它的工作原理

您可以使用范围条件来控制代码流,并确保某些部分仅在需要时执行


最简单也是最推荐的方法是只编写另一个子组件,但仅在满足特定条件时调用它

Public Sub Main_Sub()

Dim x As Integer

x = 11 '// Change x to 5 and the other sub won't run

If x > 10 Then
    Other_Sub
    '// Code execution will resume here once 'Other_Sub' has finished
    MsgBox "Other Sub complete!"
End If

End Sub

Private Sub Other_Sub()

    MsgBox "You're in the other sub now!"

End Sub

另一种方法是,如果您的条件不满足,只需退出sub,尽管这更多地用于错误处理而不是条件执行:

Public Sub Main_Sub()

Dim x As Integer

x = 11 '// Change x to 5 and the other sub won't run

If x > 10 Then
    Other_Sub
    '// Code execution will resume here once 'Other_Sub' has finished
    MsgBox "Other Sub complete!"
Else
    Exit Sub
End If

MsgBox "You will only see this message if x was greater than 10!"

End Sub

Private Sub Other_Sub()

    MsgBox "You're in the other sub now!"

End Sub

最后一种方法是使用标签,尽管这是一种可行的解决方案,但我建议不要使用标签,因为它严重影响代码的可读性和逻辑性:

Public Sub Main_Sub()

Dim x As Integer

x = 11 '// Change x to 5 and the other sub won't run

If x < 11 Then GoTo Skip_Other_Sub:

Other_Sub

Skip_Other_Sub:

MsgBox "You will see this regardless of the other sub being run!"

End Sub

Private Sub Other_Sub()

    MsgBox "You're in the other sub now!"

End Sub
Public Sub-Main_-Sub()
作为整数的Dim x
x=11'//将x更改为5,其他子系统将不运行
如果x<11,则转到跳过其他子项:
其他小组
跳过其他子项:
MsgBox“无论运行其他子系统,您都会看到此信息!”
端接头
私人分包其他分包()
MsgBox“你现在在另一艘潜艇上!”
端接头

这里的要点是,最好编写所有可能需要或不需要的代码,并有条件地调用它,而不是尝试“动态”创建代码这充其量是混乱的,需要对VBE进行编程访问,如果您对所做的事情没有100%的信心,这会使您的机器容易受到攻击。

这更多地是关于代码的设计。首先,你不能把一个潜艇放在另一个潜艇里,不幸的是,这不是它的工作原理

您可以使用范围条件来控制代码流,并确保某些部分仅在需要时执行


最简单也是最推荐的方法是只编写另一个子组件,但仅在满足特定条件时调用它

Public Sub Main_Sub()

Dim x As Integer

x = 11 '// Change x to 5 and the other sub won't run

If x > 10 Then
    Other_Sub
    '// Code execution will resume here once 'Other_Sub' has finished
    MsgBox "Other Sub complete!"
End If

End Sub

Private Sub Other_Sub()

    MsgBox "You're in the other sub now!"

End Sub

另一种方法是,如果您的条件不满足,只需退出sub,尽管这更多地用于错误处理而不是条件执行:

Public Sub Main_Sub()

Dim x As Integer

x = 11 '// Change x to 5 and the other sub won't run

If x > 10 Then
    Other_Sub
    '// Code execution will resume here once 'Other_Sub' has finished
    MsgBox "Other Sub complete!"
Else
    Exit Sub
End If

MsgBox "You will only see this message if x was greater than 10!"

End Sub

Private Sub Other_Sub()

    MsgBox "You're in the other sub now!"

End Sub

最后一种方法是使用标签,尽管这是一种可行的解决方案,但我建议不要使用标签,因为它严重影响代码的可读性和逻辑性:

Public Sub Main_Sub()

Dim x As Integer

x = 11 '// Change x to 5 and the other sub won't run

If x < 11 Then GoTo Skip_Other_Sub:

Other_Sub

Skip_Other_Sub:

MsgBox "You will see this regardless of the other sub being run!"

End Sub

Private Sub Other_Sub()

    MsgBox "You're in the other sub now!"

End Sub
Public Sub-Main_-Sub()
作为整数的Dim x
x=11'//将x更改为5,其他子系统将不运行
如果x<11,则转到跳过其他子项:
其他小组
跳过其他子项:
MsgBox“无论运行其他子系统,您都会看到此信息!”
端接头
私人分包其他分包()
MsgBox“你现在在另一艘潜艇上!”
端接头


这里的要点是,最好编写所有可能需要或不需要的代码,并有条件地调用它,而不是尝试“动态”创建代码这充其量是混乱的,需要对VBE进行编程访问,如果您对所做的事情没有100%的信心,这可能会使您的机器容易受到攻击。

请参见此处,了解如何通过VBE编程在VBA中添加代码,这很有趣,但如果可能的话,我会避免。如果按钮功能在设计时已经知道,您可以对其进行编码,并使用完全限定的引用(见下文)将其分配给每个按钮操作属性,因为即使在不同的工作簿中也可以调用sub。有两种方法:要么在“目标”工作表(按钮所在的位置)中添加对“基本”工作簿(函数所在的位置)的引用,要么使用
Run
方法(`Application.Run“'basemacrosheet.xlsm'!MacroName”)。前者不需要在运行时打开“基本”工作簿,而后者则需要。请参阅此处,了解如何通过VBE在VBA中以编程方式添加代码,这很有趣,但如果可能的话,我会避免使用它。如果按钮功能在设计时已经知道,您可以对其进行编码,并使用完全限定的引用(见下文)将其分配给每个按钮操作属性,因为即使在不同的工作簿中也可以调用sub。有两种方法:要么在“目标”工作表(按钮所在的位置)中添加对“基本”工作簿(函数所在的位置)的引用,要么使用
Run
方法(`Application.Run“'basemacrosheet.xlsm'!MacroName”)。前者不需要在运行时打开“基本”工作簿,而后者则需要。