1

I have a problem when I try to use a variable created in a subroutine in another subroutine. In the following example, I create variables Power_Origine, Power_Destination, ... in the Sub part0, I can use one of them in the Sub part1, but I can't use more than 1 variable in a single subroutine (part2).

Sub part0()

Power_Origine = 1
Description_Origine = 2
KnownUser_Origine = 3
Power_Destination = 1
Description_Destination = 2
KnownUser_Destination = 3


part1 (Power_Destination)
part2 (Power_Origine, Power_Destination, Description_Destination, KnownUser_Destination)
'part2 Power_Origine, Power_Destination, Description_Destination, KnownUser_Destination ' This does not work either

End Sub


Sub part1(Power_Destination As Integer)
MsgBox Power_Destination
End Sub


Sub part2(Power_Origine As Integer, Power_Destination As Integer, Description_Destination As Integer, KnownUser_Destination As Integer)
MsgBox "Hello " & Power_Destination & Description_Destination
End Sub

How can I call these 4 variables (Power_Origine, Power_Destination, Description_Destination, KnownUser_Destination) in the subroutine part2 ?

Thank you.

0

1 Answer 1

1

but I can't use more than 1 variable in a single subroutine (part2)

You can. Just remove ( and ). So it becomes

part2 Power_Origine, Power_Destination, Description_Destination, KnownUser_Destination 

or use

Call part2(Power_Origine, Power_Destination, Description_Destination, KnownUser_Destination)

Which is the same as above. You may still get an error because you have declared Power_Origine As Integer, Power_Destination As Integer, Description_Destination As Integer, KnownUser_Destination As Integer in part02 but in part0 they are Variants. Declare them as Integer in part0 and it will work :)

Sub part0()
    Dim Power_Origine As Integer
    Dim Description_Origine  As Integer
    Dim KnownUser_Origine  As Integer
    Dim Power_Destination  As Integer
    Dim Description_Destination As Integer
    Dim KnownUser_Destination  As Integer

    Power_Origine = 1
    Description_Origine = 2
    KnownUser_Origine = 3
    Power_Destination = 1
    Description_Destination = 2
    KnownUser_Destination = 3

    part1 Power_Destination
    part2 Power_Origine, Power_Destination, Description_Destination, KnownUser_Destination
End Sub

Sub part1(Power_Destination As Integer)
    MsgBox Power_Destination
End Sub

Sub part2(P_Orig As Integer, P_Dest As Integer, D_Dest As Integer, K_Dest As Integer)
    MsgBox "Hello " & P_Dest & D_Dest
End Sub

You may also want to read up on Passing Variables By Reference And By Value

Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I was looking for ! Thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.