+ Reply to Thread
Results 1 to 5 of 5

Mouse fires SpinButton once, keyboard fires SpinButton twice

  1. #1
    Registered User
    Join Date
    12-09-2015
    Location
    USA
    MS-Off Ver
    2010
    Posts
    3

    Mouse fires SpinButton once, keyboard fires SpinButton twice

    Greetings. I'm fairly new to coding in VBA, and I've done my best to learn as I go, piece together code snippets, and look up answers to my problem. That said, I'm stuck and I can't figure out these two issues with SpinButtons.

    1. I've created a UserForm with five SpinButtons. Their purpose is to increase or decrease the value of the active cell by 0.1, 0.01, 0.001, etc. I also wanted the up/down keyboard arrow keys to function the same as clicking the up or down buttons, and the left/right arrow keys to switch the focus across the buttons. I've got this behavior worked out... mostly. Switching from one SpinButton to another with left/right works fine. Activating the buttons with up/down causes the addition/subtraction to fire twice. Clicking the SpinButtons works as normal. Why does clicking the SpinButton work as desired, but using the arrow keys causes values to be doubly incremented/decremented?

    2. The math related to the SpinButtons is written directly in VBA as the digit 1 in the appropriate tenth/hundredth/etc place. Spinning up a certain amount then down the same amount results in changes being made at a MUCH smaller level (using the first button up four times, then down four times results in a value of 2.78E-17). Why is this happening and how can I make it stop?
    Attached Files Attached Files
    Last edited by 79590; 12-10-2015 at 10:31 AM.

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Problems with SpinButton on UserForm

    you've gotta round the result to avoid floating point errors-say Round(Activecell.value2 + 0.1, 6)
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    12-09-2015
    Location
    USA
    MS-Off Ver
    2010
    Posts
    3

    Re: Problems with SpinButton on UserForm

    Okay. Reading up on floating point error explains away #2. It's still irritating, but at least now it's understandable. Only took a minute to add Round() to the mix and fix the problem.

    That just leaves #1. I've spent a couple hours trying everything I can think of, but I still cannot even begin to figure out why using the mouse fires the related _SpinUp or _SpinDown code once but using the keyboard fires it twice.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Mouse fires SpinButton once, keyboard fires SpinButton twice

    Hello 79590,

    The SpinButton class is programmed to respond to the Up and Down keys when SpinButton has the focus. The SpinButton SpinUp and SpinDown events are called when these keys are pressed.

    The reason you have the number doubling is your code duplicates the functionality built into the control. Remove the SpinButton KeyDown events and it work as you intended.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    12-09-2015
    Location
    USA
    MS-Off Ver
    2010
    Posts
    3

    Re: Mouse fires SpinButton once, keyboard fires SpinButton twice

    Well, I'll be. I didn't realize that the SpinButton controls were already programmed to do what I wanted them to do. Taking the Case vbKeyUp and Case vbKeyDown sections out of the form code fixed the issue! Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Keep Header Row out of Spinbutton UserForm
    By guitarsweety in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-20-2014, 07:18 PM
  2. How to pass a variable value from a Userform listbox/spinbutton to another sub
    By max3732 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-13-2014, 01:01 PM
  3. [SOLVED] UserForm SpinButton to increment Time after TextBox Entry
    By GRazor71 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-02-2013, 01:10 PM
  4. Spinbutton userform increase/decrease current cell on worksheet
    By Stingone in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-21-2012, 03:56 PM
  5. Problems with a Userform (dynamic userform)
    By pierre08 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 04-19-2010, 09:16 AM
  6. Spinbutton to cyle backwards/forwards on Multipage Userform?
    By MattMatt in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-22-2008, 09:50 AM
  7. [SOLVED] Userform SpinButton problem
    By Ozgur Pars in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-06-2006, 04:10 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1