Excel Scroll Bar User Form won't Continuously Update when Dragged - excel

I am designing a spreadsheet that will utilise a user form with a scroll bar on it. I need to scroll bar to update the cell specified in real time as I drag the bar using the mouse, at present it only adjusts the value In the cell when I release the mouse.
I initially wanted to use a slider but from research it appears that these can't be used in a userform.
Does anyone know any VBA code or anything that will make the scroll bar continuously update as I drag the bar using the mouse?

If you are using the scroll bar on a userform and you want the update to be while dragging you need your code to be in the scroll bar's scroll event handler. The behavior that you descried happens with the scroll bar's change event handler. To see the difference, create a new userform with a scroll bar as the only control on it. In it's code module enter the following two event-handlers. When you run it you should see B1 but not A1 updating smoothly:
Private Sub ScrollBar1_Change()
Range("A1").Value = ScrollBar1.Value
End Sub
Private Sub ScrollBar1_Scroll()
Range("B1").Value = ScrollBar1.Value
End Sub

Related

Excel Hyperlink- When clicked make target cell always upper left hand corner

I have a very wide excel spreadsheet. It's formatted in such a way that there is specific data on each screen page. I have the first column frozen so I can place hyperlink buttons vertically. These buttons are hyperlinked to each of the formatted pages in the worksheet.
When I click one of the hyperlink buttons I would like the worksheet to scroll so the target cell in the UPPER LEFT corner of the screen.
I have googled and found this VBA code to place in the "thisworkbook" section of the VBA editor.
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
ActiveWindow.ScrollRow = ActiveCell.Row
ActiveWindow.ScrollColumn = ActiveCell.Column
End Sub
This code does not seem to work for me. If I'm at the far left of the screen and press the hyperlink button it scrolls to the target cell until it gets on the active screen. So basically it always ends up somewhere on the Upper right of the screen. However, if I click the hyperlink buttons the require the page to scroll to the left side of the worksheet it does work.
How can I make it so the target cell is ALWAYS in the upper left of screen after I press the hyperlink button?
Also, how do I know the above code is even running? The hyperlinks seem to be doing the same thing with or without it. I just cut and pasted the code into the VBA editor (per google) for thisworkbook. Is that it or do I need to check something so it will be activated?
Thanks in adavance.
There's a way to do that without using VBA. You can just use a normal hyperlink in the spreadsheet, and link it to "Place in This Document". Make sure you pick the correct Sheet and Type the cell reference that you want to go to (I assume this is A1). This will make sure that whenever you click that particular link, it will always go to that particular spot.
Here's how the edit hyperlink would look in an empty spreadsheet.

VBA Enable only worksheet scrolling while UserForm is active

Is there a way to enable only the worksheet scrolling while userform is active?
.show (vbmodeless)
Doesn't work for my needs as it enables all editing in worksheet, but I need to let the end user to only scroll the worksheet.
Could someone please share an answer to my question, please?
I suggest you put a vertical scroll bar on the form itself and use changes in the value of the scroll bar to scroll the worksheet using vba code. The form can then be modal and you retain control of the worksheet.
For example, I put a scrollbar on a modal form and ran this code:
Option Explicit
Private Sub ScrollBar1_Change()
Debug.Print ScrollBar1.Value
Sheet1.Rows(ScrollBar1.Value + 1).Select
End Sub
Which worked fine. You will need to adjust the settings of the scroll bar to be sensible in relation to the size of the worksheet and the way you want the user to interact, but if scrolling is the only function you want to allow, then perhaps this could work.

Scrollbar height in userform frame doesn't update right away. I need to click it to update it

