Consulting

Results 1 to 15 of 15

Thread: Solved: Spinner Button

  1. #1
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location

    Solved: Spinner Button

    Is there a way to control the UP and DOWN events for a spinner button created from the Form toolbar as opposed to inserting a spinner button form within VBA? A form created in VBA has the Private Sub SpinButton1_SpinUp() and Private Sub SpinButton1_SpinDown(); however, I haven't found a way to distinguish the up or down direction when I use a spinner button created from the toolbar (Excel 2000). When I test my spinner button with Application.Caller it shows the same result regardless of the up or down direction clicked.

  2. #2
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    So far, the following code is doing what I want it to do. I'll leave the thread open for a while in the event someone has a better or more succinct solution.

    [vba]
    Dim lastVal As Integer


    Sub createStatement()

    Dim sp As Spinner, forMonth As Range, rptMonth As Date

    Set sp = ActiveSheet.Spinners(Application.Caller)
    lastVal = lastVal
    rptMonth = Range("ReportMonth")
    Set forMonth = Range("ReportMonth")

    With sp

    If Len(lastVal) = 0 Then

    lastVal = lastVal - 1

    ElseIf .Value > lastVal Then

    forMonth.Value = DateSerial(Year(rptMonth), Month(rptMonth) + 2, 0)

    Else

    forMonth.Value = DateSerial(Year(rptMonth), Month(rptMonth), 0)

    End If

    lastVal = .Value

    End With

    closeIt:

    End Sub
    [/vba]

  3. #3
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    So far, the following code is doing what I want it to do. I'll leave the thread open for a while in the event someone has a better or more succinct solution.

    [vba]
    Dim lastVal As Integer


    Sub createStatement()

    Dim sp As Spinner, forMonth As Range, rptMonth As Date

    Set sp = ActiveSheet.Spinners(Application.Caller)
    lastVal = lastVal
    rptMonth = Range("ReportMonth")
    Set forMonth = Range("ReportMonth")

    With sp

    If Len(lastVal) = 0 Then

    lastVal = lastVal - 1

    ElseIf .Value > lastVal Then

    forMonth.Value = DateSerial(Year(rptMonth), Month(rptMonth) + 2, 0)

    Else

    forMonth.Value = DateSerial(Year(rptMonth), Month(rptMonth), 0)

    End If

    lastVal = .Value

    End With

    End Sub
    [/vba]

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [VBA]Sub Spinner1_Change()
    Static lastValue As Variant
    Dim spinner As MSForms.Control
    Dim linkedCell As Range
    Dim dir As String

    With ActiveSheet

    dir = "Up"
    Set linkedCell = .Range(.Spinners(Application.Caller).linkedCell)
    If Not IsEmpty(linkedCell.Value) Then

    If linkedCell.Value < lastValue Then

    dir = "Down"
    End If
    End If

    lastValue = linkedCell.Value
    End With

    MsgBox dir
    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

  5. #5
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Thank you.

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    [VBA]Private spin
    Sub Spinner1_Change()
    MsgBox IIf(Sheets(1).Spinners(1).Value > spin, "up", "down")
    spin = Sheets(1).Spinners(1).Value
    End Sub
    [/VBA]

  7. #7
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Thanks. I love the brevity.

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    In that case:

    [VBA]
    Private spin
    Sub Spinner1_Change()
    x = Sheets(1).Spinners(Application.Caller).Value
    MsgBox IIf(x > spin, "up", "down")
    spin = x
    End Sub

    [/VBA]

  9. #9
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Thanks again!

  10. #10
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by snb
    In that case:

    [vba]
    Private spin
    Sub Spinner1_Change()
    x = Sheets(1).Spinners(Application.Caller).Value
    MsgBox IIf(x > spin, "up", "down")
    spin = x
    End Sub

    [/vba]
    I've tried with no success. How would that need to be modified to, for example, call SubA if Up is clicked or SubB if Down is clicked? (P.S. I'm editing here to clarify what might not have been clear in my original post. My objective is not to have to be tied to the internal controls of the spinner but, rather, to just use the up down buttons, so I am looking for a way to bypass the controls and simply detect up or down so I can call the appropriate Subs depending on which arrow is clicked.)
    Last edited by Opv; 01-27-2013 at 04:48 PM.

  11. #11
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    On second thought, I can get it to work in a crude sort of way by resetting the Sheets(1).Spinners(1).value to 1 at the end of the Sub. That way I can test whether "spin" is 2 or 0 and call the appropriate Sub accordingly.

    [vba]
    Sub Spinner1_Change()

    If Sheets(1).Spinners(1).Value = 2 Then
    MsgBox "Going Up"
    Else: MsgBox "Going Down"
    End If

    Sheets(1).Spinners(1).Value = 1

    End Sub
    [/vba]

    Please let me know if there is a better way to accomplish the above objective.

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If you are not interested in the functionality of the control, why use them at all. Get yourself an up arrow image, and a down arrow image, and assign separate macros to them.
    ____________________________________________
    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

  13. #13
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by xld
    If you are not interested in the functionality of the control, why use them at all. Get yourself an up arrow image, and a down arrow image, and assign separate macros to them.
    That's a valid question, one in fact for which there is likely no good answer. I guess I just like the looks of the spinner button better than having to use two separate buttons.

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If you butt them up against each other, they look like a spinbutton.
    ____________________________________________
    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

  15. #15
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by xld
    If you butt them up against each other, they look like a spinbutton.
    Yeah, I had already employed that option but was just exploring the possibilities. If forcing the spinner button to reset and testing for two known spinner values is less efficient, perhaps I'll forgo the notion and continue using the separate buttons. Thx.

Posting Permissions

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