+ Reply to Thread
Results 1 to 3 of 3

Passing Variables Between Procedures

  1. #1
    Arturo
    Guest

    Passing Variables Between Procedures

    I have a work book with six sheets
    Sheet1, Sheet2. Sheet3
    Sheet1UL, Sheet2UL. Sheet3UL

    The flowing code works as it should for the first sheet in the array passed
    to the second procedure “ClearDestination�. When it jumps back up to the
    first procedure “ArrayLoop�, sheetVar(i) holds the next sheet in the array
    correctly but when it moves back to “ClearDestination�, (sheetVar(i) & "UL")
    still holds Sheet1UL. For the life of me I cannot figure out why it dose not
    change to Sheet2UL.

    ARRRRRR!

    Appreciatively,
    Arturo


    Public NumCopies As Long
    Public sheetVar As Variant

    Sub ArrayLoop()
    sheetVar = Array("Sheet1", "Sheet2", "Sheet3")
    For i = LBound(sheetVar) To UBound(sheetVar)
    Set sh = Worksheets(sheetVar(i))
    Sheets(sheetVar(i)).Select
    ClearDestination
    Next
    End Sub

    Sub ClearDestination()
    Dim myRange2 As Range

    Set myRange2 = Sheets((sheetVar(i) & "UL")).Range("A2:N65000")
    myRange2.ClearContents
    End Sub

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Arturo,

    Delcare sheetVar as a public varaint in the General Declarations Section of your code. To retain the parameter array size you must redimension your variant to it' size.
    I have placed the change in your code below.

    Public sheetVar As Variant

    Sub ArrayLoop()
    ReDim sheetVar(2)
    sheetVar = Array("Sheet1", "Sheet2", "Sheet3")
    For i = LBound(sheetVar) To UBound(sheetVar)
    Set sh = Worksheets(sheetVar(i))
    Sheets(sheetVar(i)).Select
    ClearDestination
    Next
    End Sub

    Now the sheet names will be passed in sheetVar.

    Hope this helps,
    Leith Ross
    Last edited by Leith Ross; 02-28-2005 at 09:41 PM.

  3. #3
    Shawn
    Guest

    RE: Passing Variables Between Procedures

    "Arturo" wrote:

    > The flowing code works as it should for the first sheet in the array passed
    > to the second procedure “ClearDestination�. When it jumps back up to the
    > first procedure “ArrayLoop�, sheetVar(i) holds the next sheet in the array
    > correctly but when it moves back to “ClearDestination�, (sheetVar(i) & "UL")
    > still holds Sheet1UL. For the life of me I cannot figure out why it dose not
    > change to Sheet2UL.


    It's occasions like these that convince you to go into the preferences and
    require "Option Explicit" in all your modules. In the Visual Basic Editor,

    Tools -> Options... -> Editor tab -> Require Variable Declaration

    That will also protect you from typos in variable names.

    The problem you're having has to do with what's in the variable i when you
    get to the ClearDestination sub.

    You might want to re-do the ClearDestination sub so that it requires an
    argument or two. That would be better than throwing everything into the
    global namespace.

    --Shawn

+ 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