Excel 在父对象中调用helper函数,而无需从helper中的父对象重新定义对象

Excel 在父对象中调用helper函数,而无需从helper中的父对象重新定义对象,excel,vba,Excel,Vba,我在Excel中使用VBA为我正在构建的表收集数据,我必须到TN3270仿真器那里才能得到它。为了使用emulator,我必须定义一些对象来完成这项工作。我还有几个助手函数,多个函数使用这些函数导航到emulator中的不同屏幕。到目前为止,为了使用它们,我不得不将对象定义复制到这些函数中,以使它们工作。这在大多数情况下都有效,但偶尔(以我无法预测的方式复制)当助手重新创建要使用的特定对象时,我会出错 Option Explicit Public Sub gather_data() Di

我在Excel中使用VBA为我正在构建的表收集数据,我必须到TN3270仿真器那里才能得到它。为了使用emulator,我必须定义一些对象来完成这项工作。我还有几个助手函数,多个函数使用这些函数导航到emulator中的不同屏幕。到目前为止,为了使用它们,我不得不将对象定义复制到这些函数中,以使它们工作。这在大多数情况下都有效,但偶尔(以我无法预测的方式复制)当助手重新创建要使用的特定对象时,我会出错

Option Explicit
Public Sub gather_data()
    Dim TN_Emulator As Object
    Dim Workbook As Object
    Set TN_Emulator = CreateObject("TN_Emulator.Program")
    Set Workbook = ActiveWorkbook
    Dim string_from_excel As String

    #for loop to go through table rows
        #put value in string_from_excel
        If string_from_excel = some condition
            go_to_screen_2
            #grab and put data back in excel
        Else
            go_to_screen_3
            #grab and put data back in excel
        End If
        go_to_screen_1
    #next loop logic
End Sub

Public Sub go_to_screen_1()
    Dim TN_Emulator As Object

    #the next step occasionally throws the error
    Set TN_Emulator = CreateObject("TN_Emulator.Program") 

    #send instructions to the emulator
End Sub

有没有一种方法可以导入现有对象(创建和使用时没有任何错误),而无需将其重新定义到helper函数中以避免此问题?我曾尝试在谷歌上搜索,但我认为我没有使用正确的搜索词。

首先感谢@JosephC和@Damian在评论中为我发布了答案


从JosephC的“你要寻找的关键词是:“如何向函数传递参数”。”,他提供了以下链接,描述了在函数调用中传递参数的两种不同方式

从达米安那里得到了我最关心的问题的解决方案。而不是声明和设置将在helper函数体中使用的对象。将对象名称和类型放在初始帮助器名称的括号中,从其他函数调用帮助器时也放在括号中,如下所示

Option Explicit
Public Sub gather_data()
    Dim TN_Emulator As Object
    Dim Workbook As Object
    Set TN_Emulator = CreateObject("TN_Emulator.Program")
    Set Workbook = ActiveWorkbook
    Dim string_from_excel As String

    #for loop to go through table rows
        #put value in string_from_excel
        If string_from_excel = some condition
            Call go_to_screen_2(TN_Emulator)
            #grab and put data back in excel
        Else
            Call go_to_screen_3(TN_Emulator)
            #grab and put data back in excel
        End If
        Call go_to_screen_1(TN_Emulator)
    #next loop logic
End Sub

Public Sub go_to_screen_1(TN_Emulator As Object)
   #send instructions to the emulator
End Sub
我相信我正确理解了说明,并成功地为自己测试了这一点。我还根据实际应用程序的需要,在helper函数定义和调用中传递了多个对象,每次传递的顺序相同

Sub go_to_screen_1(TN_Emulator As Object, ConnectionName As Object)


Public Sub go\u to_screen\u 1(TN\u仿真器作为对象)
然后:
Call go\u to_screen\u 1(TN\u仿真器)
因此,通过在helper函数的括号中传递对象定义,并在函数调用中传递本地指定的变量,我不需要在助手中包含设置对象的步骤?您要查找的关键词是:“如何将参数传递给函数”。另外,请快速阅读@Donald.H.Hartley您需要删除helper函数中的声明和设置说明,否则它将重新启动。JosephC,Damian,谢谢大家。我将在下面自我发布一个答案,显示解决方案以及ByRef vs ByVal链接。
Call go_to_screen_1(TN_Emulator, ConnectionName)