+ Reply to Thread
Results 1 to 2 of 2

Passing Variables within For loops to other modules

  1. #1
    Registered User
    Join Date
    06-27-2005
    Posts
    3

    Passing Variables within For loops to other modules

    Hi.

    I has a hugh piece of code which was too big to compile, so I have have had to split it off into separate modules. I keep on getting subscript out of range errors now.


    Public I
    Public p

    Sub CreateMardakInput()

    Application.ScreenUpdating = True

    Sheets("AutoInput").Select
    Application.CutCopyMode = False
    ActiveWindow.SelectedSheets.Delete

    Sheets.Add.Name = "AutoInput"

    Sheets("AutoInput").Move Before:=Sheets(1)
    Sheets("AutoInput").Select

    ' Dim p As Integer
    p = 2

    For I = 1 To Sheets.Count
    Sheets(I).Select
    GoSub DoCopy
    Next I

    Exit Sub

    DoCopy:

    If Cells(1, 1) = "Sub-Contract Payment" Then ' If A1 = LO Template then:
    ' Test for name in box & link through if so
    ' for per subcontractor.
    'Module3.PartOne (p)
    'Module4.PartTwo (p)
    'Module5.PartThree (p)
    'Module6.PartFour (p)
    'Module7.PartFive (p)

    Call PartOne
    Call PartTwo
    Call PartThree
    Call PartFour
    Call PartFive

    End If

    Return

    End Sub


    For some reason the I variable is not picked up by the other modules. I have declared I & p as public in these modules. I am stuck.

    Please help!

    Thanks,
    Tom

  2. #2
    Dave Peterson
    Guest

    Re: Passing Variables within For loops to other modules

    If you declared I in this main module and also declared I in each of the other
    modules, then VBA will use the one closest to the code--within that same module.

    Try removing the declarations from the other modules (Just declare it public one
    time).

    tomjermy wrote:
    >
    > Hi.
    >
    > I has a hugh piece of code which was too big to compile, so I have have
    > had to split it off into separate modules. I keep on getting subscript
    > out of range errors now.
    >
    > Public I
    > Public p
    >
    > Sub CreateMardakInput()
    >
    > Application.ScreenUpdating = True
    >
    > Sheets("AutoInput").Select
    > Application.CutCopyMode = False
    > ActiveWindow.SelectedSheets.Delete
    >
    > Sheets.Add.Name = "AutoInput"
    >
    > Sheets("AutoInput").Move Before:=Sheets(1)
    > Sheets("AutoInput").Select
    >
    > ' Dim p As Integer
    > p = 2
    >
    > For I = 1 To Sheets.Count
    > Sheets(I).Select
    > GoSub DoCopy
    > Next I
    >
    > Exit Sub
    >
    > DoCopy:
    >
    > If Cells(1, 1) = "Sub-Contract Payment" Then ' If A1 = LO Template
    > then:
    > ' Test for name in box &
    > link through if so
    > ' for per subcontractor.
    > 'Module3.PartOne (p)
    > 'Module4.PartTwo (p)
    > 'Module5.PartThree (p)
    > 'Module6.PartFour (p)
    > 'Module7.PartFive (p)
    >
    > Call PartOne
    > Call PartTwo
    > Call PartThree
    > Call PartFour
    > Call PartFive
    >
    > End If
    >
    > Return
    >
    > End Sub
    >
    > For some reason the I variable is not picked up by the other modules. I
    > have declared I & p as public in these modules. I am stuck.
    >
    > Please help!
    >
    > Thanks,
    > Tom
    >
    > --
    > tomjermy
    > ------------------------------------------------------------------------
    > tomjermy's Profile: http://www.excelforum.com/member.php...o&userid=24666
    > View this thread: http://www.excelforum.com/showthread...hreadid=574197


    --

    Dave Peterson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1