0

I have one "release" button which: assigns numbers to specific cells, counts numbers, creates PDF documents, makes history stamps and so on.

I need to automatically generate buttons based on number value with different macros.

For example:

If target cell value = 4, then create 4 buttons with assigned macro 1, 2, 3, 4.

If value = 5, then create 5 buttons and assign macro 1, 2, 3, 4, 5 and so on.

Every assigned macro will be different. (Max count of buttons should be 20.)

3
  • 1
    Can you supply your attempted code please. Commented Dec 12, 2016 at 17:34
  • Hm, that is problem I don´t know where to start. Maybe it could be VBA form where buttons are generated? I don´t realy know. I am VBA begginer. imgur.com/3yGxByg Commented Dec 12, 2016 at 17:55
  • Take a look at my answer. This should do the trick. Commented Dec 12, 2016 at 17:56

2 Answers 2

1

Within your sheet, you should create the 20 buttons. From here assign them to their macro of equal value, and hide each one. From here you can loop to enable visibility for your current target cell value. In this case A1.

Dim MyVal As Long
MyVal = Range("A1").Value

For i = 1 To MyVal
    ActiveSheet.Buttons("Button " + CStr(i)).Visible = True
Next i
Sign up to request clarification or add additional context in comments.

10 Comments

Hm and where should I put this code? :) And how can I control which buttons want hide? I have multiple buttons on my sheet and don´t want hide everything. THx
@Nataniell in the upper left hand corner you can see the name of the button and can rename them their aswell. In the scenario I provided, you will need to name the 20 buttons you had in mind from "Button 1" through "Button 20" and assign them to each of your macros. This code goes within a Sub() and you can use another button to execute it if you'd like. You can control the amount of buttons shown by placing a value in A1.
I still trying but if i set number to A1 I have debug error : Object doesn´t support this property or method " ActiveSheet.Shapes("Button " + CStr(i)).Visable = True"
@Nataniell Change ".Shapes" to ".Buttons"
@Nataniell Are you sure all your buttons are named correctly? IE: Button 1. Button 2, Button 3, etc? Attempt calling each one individually like so; ActiveSheet.Buttons("Button 1").Visable = True
|
0

You'll have to sort the deleting etc, but something like this

Sub SortButtons()

Dim intButton As Integer
Dim cbNewButton As Button

Const intHeight = 30

For intButton = 1 To 4
    Set cbNewButton = ActiveSheet.Buttons.Add(224.25, (intButton * intHeight) + 20, 90.75, intHeight)
    cbNewButton.OnAction = "Macro" & intButton
    cbNewButton.Text = "Button for Macro " & intButton
    cbNewButton.Name = "OK_TO_DELETE_" & intButton
Next intButton

End Sub

2 Comments

That look nice, but i need control number of buttons by cell value. If A1 =4 then I have 4 buttons. If A1 = 3 then I have 3 buttons and each button have different macro. Look picture imgur.com/a/Ms86r
look at how brandons done the loop instigation, I think if you don't know how to tackle that, you should read a little first on what the suggested code is doing also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.