+ Reply to Thread
Results 1 to 7 of 7

Can't get variables to pass from one procedure to same in the same module

  1. #1
    Registered User
    Join Date
    12-10-2014
    Location
    Orion, Michigan
    MS-Off Ver
    2003
    Posts
    3

    Can't get variables to pass from one procedure to same in the same module

    The code I have works great as separate procedures. I decided to combine the 2 procedures as the code is mostly the same with a few differences in which cells are copied to the array to save to different log sheets. Each Procedure has a Inputbox to get the year of interest (2014, 2015, 2016....) that will be used to open AND in the event the Log does not exist Save a new one from a master using the year code in the file name. Based on a cell value I am doing an If_Then_Else to determine to run one procedure or both. If I run them separately as individual procedures they work fine. When I combined them I decided to call one procedure (SaveToLog_Audit) from inside (SaveToLog_Replc). I removed the Date input from (SaveToLog_Audit) letting (SaveToLog_Replc) get that date code and pass it along. I don't want to have (SaveToLog_Audit) ask for a date a second time as it run since I basically want it to be inobtrusive (no need to ask twice when I can pass 'Ans' to it.

    I have tried setting public Ans and used Explicit at the beginning with both procedures in same module --> didn't work. I tried Dim Ans As String after option Explicit --> Didn't work. I tried Public Ans as String --> didn't work. Pretty much I couldn't get it to pass the variable 'Ans" no matter what I tried. I'm still pretty new to coding VBA so any help would be great. Showing the basic code, only the parts that have issues. I apologize in advance if I didn't tag this correctly.

    Please Login or Register  to view this content.
    Last edited by valhalla_33; 05-20-2015 at 10:59 PM.

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Can't get variables to pass from one procedure to same in the same module

    kyle has already provided the solution but here's a link for further information on how to pass variables between subroutines

    http://www.cpearson.com/excel/byrefbyval.aspx
    Last edited by humdingaling; 05-21-2015 at 07:42 AM. Reason: OP complied with request
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Can't get variables to pass from one procedure to same in the same module

    I haven't really read your code, but as a starter, to pass values, you need parameters (don't use public variables):

    Firstly delete
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Should be:
    Please Login or Register  to view this content.
    It's then called in the other procedure:
    Please Login or Register  to view this content.
    Right now your Locally scoped variables are overriding you public, but you shouldn't be using public anyway and passing parameters will remove the issue

  4. #4
    Registered User
    Join Date
    12-10-2014
    Location
    Orion, Michigan
    MS-Off Ver
    2003
    Posts
    3

    Re: Can't get variables to pass from one procedure to same in the same module

    I commented out the Dim Ans in both procedures. I found that Dim Ans in both reset the public variable and caused it to fail. Removing the Dim Ans from both allowed the variable to pass unimpeded. Yay! Problem solved.

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Can't get variables to pass from one procedure to same in the same module

    Yes, that's what I said. I also said that it's wrong

  6. #6
    Registered User
    Join Date
    12-10-2014
    Location
    Orion, Michigan
    MS-Off Ver
    2003
    Posts
    3

    Re: Can't get variables to pass from one procedure to same in the same module

    If I declare it the way you suggest, can the procedures then be placed in separate modules again? I prefer to keep each procedure in it's own module when I can. The exception to that is when I generate a UserForm and that code needs to be in the same place for the buttons and check boxes. Do I add the Ans to the () in SaveToLog_Audit name when I declare the sub or just in the call to that Sub?

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Can't get variables to pass from one procedure to same in the same module

    Both, as I gave you in the example. Declaring it tells the sub that it requires a parameter, then you pass it the param when you call it.

    Yes, they can go in different modules

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How can I call a Module or a sub procedure and run it automatically in current module?
    By qzqzjcjp in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-29-2014, 11:48 AM
  2. [SOLVED] How to use second procedure (the 2nd Sub) results in first procedure in the same module
    By lubbamkt in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-11-2014, 10:17 AM
  3. [SOLVED] Pass date variable to another procedure/module
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-20-2013, 10:16 PM
  4. [SOLVED] Pass variables from userform to module
    By Bishonen in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-08-2012, 07:54 AM
  5. [SOLVED] Pass variables from Worksheet_Calculate sub to Module
    By John Michl in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-22-2006, 11:15 AM

Tags for this Thread

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