VBA Excel Combobox: drop-down list scrolling issue - excel

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.

Related

I want to create an add button

I am newbie in excel and I really need a button that if pressed adds a certain value to the cells I selected, is that possible?
You can add buttons to Excel by enabling developer mode:
On the File tab, go to Options > Customize Ribbon.
Under Customize the Ribbon and under Main Tabs, select the Developer check box.
Then on the developer ribbon you can click insert, then select a button to draw onto the sheet.
You will then be given the option to assign a macro to the button (Also later accessible by right clicking the button).
As for the VBA code of the macro, you would need to be more specific about what functionality you require, is the 'certain value' to be added to the selected cells always the same value?
Someone answered me in two minutes on another site with this perfect code for what I wanted, thanks a lot sir. It works like magic.
Sub Add_to_Selection()
If Not IsNumeric([E4]) Then
Exit Sub
Else
Dim cell As Range
For Each cell In Selection
cell.Value = cell.Value + [E4].Value
Next cell
End If
End Sub

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.

Run Macro When ComboBox is Clicked

I need help with figuring out how to make a macro run when I click the ComboBox. For example, the ComboBox is pulling in a list from another column, if that column changes, I want to see the updated list when I click the ComboBox for this first time. Right now it is running the macro after I click the ComboBox and then click on an option that already exists. Once I reopen, then I see the updated list I want to see the first time a user clicks. Any help is appreciated, thank you so much!!!!
Sub costcenterdup()
'
' costcenterdup Macro
Application.ScreenUpdating = False
With Sheets("Dollars")
.Range("K9:K" & .Cells(9, "K").End(xlDown).Row).Copyy
Destination:=Sheets("LookUp").Range("E2")
End With
With Sheets("LookUp")
.Range("$E2:E" & .Cells(.Rows.Count, "E").End(xlUp).Row).RemoveDuplicates
Columns:=1, Header:=xlNo
End With
With Application.Worksheets("LookUp")
.Range("E2:E5000").Sort Key1:=.Range("E2")
End With
Range("C5").Select
Application.ScreenUpdating = True
End Sub
There are two types of combo boxes that you can add to a sheet.
One of them is a form combo box, which can be accessed from the "Forms" toolbar.
The other one is the ActiveX combo box, which can be accessed from the "Control Toolbox" toolbar.
A very good explanation of these is here (link is courtesy of #Ralph): http://peltiertech.com/forms-controls-and-activex-controls-in-excel/
To the first one you can only assign one macro when the combo box changes.
But to the ActiveX ComboBox you can assign several. If you add it to the GotFocus event then it will run the macro every time the box gets focus:
Private Sub ComboBox1_GotFocus()
'Add code here
End Sub
But if I correctly understand your question, that you want to have the combo box have the data in it from a column – then you need the first version of combo box (the form one) and simply right-click on it, select "Format control..." and on the Control tab set the cells you want the data filled with. It will automatically update the combo box for you and you will always see the values from the cells. There is no need for a macro in this case. See the below image:

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

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

Excel Hyperlink - jump to cell and scroll window

I am working in excel and I want to make a Hyper Link from the top of the page to another location on the page.
I type in a box at the top, and then right link and go down to hyper link in the dropdown menu I click it and select the tab that says "In This Work Book" and change it to where I want it to go. So all this is good and all but my Question is:
Can I make a Hyper link to bring me to a cell and scroll the window so the selected cell is the first row, instead of being near the bottom of the window?
Example:
Hyper link: "Test" Located in Cell A,1
Location Of Hyper Link: A,210
Now instead of having it put A,210 at the very Bottom and show the cells above it, I want to to be at the top and show the cells below it.
Thanks for the help,
Add the following VBA code to your worksheet:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
ActiveWindow.ScrollRow = ActiveCell.Row
End Sub
By magic, when you click a link, that cell will be at the top. If you don't want this behavior for all links, you can test the Target address.
You will have to save the code as a xlsm file so that macros are enabled. Use Alt-F-11 to open the VBA editor so you can actually add the code (double click the worksheet in the left hand pane, then paste the above code in the window that opens).

Resources