VBA to change spin value?

612Guy

New Member
Joined
Aug 14, 2023
Messages
1
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Is it possible to change a spin value using VBA? I tried using the macro recorder but it does not record any code when I manipulate the control.
I would like to have VBA print the current sheet to PDF, name the file after the value in Letter!$O$1, and then loop through (press the up arrow/spin the control in cell Letter!$M$1) until at the max spin value. I suppose another way to handle it would be to have VBA change the cell d!$A$2 which houses all the data (screenshot 2). The spinner control in Letter!$M$1 is linked to the value in cell d!$A$2.

Appreciate any guidance.
 

Attachments

  • Screenshot 2023-08-14 224852.png
    Screenshot 2023-08-14 224852.png
    53.5 KB · Views: 27
  • Screenshot 2023-08-14 225044.png
    Screenshot 2023-08-14 225044.png
    34.2 KB · Views: 23

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
WRT the spinner, not sure what you're asking for. If you want a value in the cell why not just type it in and forget the spinner? If I want the cell to be a number, surely I have that number in mind and could just type it in? Or maybe you're asking about adjusting the min/max or step values, but I still think the spinner is a somewhat useless control most of the time. If I understood the vba comment, it still doesn't make sense to me to write code that will put 44 in a cell when I can just key it in.

Can't help much with the other parts - would have to google it and find some code examples so others will probably step in and provide that while I spend lotsa time finding out how. Happens to me all the time. 😖
 
Upvote 0
Here are two methods that hopefully do what you want. The first method loops through each spinner value, as per your request. The other loops through each record in your table. With either method though, you may need to pause/DoEvents, so I have included it in the solution. Note that I have assumed that the Spinner is taken from Form Controls, not ActiveX Controls. And I have also assumed the data in the sheet named "d" has been converted into a Table. So first add the following procedure to your module...

VBA Code:
Sub PauseMacro(ByVal secs As Long)

    Dim endTime As Single
    endTime = Timer + secs
 
    Do
        DoEvents
    Loop Until Timer > endTime
 
End Sub

Then try the following...

Looping through each spinner value

VBA Code:
    Dim theSpinner As Spinner
    Set theSpinner = ThisWorkbook.Worksheets("Letter").Spinners("Spinner 1") 'change the name of the spinner accordingly
 
    Dim i As Long
    With theSpinner
        For i = .Min To .Max
            .Value = i
            'your code to print to PDF, etc
            '
            '
            PauseMacro 3 'seconds
        Next i
    End With

Looping through each table record

VBA Code:
    Dim records As Variant
    records = ThisWorkbook.Worksheets("d").ListObjects("Table1").ListColumns("Record").DataBodyRange.Value 'change the name of the table accordingly
 
    Dim arrIndex As Long
    For arrIndex = LBound(records) To UBound(records)
        ThisWorkbook.Worksheets("d").Range("A2").Value = records(arrIndex, 1)
        'your code to print to PDF, etc
        '
        '
        PauseMacro 3 'seconds
    Next arrIndex

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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