VBA: Get Spin Button Name

zero269

Active Member
Joined
Jan 16, 2023
Messages
301
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm looking for a way to get the name of a Form Controls Spin Button on a Worksheet, not in a Form.
I'm working on a macro that builds a calendar, and I'm having to delete the button manually each time I want to clear everything and test my code.

1736368957988.png

I'm trying to avoid deleting all Objects or all Spin Buttons (only 1 in my case) for a givent Sheet.

Thank you and best regards,
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
This code will give you the names of all the controls, but I don't how to help beyond that. If you give that spinner the same name each time, you could delete it very easily


VBA Code:
Sub GetNameOfSpinners()
  Dim Cntrl As Shape
  Dim Sht As Worksheet
  
  Set Sht = ActiveSheet
  
  For Each Cntrl In Sht.Shapes
    Debug.Print Cntrl.FormControlType; Cntrl.Name
    
  Next Cntrl
End Sub

XlFormControl enumeration​

xlButtonControl0Button.
xlCheckBox1Check box.
xlDropDown2Combo box.
xlEditBox3Text box.
xlGroupBox4Group box.
xlLabel5Label.
xlListBox6List box.
xlOptionButton7Option button.
xlScrollBar8Scroll bar.
xlSpinner9Spinner.
[th]
Name​
[/th][th]
Value​
[/th][th]
Description​
[/th]​
 
Upvote 1
Solution
Hello @Jeffrey Mahoney
If you give that spinner the same name each time, you could delete it very easily
If only I knew how to create one and name it, I'd be in good shape. :)I'm currently just using the macro I recorded when I manually inserted it. I couldn't figure out how to modify the recorded code, and there's nothing in there that shows the name; i.e. "Spinner 1" and so on.

I had to record a macro of me deleting it to see what I got...
VBA Code:
 ActiveSheet.Shapes.Range(Array("Spinner 5")).Select
 Selection.Delete
I was able to use your code to extract the name and delete it:
VBA Code:
Sub Spin_Button_Delete()
  Dim Cntrl As Shape, sName As String
  For Each Cntrl In wsCalendar.Shapes
    sName = Cntrl.Name  'Get Object Name
    wsCalendar.Shapes(sName).Delete 'Delete Object
  Next Cntrl
End Sub
Looping it works just fine in my case. It's only ever one Spin Button. However, in future personal projects, I'll need to figure out how to create them in a more useful way.

Thank you and best regards,

...
If curious, here's my recorded macro:
VBA Code:
Sub Spin_Button_New()
  'Spin Button (Cell AF3)
  Range("AF3").Select
  wsCalendar.Spinners.Add(580.2, 41.4, 11.4, 21.6).Select
    With Selection
      .Placement = xlMove
      .PrintObject = False
    End With
    With Selection
      .Value = Year(Date)
      .Min = 0
      .Max = 2050
      .SmallChange = 1
      .LinkedCell = "$AA$3"
      .Display3DShading = True
    End With
    Range("AF3").Select 'Deselect Spin Button
End Sub
 
Upvote 0
The macro recorder won't necessarly show you the name given to the newly added spin button. You can however give it a name upon creation with VBA via the Spinner Name Property.

Here is a sanitized version of the code output by the macro recorder:
VBA Code:
Sub Spin_Button_New()
    Dim oSpin As Spinner
    Set oSpin = wsCalendar.Spinners.Add(580.2, 41.4, 11.4, 21.6)
    With oSpin
        .Name = "Choose_A_Spinner_Name_Here"  '<=== Choose_A_Spinner_Name_Here
        .Placement = xlMove
        .PrintObject = False
        .Value = Year(Date)
        .Min = 0
        .Max = 2050
        .SmallChange = 1
        .LinkedCell = "$AA$3"
        .Display3DShading = True
    End With
End Sub

Now that you have named the spinner , it is easy to directly delete it without iterating all the controls:
VBA Code:
Sub DeleteSpinButton()
    wsCalendar.Spinners("Choose_A_Spinner_Name_Here").Delete
End Sub
 
Upvote 0
The macro recorder won't necessarly show you the name given to the newly added spin button. You can however give it a name upon creation with VBA via the Spinner Name Property.

Here is a sanitized version of the code output by the macro recorder:
VBA Code:
Sub Spin_Button_New()
    Dim oSpin As Spinner
    Set oSpin = wsCalendar.Spinners.Add(580.2, 41.4, 11.4, 21.6)
    With oSpin
        .Name = "Choose_A_Spinner_Name_Here"  '<=== Choose_A_Spinner_Name_Here
        .Placement = xlMove
        .PrintObject = False
        .Value = Year(Date)
        .Min = 0
        .Max = 2050
        .SmallChange = 1
        .LinkedCell = "$AA$3"
        .Display3DShading = True
    End With
End Sub

Now that you have named the spinner , it is easy to directly delete it without iterating all the controls:
VBA Code:
Sub DeleteSpinButton()
    wsCalendar.Spinners("Choose_A_Spinner_Name_Here").Delete
End Sub
Hello Jaafar,

This was soooo super helpful. You didn't even give me a chance to ask this question... :)

So, I decided to post the question anyways and link to this post. However, perhaps you can copy your post to that thread so I can mark that other thread as the Solution...?

It's here: VBA: Create a Form Controls Spin Button

Thank you and best regards,
 
Upvote 0
Glad I could answer your question and thanks for the feedback.(y)
So, I decided to post the question anyways and link to this post. However, perhaps you can copy your post to that thread so I can mark that other thread as the Solution...?
I see you already posted a link to this thread in your new thread but I will link my answer there as well if you wish.

Regards.
 
Upvote 0

Forum statistics

Threads
1,225,637
Messages
6,186,135
Members
453,340
Latest member
Stu61

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top