+ Reply to Thread
Results 1 to 10 of 10

Storing VB Code in a variable withing a Macro

  1. #1
    Registered User
    Join Date
    08-04-2008
    Location
    Ohio
    MS-Off Ver
    2003
    Posts
    14

    Storing VB Code in a variable withing a Macro

    I'm hoping this can be done in VB. I have piece of code that I'm trying to make dynamic. I want to store parts of an IF statement in a variable and use them when I need them. Here's a little sample of what I'm trying to do
    Please Login or Register  to view this content.
    Can this be done? I get an error stating Type Mismatch. Does the code need to be stored in a different type of variable?

    If someone could help me, I'd greatly appreciate it.

    Thanks
    Last edited by royUK; 12-31-2008 at 11:37 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    I have added them this time!
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    You will need to use VBComponet. Be sure to add the reference as I have commented.
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    08-04-2008
    Location
    Ohio
    MS-Off Ver
    2003
    Posts
    14

    Storing VB Code in a variable within a Macro

    Thanks, this seems to work alright during my testing. Is it possible though to make it create the code in the same module as the calling code? (i.e. I have my code that calls this code in a FORM, so is it possible to create the new Sub in the current module or Form that it's being called from?

    Any help would be greatly appreciated.

  5. #5
    Registered User
    Join Date
    08-04-2008
    Location
    Ohio
    MS-Off Ver
    2003
    Posts
    14
    I got the code to create in my form/module, I changed you line


    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    But now I get an error stating that it can't find the macro.

    The macro is being built at the top of the Form just to note.

    Any ideas how I can get around this?

    Thanks again for all the help.

  6. #6
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    Userform code is Private which may be the cause.

    A sample xls might help us help you.

  7. #7
    Registered User
    Join Date
    08-04-2008
    Location
    Ohio
    MS-Off Ver
    2003
    Posts
    14
    ok, this only happens when the code is built in the form code. For some reason it can't find the macro that was created in the form code. Any reason why this won't work in the form code, looks like it works great in a module but I can't access the form variables and such from out side the form it seems.

  8. #8
    Registered User
    Join Date
    08-04-2008
    Location
    Ohio
    MS-Off Ver
    2003
    Posts
    14
    If I can call the code that was provided in this thread from within the form, why wouldn't I be able to call the macro that is within the form as well?

    I'll see if I can create an xls of what I'm trying to do, it may take some time though.

    Thanks again for all your help.

  9. #9
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    You can access the Userform objects in Modules. The userform has to be open of course.

    e.g.
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    08-04-2008
    Location
    Ohio
    MS-Off Ver
    2003
    Posts
    14
    Thank you so much, I built the new macro in a seperate module and adjusted my code a little,appending the form name to the variables being used and this solved the issue.

    Thanks again!

+ 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