Consulting

Results 1 to 10 of 10

Thread: Add Active X Command Button to sheet and keep Userform Open

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location

    Add Active X Command Button to sheet and keep Userform Open

    Hello, is there a way to create an active x command button on a worksheet while keeping a userform open?
    When I use the following code to add the button on the worksheet, it closes the userform and I have to reload it manually.

    Set myb = ActiveSheet.OLEObjects.Add(classtype:="Forms.CommandButton.1", link:=False, displayasicon:=False) 
            'set button properties
            With myb
                .Name = "CommandButton3"
                .Object.Caption = "Remove " & ActiveSheet.Name
                .Object.Font.Bold = True
                .Object.Font.Size = 11
                .Object.WordWrap = True
                .Top = 42.75
                .Left = 675
                .Width = 72
                .Height = 40
                .Shadow = True
                .Placement = xlMoveAndSize
                .PrintObject = False
            End With
    By the way, the userform is Modeless.

    Thanks
    Gary
    Last edited by zoom38; 04-27-2016 at 08:56 AM.

  2. #2
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Copying and pasting an existing button also closes the userform.
     ActiveSheet.Shapes.Range(Array("CommandButton2")).Select
             Selection.Copy
             ActiveSheet.Paste

  3. #3
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    I have tried activating the code below from just about everywhere to reload the userform but it doesn't work. I can only get it to reload the userform from worksheet events such as Selection_Change, Activate or Deactivate but that takes a click one way or another to load the userform.

    Sub ShowUserform1()
       If UserForm1.Visible = False Then
          UserForm1.Show False
       End If
    End Sub
    Demo5.xlsm
    Anyone have an idea what is happening? I've uploaded an example. Try it from sheet 201.

    Gary
    Last edited by zoom38; 04-27-2016 at 01:26 PM.

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    You have a lot going on there so it is hard to see your problem as it errors on Open.

    I recommend that you always use Option Explicit as the first line of code in objects like Modules and UserForms.

    Consider rethinking your Protect/UnProtect scheme. When using Protect, be sure to use the option UserInterfaceOnly:=True. This lets your code modify things without UnProtect/Protect. That alone will likely help. I recommend protecting all sheets that way and then Unprotect the others as needed.

  5. #5
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Ken, it doesn't error on open for me but I streamlined the workbook even more and used option explicit throughout and unprotected only the active sheet in my code. However the userform still closes or unloads upon the creation of a button when a new sheet is added using my addsheet sub. I'm assuming that excel automatically puts it in Developer mode but I still cant get the userform to automatically reload after the new sheet is created without clicking somewhere relative to an event. I'm attaching a more streamlined workbook should you get a chance to look at it again. I tried unloading the userform before the sheet is added and then reloading but that didn't work. I think next I'm going to try having my code add a forms control command button rather than the active x command button but I'd be surprised if that changes anything.

    Demo5.xlsm


    Thanks
    Gary

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    It still had an error on open. This was because you used Call Userform1.Show. Do not use Call. Remove those Call words. Call something is really only needed if you want to use () around the parameters for something.

    Also, remove or comment out your Application.ScreenUpdating=False.

    Once I did those, it did copy the sheet and with Userform1 still showing. Getting late so I would have to look at this more tomorrow. I did not see that the command button was added but maybe it was and my eyes are just too tired.

  7. #7
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Interesting that I don't get that error on open. I took out the call keywords and commented out the Application.ScreenUpdating=False but I still can't get the userform to stay open when I add a new sheet by clicking on the add 201 button on sheet 201. As a workaround I changed the code to add a forms control button instead of the active x button and it seemed to work. Tomorrow i'll have to learn how to code those buttons instead of the active x buttons that I've been using and am used to.

  8. #8
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Since a Microsoft patch screwed up ActiveX some time back, I normally use Form controls now. I used to prefer ActiveX but stability is more important to me.

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Why don't you add the ActiveX control in 'design mode' ? property .Visible=False

    The only thing you have to do in the userform is making the activex control visible: commandbutton3.visible=true

  10. #10
    When you add any activeX control, your VBA project gets reset (similar to pressing the "End" toolbar button in the VBE). There is no workaround for this problem other than these:
    1. make sure the ActiveX control already is on the sheet (as snb points out)
    2. Make sure the running code is in a different VBAProject (an add-in for instance)
    3. Don't use ActiveX controls to begin with (most recommended!)
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •