1

for my question I would like to create a button beside the cell that is not NULL or "". The caption for the button must follow the value in the cell beside.

For example:

  1. I typed 'EMPLOYEE' in Range("D3")
  2. I want the macro to create a button named "EMPLOYEE" in the Range("C3")
  3. However I want the macro to be dynamic so that every time I type in the value in the column 'D', the cell on the left side - C3 will comes out a button.

Therefore, I've figured out that I needed to code for the CommandButton manually is that right?

Nevertheless, million thanks in advance for all.

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
4 Leave Cover
  • 1,248
  • 12
  • 40
  • 83
  • First ... [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) Second ... see the `Worksheet_Change` event and the `Macro Recorder` while doing what you want manually. – Scott Holtzman Dec 21 '12 at 16:50
  • First, I did add the button into the ActiveSheet but got no clue on the 'Selection.OnAction = ' because the macro is a sub in my module. – 4 Leave Cover Dec 21 '12 at 16:55
  • What do you want the button to do? – Alex P Dec 21 '12 at 16:56
  • `Selection.OnAction = ` would be if you wanted to click the button and perform a macro, which seems different than the question you asked. You don't need any button to create buttons... unless I am confused. – Scott Holtzman Dec 21 '12 at 16:58
  • Yes indeed sorry for the confusion. I wanted the worksheet_change() to create CommandButton for me under certain condition. – 4 Leave Cover Dec 21 '12 at 16:59
  • Why don't you post the code you've tried? Then we can help you edit it... Also, `Selection.OnAction` follows this syntax `Selection.OnAction = "Workbookname!Macroname"` – Scott Holtzman Dec 21 '12 at 17:01
  • possible duplicate of [How to add a button programmatically in VBA next to some sheet cell data?](http://stackoverflow.com/questions/4559094/how-to-add-a-button-programmatically-in-vba-next-to-some-sheet-cell-data) – Siddharth Rout Dec 21 '12 at 18:44
  • @Remnant I want the button, when clicked, pop out a UserForm for data entry (I already done coding for the UserForm, just left the part mentioned in my question above) – 4 Leave Cover Dec 22 '12 at 03:18
  • @ScottHoltzman I wanted to create CommandButton. Ain't 'Selection.OnAction' is for normal button? – 4 Leave Cover Dec 22 '12 at 03:19
  • @SiddharthRout Yes I've seen that thread but I wanted a CommandButton not a normal button (CommandButton looks better) – 4 Leave Cover Dec 22 '12 at 03:19

2 Answers2

3

You may record a macro by adding a command button to see how it's created and then incorporate the fancy parts. Note on properties of OLE Command button object, pay more attention to them.

e.g. theButton.Name yet for caption is set via theButton.Object.Caption etc.

Here is a code snippet to get you going:-

Option Explicit

Sub createButtons()
Dim theButton As OLEObject
Dim rngRange As Range
Dim i As Integer

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set rngRange = Sheets(2).Range("B2")

    For i = 0 To 9
        If rngRange.Offset(i, 0).Value <> "" Then
        With rngRange.Offset(i, 1)
            Set theButton = ActiveSheet.OLEObjects.Add _
                (ClassType:="Forms.CommandButton.1", _
                Left:=.Left, _
                Top:=.Top, _
                Height:=.Height, _
                Width:=.Width)

                theButton.Name = "cmd" & rngRange.Offset(i, 0).Value
                theButton.Object.Caption = rngRange.Offset(i, 0).Value

                '-- you may edit other properties such as word wrap, font etc..
      End With
      End If
    Next i

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

Output:

enter image description here

bonCodigo
  • 14,268
  • 1
  • 48
  • 91
  • @Tan Siong Zhe please give this a try. Imagine how you would want to add a picture/image, rename it, and then position it in a cell :) – bonCodigo Dec 21 '12 at 19:50
  • Thanks @bonCodigo Am trying now – 4 Leave Cover Dec 22 '12 at 03:22
  • Referring to your code, I set every 'Sheets(2)' to 'ActiveSheet'. There is one weird issue where it successfully creates a CommandButton but at the same time pop out "Run time error '438'" Object doesn't support this property or method. Any idea how the message exist? – 4 Leave Cover Dec 22 '12 at 03:31
  • @TanSiongZhe I was supposed to update this code. Error was due to `theButton.Object.Name` where as it should be `theButton.Name` :) Check now :) comment please. – bonCodigo Dec 22 '12 at 05:07
  • Please check again your code. The coordinate of the buttons are off. Thank you. I'm working on it in the moment too. – 4 Leave Cover Dec 22 '12 at 09:10
  • The error was on this line 'With rngRange.Offset(i, i)' should change to 'With rngRange.Offset(i, 1)' – 4 Leave Cover Dec 22 '12 at 09:24
  • Excellent, that was typo happened while formatting here. I was wondering how there's an error when it output correct results. Please mark if all is good for you and happy to hear any further comments :-) – bonCodigo Dec 22 '12 at 10:06
  • I've edited little in your code and it works perfectly for me. Thank you so much. I also marked yours as the answer. Have a nice day :) – 4 Leave Cover Dec 24 '12 at 03:34
  • @TanSiongZhe I would have given you the exact code, but I believe we do justice to the OP by encouraging him to try out with a proper direction we provide. Because it's more fun when you see finally your own code running up :) Merry Christmas! – bonCodigo Dec 24 '12 at 14:00
2

Try this out.

Public Sub Worksheet_Change(ByVal Target As Range)
    Dim col As Integer
    Dim row As Integer

    col = Target.Column
    row = Target.row

    If Not IsNull(Target.Value) And Not IsEmpty(Target.Value) Then
        Application.EnableEvents = False
        Buttons.Add Cells(row, col - 1).Left, Cells(row, col - 1).Top, Cells(row, col - 1).Width, Cells(row, col - 1).Height
        Application.EnableEvents = True
    End If
    End Sub

Open up the Developer Tab --> Visual Basic, double click "Sheet1", then paste this code in there. Test it by typing text in a cell on Sheet1 then moving away from that cell (e.g. by pressing Enter).

StoriKnow
  • 5,738
  • 6
  • 37
  • 46
  • @TanSiongZhe I apologize, I'm just noticing that you specifically want a commandbutton, not a regular button. The above logic would remain the same, but you'll have to replace "Buttons.Add" with the appropriate code for [adding a commandbutton](http://www.vbaexpress.com/forum/showthread.php?t=8907) – StoriKnow Dec 22 '12 at 03:33
  • Don't be :) Thanks for the link. Checking it out now. – 4 Leave Cover Dec 22 '12 at 08:54