Samadhi Ghosh
Samadhi Ghosh

Reputation: 31

How to determine in a sub procedure from which case it was called?

How can I put an if-else statement in a sub-procedure (say Sub C) so that I know which case (from the parent sub) has called sub C?

I do not know of it is possible or not as I am still learning VBA!

Actual Code:

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

Upvotes: 0

Views: 454

Answers (1)

user11509084
user11509084

Reputation:

OK, I think this is what you are asking

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

You need to pass an argument which can be fixed or variable into the Sub. So b 1 says run the Sub or Function called b and send the argument 1.

The Sub b expects this argument and outputs it as a message box.

In your case you might use

MainAppenderSub userResponse

or

Call MainAppenderSub(userResponse)

(Which are exactly the same as each other)

How this would work with your code

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

Upvotes: 1

Related Questions