如何在子过程中确定从哪种情况调用它?

时间:2019-06-21 08:02:33

标签: excel vba

如何将if-else语句放在子过程(例如Sub C)中,以便使我知道哪种情况(来自父子)调用了sub C?

我仍在学习VBA,所以我不知道是否有可能!

实际代码:

Sub UserInput()
userResponse = InputBox(Prompt:="Please select the action you want to perform?" & vbNewLine & vbNewLine & "Enter 1 to sort test cases as per test-data-input" & vbNewLine & "Enter 2 to Rectify steps")

Select Case userResponse
    Case 1
        Call MainAppenderSub         
    Case 2
        Call MainAppenderSub                
End Select
End Sub

Sub MainAppenderSub()
    Some code
    appendCellContent (lreturn)    
End Sub

Sub appendCellContent(lreturn As Long)  

   'Some code
   'Here I want to call appender1 only if it has been called by Case 1 else call appeder2 if it has been called by Case 2

End Sub

Sub appender1(lreturn As Long)

Dim i As Long
For i = 1 To lreturn
    If  ActiveCell.Offset(0, 1) <> Empty Then
        ActiveCell.Formula = "=RC[2]"
    End If
        ActiveCell.Offset(1, 0).Activate
Next i

End Sub

Sub appender2(lreturn As Long)

Dim i As Long
For i = 1 To lreturn
    If  ActiveCell.Offset(0, 1) <> Empty Then
        ActiveCell.Value = "=RC[-1]&""-""&RC[2]"
    End If
        ActiveCell.Offset(1, 0).Activate
Next i

End Sub

1 个答案:

答案 0 :(得分:1)

好的,我想这就是你要问的

Sub a()
    b 1
    Dim c As Integer
    c = 5
    b c
End Sub

Sub b(arg As Integer)
    MsgBox arg '1 first time, then 5 second time
End Sub

您需要将可以固定或可变的参数传递给Sub。因此b 1运行名为b的Sub或Function并发送自变量1

Sub b需要此参数并将其输出为消息框。

您可以使用

MainAppenderSub userResponse

Call MainAppenderSub(userResponse)

(彼此完全相同)

这将如何与您的代码一起使用

Sub UserInput()
    userResponse = InputBox(Prompt:="Please select the action you want to perform?" & vbNewLine & vbNewLine & "Enter 1 to sort test cases as per test-data-input" & vbNewLine & "Enter 2 to Rectify steps")

    MainAppenderSub userResponse
End Sub

Sub MainAppenderSub(arg As Long)
    'Some code
    appendCellContent arg, lreturn 'where does lreturn come from
End Sub

Sub appendCellContent(arg As Long, anotherArg as Long)
   'Some code
   'Here I want to call appender1 only if it has been called by Case 1 else call appeder2 if it has been called by Case 2
    appender arg, anotherArg
End Sub

Sub appender(arg As Long, lreturn as Long)

    Dim i As Long
    For i = 1 To lreturn
        If ActiveCell.Offset(0, 1) <> Empty Then
            Select Case arg
                Case 1
                    ActiveCell.Formula = "=RC[2]"
                Case 2
                    ActiveCell.Value = "=RC[-1]&""-""&RC[2]"
            End Select
        End If
            ActiveCell.Offset(1, 0).Activate
    Next i

End Sub