+ Reply to Thread
Results 1 to 3 of 3

Positioning multiple command buttons on a userform using VBA

Hybrid View

newbi004 Positioning multiple command... 11-04-2012, 05:39 AM
HSV Re: Positioning multiple... 11-04-2012, 12:31 PM
newbi004 Re: Positioning multiple... 11-04-2012, 04:13 PM
  1. #1
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Positioning multiple command buttons on a userform using VBA

    Hi

    I have been searching the web for days for my answer!

    I have 10 command buttons, cmd1, cmd2, cmd3 etc on a user form. These are positioned one below the other. When I click any of these buttons, say cmd3, then I want cmd3 to be placed at the top and another set of command buttons are placed below this one in a verticle placement. So in example, with cmd3 first then below it are a series of new buttons, cmd3_1, cmd3_2 etc.

    I think an array is the answer but I can't fathom how to do it using vba.

    Can anyone in this forum help?

    Thanks

  2. #2
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    860

    Re: Positioning multiple command buttons on a userform using VBA

    This might help you on your way.
    Play something.

    Option Explicit
    Public com1left, com1top, com1width, com1height, com2left, com2top, com2width, com2height
    Private Sub CommandButton1_Click()
     CommandButton1.Move com1left, 10, com1width, com1height
     CommandButton2.Move com2height, com2left, com2top, com2width
     CommandButton2.WordWrap = True
    End Sub
    
    Private Sub CommandButton2_Click()
     CommandButton2.Move com2left, 10, com2width, com2height
     CommandButton1.Move com2height, com2left, com2top, com2width
     CommandButton1.WordWrap = True
    End Sub
    
    Private Sub UserForm_Activate() 
    'setting position of each Commandbutton
    
    com1left = CommandButton1.Left
        com1top = CommandButton1.Top
        com1width = CommandButton1.Width
        com1height = CommandButton1.Height
        
        com2left = CommandButton2.Left
        com2top = CommandButton2.Top
        com2width = CommandButton2.Width
        com2height = CommandButton2.Height
    End Sub
    Attached Files Attached Files
    Harry.

  3. #3
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Positioning multiple command buttons on a userform using VBA

    I knew this forum would come good! Thanks for taking the time Harry. This will get me what I need to do.

    As I have many buttons, and the .Top property will vary by 40 each time (i.e. cmd1.Top=100, cmd2.Top=140, cmd3.Top=180 etc), I assumed that an array would have been required for these values.

    Your code is a much neater way. It's shown me a new coding terminology.

    Cheers

+ 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