检查私有子

时间:2016-07-23 13:51:38

标签: excel vba return-value

在Excel 2007工作簿中,我有三个Excel模块,每个模块包含一个子例程。 Driver子(UpdateDataFromOracle)调用子UpdateResponse和UpdateSched。代码工作正常,但我想检查"返回代码"每个被叫的潜艇。我只希望Driver子对用户可见,所以我在模块1和2中创建了子。 模块1私有子UpdateResponse 模块2私有子UpdateSched 第3单元Public Sub UpdateDataFromOracle

来自Driver sub

的代码
Sub UpdateDataFromOracle()
'DECLARE VARIABLES
Dim varSchedReturn as variant
'...
Call UpdateResponse
Call UpdateSched
'I Would like to insert the "return code" check here
End Sub

来自Called sub

的代码
Option Explicit
Private Sub UpdateResponse()
'DECLARE VARIABLES
'...
If Sheets(strTempSheet).UsedRange.Rows.Count > 10 Then
  UpdateResponse = 0
Else UpdateResponse = 90
End If
End Sub 

要拨打私人潜水员我不得不放弃"呼叫"并使用"

Application.Run "Module1.UpdateResponse"

但我无法弄清楚如何以这种方式获得返回代码。 我还制作了UpdateResponse和UpdateSched私有函数,但我仍然无法弄清楚如何获取返回代码。 当我创建UpdateResponse和UpdateSched公共函数时,我可以在被调用的subs的末尾使用一个语句,如:

Else UpdateResponse = 90

问题是如果我将函数保留为Public,则被调用的子例程对用户可见。

我的目标是只让用户看到“驱动程序”子,并且能够评估某种“返回代码”"来自Driver子中的被调用子。 谢谢你看这个。

3 个答案:

答案 0 :(得分:1)

我没有完全阅读这个问题,但是将它们更改为功能

Private Function UpdateResponse() As Integer
    'DECLARE VARIABLES
    '...
    If Sheets(strTempSheet).UsedRange.Rows.Count > 10 Then
        UpdateResponse = 0
    Else 
        UpdateResponse = 90
    End If
End Function

然后:

Dim response         ' As Variant or Integer 
response = Application.Run("Module1.UpdateResponse")

此外,还有2种更好的方法Option Private Module或模块1中的公共变量

3 Ways to Call a Private Sub from Another Module

答案 1 :(得分:0)

@DougGlancy给出的答案效果很好。它被列为对我原始问题的评论,因此我添加此答案以表明他的答案是正确的。

答案 2 :(得分:-1)

我使用的Windows Excel的一个选项是在用户的计算机上设置可在以后的过程中检索的值。 https://msdn.microsoft.com/en-us/library/z46c489x(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

备选方案可以是设置Excel工作簿的某些属性,也可以在Excel中创建与其关联的名称和值。 (命名范围的一部分)。

最后,您可以在Windows注册表中添加和更改值。