remembering the nth item in one sub routine in the next sub routin

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a macro which has one sub routine running another sub routine, however
I want the n number I defined in the first sub routine to be rembered in the
next sub routine.

ie
Dim Asset(1 To 19) As String 'Camilla EDITED coding
Asset(1) = "Eurostoxx 50"
Asset(2) = "Nasdaq 100"

Dim n As Integer
For n = 1 To 19
Worksheets(Asset(n)).Activate
CloseDay MyDay, MyMonth, MyYear
Next n

then in the next sub routine I am trying to switch between workbooks and
worksheets
ie Workbooks("Trade Files").Worksheets(Asset(n)).Activate

but I need it to remember the the (Asset(n)) from the previous sub routine.

Can anyone help?
 
If you want a variable to be available (global) to several Subs, then declare
it above all the subs in the module.
 
Even when I define

Dim n As Integer
Dim Asset(1 To 19) As String
Asset(1) = "Eurostoxx 50"
Asset(2) = "Nasdaq 100"

At the beginning of the sub routine, the macro still get stuck on the coding:


Workbooks("Trade Files").Worksheets(Asset(n)).Activate

do you know why?
 
sorry I forgot to say I don't want the variables defined for all sub routines
just the two that are relevant.
 
Also if I do define Asset(1) , Asset(2) at the top of the all subs in the
procedure it says it is an invalid procedure.
 
I have decided that it is best to define it globally but I have defined it
using another letter k so that all other n's in other subroutines are not
affected.
Thanks gary!
 
Why not just pass the variables to the second routine?
Sub Macro1()
Dim n As Integer
Dim Asset(1 To 19) As String
Asset(1) = "Eurostoxx 50"
Asset(2) = "Nasdaq 100"
'other stuff here
Macro2 Asset(1), Asset(2), n
End Sub
Sub Macro2(myAsset1 as string, myAsset2 as string, myN as integer)
'your stuff
End Sub

James

Sub Macro1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top