Consulting

Results 1 to 5 of 5

Thread: Grid of Buttons - Sizing and Positioning

  1. #1

    Grid of Buttons - Sizing and Positioning

    I would like to create a grid of buttons. I currently just do a copy/paste and manually position, but that will not work well for a large grid.

    Is there a way to center all of the buttons to the nearest cell? Is there a way to make them all the same size?

    If not, perhaps there is a way to precisely position the buttons (via VBA) and then line the cells up to them?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Create a table of positions and sizes on a worksheet and then use those values to set the actual buttons.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    If you want to lay buttons over one or more worksheet cells, this might work for you

    [vba]
    Option Explicit
    Sub drv()

    Call AddButtonOverCell(Range("D4"), "DummyMacro", "D4")
    Call AddButtonOverCell(Range("G6"), "DummyMacro", "G6")
    Call AddButtonOverCell(Range("L10"), "DummyMacro", "L10")
    Application.CutCopyMode = False
    End Sub

    Sub AddButtonOverCell(ButtonCell As Range, ButtonMacro As String, ButtonCaption As String)
    With ButtonCell.Parent
    On Error Resume Next
    .Buttons(ButtonCaption).Delete
    On Error GoTo 0

    .Buttons.Add( _
    ButtonCell.Cells(1, 1).Left, ButtonCell.Cells(1, 1).Top, _
    ButtonCell.Cells(1, 1).Width, ButtonCell.Cells(1, 1).Height).Name = ButtonCaption
    .Buttons(ButtonCaption).Text = ButtonCaption
    .Buttons(ButtonCaption).OnAction = ButtonMacro
    End With
    End Sub

    Sub DummyMacro()
    MsgBox "Hello World"
    End Sub

    [/vba]

    I used .Cells(1,1), but you could relax that and allow a button to cover any number of cells, instead of just the first one

    An extension might be to resize the worksheet cells to a desired height and width (e.g. squares, etc.) firstand then apply the buttons


    Paul

  4. #4
    I also saw this code:

    [vba]Sub TextBox2Cell()
    With ActiveCell
    ActiveSheet.Shapes.AddTextbox _
    msoTextOrientationHorizontal, .Left, _
    .Top, .Width, .Height
    End With
    End Sub
    [/vba]
    It works pretty well by fitting a text box to the size of the cell (and then I can assign a macro to it - instead of using a button). But is there a way to make the cell slightly smaller (centered in cell) such that it looks like a action? As it is, they all run together and they just look like cells rather than actions.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub TextBox2Cell()
    With ActiveCell
    ActiveSheet.Shapes.AddTextbox _
    msoTextOrientationHorizontal, .Left + 2, _
    .Top + 2, .Width - 4, .Height - 4
    With .Offset(1, 0)
    ActiveSheet.Shapes.AddTextbox _
    msoTextOrientationHorizontal, .Left + 2, _
    .Top + 2, .Width - 4, .Height - 4
    End With
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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