So I have an Excel userform that contains a frame. In this frame, there may be a X amount of dynamically added objects that fill it's content until it overfills. Of course I added a vertical scrollbar to compensate this issue, this is not the problem.
The bottom frame with caption "Données" is the one im talking about:
The problem is much trickier. So as you can see in the previous image, there are a bunch of buttons (actually toggle buttons). When you toggle a set of each of them, they will appear in the frame below. In fact, all the possible combinations of the toggled buttons will be shown.
Example 1:
Example 2:
The toggle buttons click methods are actually programmed in a class-module that calls a sub directly in the userform (named RSAjout2).
Option Explicit
'THIS PIECE OF CODE IS WRITTEN IN A CLASS-MODULE
' Is not useful for this demonstration
Public WithEvents btn As MSForms.ToggleButton
Public frm As UserForm
'''
' Here, each button is an object that calls the refreshData function to make sure it is updated each
' time one of then is pressed
Private Sub btn_Click()
'We call the subprocedure in the userform RSAjout2
RSAjout2.refreshData
End Sub
When this sub (refresh data) is called, there is a part of its code that ajusts the height of the scrollbar.
Public Sub refreshData()
'###Lots of code that I dont have any problem with###
RSAjout2.CadreDonnees.ScrollHeight = totalHeight
'Where totalHeight is just the height of all the frames stacked in the big frame
End Sub
And that works fine too. (So what is the problem then?)
The problem is that when the code actually resizes the height of the scrollbar, it doesn't actually update! I have to either click on the scrollbar itself (and move it) to make it change shape or click another toggleButton (but its state is an old one). Let me show you with pictures.
If I make a combination of tests and medias so that the number of frames overfill the container frame, it should display a scrollbar right (well its height should update)?
Wrong:
Then if I click the scrollbar, and move it just as it already existed, it suddently updates it is now visible.
Why?
This only happends with my toggleButtons. If I, for example, use my "Abondonner" button to change the height of the scrollbar, it works perfectly. Why is that so?
Private Sub Abandonner_Click()
'Code to test if the scrollbar updates normally from a normal click procedure
Me.CadreDonnees.ScrollHeight = 1000
'Spoiler* It works perfectly
End Sub
EDIT: This seemed to do the trick. However, it is clearly not the best way to solve this problem. Well at least it works...
'An aweful, but working solution
CadreDonnees.ScrollTop = 1
CadreDonnees.ScrollTop = 0
CadreDonnees.ScrollHeight = totalHeight
CadreDonnees.ScrollTop = 1
CadreDonnees.ScrollTop = 0
Try invoking the Repaint method of your Frame control =)
CadreDonnees.Repaint

Close userform with Esc without control_KeyPress() command for each control

In this answer, it's shown that Excel userforms can be closed with Esc by setting up a control_KeyPress() sub for each control that can take focus - Close userform with escape button
I've gotten this to work, but I have several menus, each with a number of controls. I tried putting this routine on just one button, but it's not always quick to tab/nav back to a specific button.
I'm looking for a way to make it so that I either
don't have to create control_KeyPress() subs for every control that can take focus
or, can achieve the same goal (closing the userform with esc) in a different way
Thanks!
My idea would be to create a command button on the form, set the cancel property to true AND set the width and height to 0. Add the following code to the not visible button.
Private Sub CommandButton1_Click()
Hide
End Sub
Leave the visible property on true
Create a small button, e.g. 6 x 6 so that it can be visible but unobtrusively positioned in one of the corners of the form. Making it this small stops the caption from being displayed and just leaves a neat, small square.
Create a button, (e.g. BTXX), and set:
Cancel = True so that pressing the escape key "presses" this button
TabStop = False so that tabbing through the form does not stop on the button
Caption = "Esc" as a reminder of what the button is for
In the onclick event use:
Private Sub BTXX_Click()
Unload Me
End Sub
In this example the escape button is "hidden" top-left on the form by setting Top=0 and Left=0. (See images below).
Userform
Properties
If you already have a button for closing the userform then you can simply set its cancel property to true. It is then triggered by pressing escape, remains clickable, keeps its accelerator and stays in the tab order. You can only have 1 button with Cancel=True and the property editor takes care of this automatically.

VBA Excel Combobox: drop-down list scrolling issue

I am running 32-bit Excel 2010. I have created multiple ActiveX Control combo boxes and they all have numbers of entries in their drop-down lists. The thing is that instead of using mouse click to scroll the list, I want to use the mouse scroll to scroll the list, but it actually doesn't work. When I scroll inside of the list, it scrolls the whole list down instead of the content in it. So does anyone know how to add this feature to it?
I used this method to stop the list detaching from the combo box and moving down the sheet with the mouse scroll. It actually disables the mouse scroll, but you can still move the mouse to select an item, and operaton the right scroll bar if it appears.
Select the row(s) where you have placed the ActiveX combo Box and the sheet
Type a named range in the Formula Bar, and press enter. eg: "rngJobRoleCombo"
Right click on the control in Development mode, and select "View Code"
Select the control's GotFocus event
Private Sub cboJobRole_GotFocus()
Me.ScrollArea = Range("rngJobRoleCombos").Address
End Sub
Select the controls LostFocus event
Private Sub cboJobRole_LostFocus()
Me.ScrollArea = ""
End Sub
This limits the mouse scroll to the cell range address of the worksheet while the control is in focus.

Resources