+ Reply to Thread
Results 1 to 12 of 12

need help transferring sheet code to module

  1. #1
    Forum Contributor
    Join Date
    03-10-2017
    Location
    USA
    MS-Off Ver
    office 2016
    Posts
    393

    need help transferring sheet code to module

    I have this code i've been using in a sheet but want to put it in a module but I keep getting errors. Its having issues with the (ByVal ) statements after the sub so my ws and dest are not defined correctly. Not sure what it needs.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: need help transferring sheet code to module

    You might want to change the 'ByVal' to 'ByRef' if you are calling the procedure and supplying those values on the call. eg.
    Please Login or Register  to view this content.
    ByVal is used when VBA can automatically detect a value, such As the Selected cell, or ActiveSheet. ByRef is used when the user supplies the values. If you have the code in a public module, then VBA cannot detect the sheet nor the range on its own.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Forum Contributor
    Join Date
    03-10-2017
    Location
    USA
    MS-Off Ver
    office 2016
    Posts
    393

    Re: need help transferring sheet code to module

    ok that makes sense. Thanks. The code that calls on this is looking at several arrays so the range changes for each range processed, 6 in total. I tried to have ws and dest change with each array but couldnt figure it out. Here is the parent code.

    Please Login or Register  to view this content.
    I'm thinking i can set ws and dest to the value of e but not sure how to get the range of e. CStr(e(0)) gives me the sheet name but not the range.

    Sorry i didnt post this code before, probably helps to know whats going on better.
    Last edited by kevinu; 06-16-2018 at 01:38 PM.

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: need help transferring sheet code to module

    Where you call
    Please Login or Register  to view this content.
    it would equate to
    Please Login or Register  to view this content.
    I think you might be better off using two arrays, one for the names and one for the ranges. Say arrays e & f

    Then you could do something like
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    03-10-2017
    Location
    USA
    MS-Off Ver
    office 2016
    Posts
    393

    Re: need help transferring sheet code to module

    would that work in a module? and would i just set the ws or worksheet to e0 and the range to f0 so i can bypass the byval entry?

  6. #6
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: need help transferring sheet code to module

    Quote Originally Posted by kevinu View Post
    would that work in a module? and would i just set the ws or worksheet to e0 and the range to f0 so i can bypass the byval entry?
    It would work like you are trying to make it work now, but using ByRef in the called macro. The arrays would be like
    Please Login or Register  to view this content.
    Then the call would be
    Please Login or Register  to view this content.
    and would then make your values int the called macro equate to ws = Name1 and dest = Range("A1").
    Last edited by JLGWhiz; 06-17-2018 at 08:33 AM.

  7. #7
    Forum Contributor
    Join Date
    03-10-2017
    Location
    USA
    MS-Off Ver
    office 2016
    Posts
    393

    Re: need help transferring sheet code to module

    JLG, Im having trouble getting it to work. Can you show me how it should look in this code?

    Please Login or Register  to view this content.
    if this is the code in a sheet how should it look in a module?

  8. #8
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: need help transferring sheet code to module

    I don't believe my suggestion is going to work either. I don't feel like tackling it tonight, but I will try to straighten it out tomorrow. I am sure we can come up with a solution that will work better than the way it is. One of the main problems is trying to use two different data types on the same variable. That is a definite no-no.

  9. #9
    Forum Contributor
    Join Date
    03-10-2017
    Location
    USA
    MS-Off Ver
    office 2016
    Posts
    393

    Re: need help transferring sheet code to module

    no problem, im in no rush. Thanks for looking at it.

  10. #10
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: need help transferring sheet code to module

    Here is what is happening:
    Please Login or Register  to view this content.
    1. e becomes a sub-array of the main array in the For loop as a variant data type
    2. e(0) produces the String value of the sub-array first item.
    3. e(1) produces the cell value of the sub-array second item.
    4. test e(0), e(1) then equates to
    Please Login or Register  to view this content.
    5. The values will not be invoked into the test macro using ByVal syntax because because the array values are not selected.
    6. In the test macro both the 'ws' and 'dest' variables would need to be declared as Variant to avoid generating an error when called from the ComboBox_Change macro.
    7. The only place I see the 'dest' variable used in the 'test' macro is dest = ws, This appears to be putting the ws value in the dest cell. If That is all that it does, then that could be handled in the change macro and eliminated from the test macro altogether.
    8. If you eliminate the dest variable, then you can modify your For loop, using a single array for only the Strings and not the corresponding ranges.
    9. But you would need to add in a code segmenrt in the change macro to put compensate for the dest = ws statemtent that had been eliminated from the test macro.
    10. Your loop would then look like
    Please Login or Register  to view this content.
    Then your title line for the test macro
    Please Login or Register  to view this content.
    For repalcing the dest = ws you could use a slect case in the change macro like
    Please Login or Register  to view this content.
    There might be better ways of doing this, but I am trying to minimize the rewrite of your code.

  11. #11
    Forum Contributor
    Join Date
    03-10-2017
    Location
    USA
    MS-Off Ver
    office 2016
    Posts
    393

    Re: need help transferring sheet code to module

    ok i see. I just couldn't for the life of me figure out how to assign 'dest' the range. Looks like a case select can do that nicely. Thanks for your time and effort

  12. #12
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: need help transferring sheet code to module

    You're welcome,
    regards, JLG

+ 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. Change a vba code of a sheet to a module code
    By PaulaGon in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-21-2018, 10:42 AM
  2. Difference between code in module vs code in sheet ??
    By fairweather in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-22-2014, 03:05 PM
  3. [SOLVED] Code locks cells when inserted in sheet module but returns error in standard module
    By yoda66 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-07-2014, 07:39 AM
  4. How to copy sheet to new book without sheet module code?
    By MelinaT in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-09-2011, 11:52 AM
  5. how to access Sheet module, normal module, Worbook module to type code
    By alibaba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2009, 07:51 AM
  6. add code to sheet module
    By realniceguy5000 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-28-2009, 12:54 PM
  7. Passing variables and values between sheet code and module code
    By imux in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-14-2009, 12:23 PM

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