+ Reply to Thread
Results 1 to 8 of 8

For Macro - Specify Userform Workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    10-22-2007
    Posts
    37

    For Macro - Specify Userform Workbook

    Hi there,

    I have several versions of a workbook I am working with, so I have made several copies of the same .XLS file and renamed them. They all have the same Userforms in them, and I have a button on my worksheets that runs a macro "UserForm1.Show"

    It works, but it tries to open the UserForm1 from the original .XLS file instead of using the UserForm1 from the version of the .XLS file I already have open. How can I specify a path / force the macro to open the UserForm1 from my current open workbook without having to load the original?

    Example: My first XLS is called EntryMaster, and one of my other versions is EntryVersionA. When I try to open my UserForm1 from EntryVersionA, it attempts to open the UserForm1 from EntryMaster instead...

    I've tried something like this in the macro:
    Excel.ActiveWorkbook.UserForm1.Show
    but this is the wrong syntax for this. Anyone have any idea to stop this behaviour? Thanks!
    Last edited by rokuk; 11-26-2007 at 10:48 PM.

  2. #2
    Forum Contributor Gabor's Avatar
    Join Date
    02-15-2004
    Location
    Székesfehérvár, Hungary
    MS-Off Ver
    xl2016
    Posts
    226
    It should work with either a CommandButton1, with the macro on the worksheet module:

    Private Sub CommandButton1_Click()
    UserForm1.Show
    End Sub
    or with a macro on a normal module assigned to an object, e.g a Textbox :

    Private Sub LaunchMyUserForm1()
    UserForm1.Show
    End Sub
    With a Private Sub it should attempt to open the UF from the workbook.

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Your line of code should open the userform from the workbook containing it. I think you need to attach the workbook for someone to check.
    Hope that helps.

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

    Free DataBaseForm example

  4. #4
    Registered User
    Join Date
    10-22-2007
    Posts
    37

    update

    Ahh... I've found the problem, but haven't found a way around it.

    The macro is fine, but on the custom menu ("Assign Macro" dialog box) I have it launching the macro from:
    'C:\Documents and Settings\Desktop\EntryMaster.xls'!OpenAddUpdateForm
    instead of from EntryVersionA.xls.


    In order to assign the correct workbook (.XLS) I could manually change this to say:
    'C:\Documents and Settings\Desktop\EntryVersionA.xls'!OpenAddUpdateForm
    but this would require me to do this manually for each workbook.


    Is there a way to change this to say something like "use the macro OpenAddUpdateForm from the current workbook" instead of having to specify the .XLS file? In VBA terms, something that might work akin to:
    Excel.ActiveWorkbook!OpenAddUpdateForm
    that would allow for a dynamic .XLS filename?



    Ideally each file would be able to work independent from any other files. I just realized this may be related to a question I had before but I didn't get a working answer to. I think someone had mentioned using a method requiring a secondary file containing personal / custom macros that would need to tag along with the workbook, but I'd like to keep everything confined to one file if at all possible, as the .XLS files will be circulated to people who are not technical and telling them anything other than "just open YourVersion.XLS" file may be starting to stretch things for them.


    Thanks for the feedback so far
    Last edited by rokuk; 11-27-2007 at 02:44 PM.

  5. #5
    Forum Contributor Gabor's Avatar
    Join Date
    02-15-2004
    Location
    Székesfehérvár, Hungary
    MS-Off Ver
    xl2016
    Posts
    226
    Not the best way to launch the macro.
    Do not use custom menu bar button !
    Use an object, e.g. a TextBox on the worksheet and assign the macro to this object.
    Then it will launch the userform from the actual workbook.
    By this method all your workbooks will be independently operational at every user without any tricks on the customized menu or in personal.xls.
    The other way is to use a CommandButton, which is a so called activecontrol, as I wrote before.
    Gabor

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Create an addin containing all the code and userform

+ 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