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:
Related
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
I am relatively new at all of this. I have a code that takes information that is in a worksheet and sets it up to print. The data for this worksheet is copied from a pivot table in a different sheet with excel formulas. That sheet and its pivot table updates first when a location is selected, then the person can click a button that will adapt this linked worksheet to the information it contains and set it up to print.
Sub PrintIfNotEmpty()
Dim ra As Range, re As Range, i As Long, R As Range
ShInv.Rows("1:228").EntireRow.Hidden = False
Call Entry_Point
With ShInv
Set ra = .Range("A20:A228")
For Each re In ra
If IsEmpty(re.Value) Or re.Value = vbNullString Then
re.EntireRow.Hidden = True
End If
Next re
End With
With ShInv.PageSetup
.Orientation = xlPortrait
End With
ShInv.PrintPreview
Call Exit_Point
End Sub
This code runs fine if I step through it (F8), it runs fine if I hit play in the VBA coding section. As soon as I click the button it does NOTHING. It shows the circle like it's thinking and says it is doing the work, but NOPE.. No changes take place on the impacted worksheet.
I am at a loss regarding what to do next. Some locations have 228 products, some have 1. I need the code to adapt the worksheet so it only prints what is necessary for the inventory sheet.
Also, I have an EXCEL listbox for selection on my dashboard (ActiveX controls are blocked for us). What is the Worksheet activity that selecting an item from the dropdown list would be? It is not SelectionChange, as that action is not triggering the macro when a different item on the list is selected (ie the listbox changes). That only triggers action if the listbox is changed, the user tabs away from the listbox and then clicks on the listbox again. I just want one that happens as soon as the listbox changes without all the extra actions.
This might be because of the type of file it is saved as.
Try saving it as an "Excel Binary Workbook". Maybe this can help run your worK?
I have a UserForm with refedit, textbox, and command button controls.
I would like the user to be able to select a range of cells from the active worksheet with the refEdit control, and as the range values are selected, the sum of its values are displayed in the text box.
Once the total of the ranges are selected, the user will click the command button, which should copy the value from textbox, close current UserForm, open another UserForm and paste the value into a textbox.
However, when I click the refEdit control, it only shows the refEdit textbox by minimizing the user form until the button is clicked. How can I prevent this from happening?
Also, the code I wrote for the textbox doesn't work, in fact it doesn't do anything:
Private Sub RefEdit1_Change()
txtbxSum.Value = Sum(RefEdit1.Value)
End Sub
Thanks!
The RefEditcontrol has its issues, so it may not be the best design choice. As Ashleedawg commented, see here
That said, the RefEdit.Value property is a string representing the selected range. So to Sum that range you need to use
Private Sub RefEdit1_Change()
txtbxSum.Value = Application.Sum(Range(RefEdit1.Value))
End If
Note that RefEdit1_Change fires every time the Selected Range changes (so if the user drags over a 3 cell range it will fire three times). If you also have a txtbxSum_Change event it will fire each time the refedit1_Change event updates the
Textbox with new value, ie as the selected range Sum changes.
I am maintaining a large spread sheet at work and there are ActiveX command buttons throughout the code. One sheet I am currently working on has almost 100 of these. I have made sure the button itself is visible as some of the buttons are hidden/unhidden depending on the flow of the code.
Is there a way I can find whereabouts on the sheet itself the button is located that the VBA code is pointing to? Here is a snippet:
enter code here
Private Sub CommandButton4_Click()
Application.Goto Range("Add_Trainees"), True
CommandButton3.Visible = True ' unhides menu button
CommandButton81.Visible = True ' hides SC1
CommandButton97.Visible = True ' hides SC2
End Sub
I am trying to find where on the sheet where command buttons 3, 81 and 97 are located.
TIA for any help/suggestions.
I think pnuts provides the correct way to find out where the buttons are.
I would also suggest to open the panel from "Developer" -> "Properties". In that panel, you can check out all the ActiveX Controls, including the command buttons, in the list.
Background Details
I have an excel spreadsheet with Activex dropdown (combobox) objects which help the user to know what options are available. I did this because the data validation list dropdowns are way too small in font size, and were gathering a lot of complaints.
So my solution was to add combobox objects which allow the user to select from a range of options. However, I have to link the comboboxes to a cell with the linkedcell property, so that both the user and various formulas can see what has been chosen. I also set up the combobox to disappear when it's not in use (much in the same way as the data validation dropdown button only appears when you select the relevant cell).
Here is the problem:
I don't want the users to edit the value in the linked cell, so I make sure the linked cell is locked whenever the combobox is not selected:
Private Sub comboBox1_GotFocus()
Call unlockComboBoxTargetCell(comboBox1)
End Sub
the procedure above does this:
If (targetComboBox.LinkedCell <> "") Then
Dim targetCell As Variant
Set targetCell = Range(targetComboBox.LinkedCell)
If Not targetCell Is Nothing And targetCell.Locked <> False Then
unlockSheet (activesheet.Name)
targetCell.MergeArea.Locked = False
lockSheet (activesheet.Name)
End If
End If
Equivalent procedures exist to lock the target cell.
However, whenever you do a "Save As" action on the workbook, it seems that the linked and locked cells create a problem: Excel gives this error out of the blue:
"The cell or chart you are trying to change is protected and therefore read-only..."
This error comes up about twice or three times for each cell that is locked and is the linkedcell for a combobox.
Is there a good way to overcome this problem? Right now my best solution is to leave the cells unlocked and place data validation on the cell, so that if the user edits the cell they will at least be refused when they type something invalid. I could make sure that the combobox covers up the linked cell whenever it is selected, but sometimes that means having a very large, annoying combo box with a very tiny dropdown button on its right side.
Perhaps I am being a bit too particular about the user interface?
Thanks in advance for reading this long and involved post.
In the "lockSheet" procedure you have created, the code to 'protect' the worksheet needs an additional parameter, UserInterfaceOnly, set to true.
I imagine the LockSheet sub is something like this;
sub lockSheet(strSheetName as string)
thisworkbook.sheets(strSheetName).Protect
end sub
Try this:
sub lockSheet(strSheetName as string)
thisworkbook.sheets(strSheetName).Protect, UserInterfaceOnly=True
end sub
UserInterfaceOnly allows programmatic changes to the protected sheet.
Bill