I have an Excel sheet for data input by a third party. To restrict erroneous data I want to restrict data to a certain list of items.
As the list is rather long, I want to use a combobox, so that when they start typing the correct names show up.
This worked in testing, but as soon as I shipped the Excel file the ActiveX Combobox started acting out. This appears to be known behaviour, without a fix besides not using the ActiveX version of the combobox.
I'm switching to make use of the Forms version of the combobox.
Besides the combobox there is a simple cell with data validation with two options: "Professioneel" and "Particulier". I use an onchange event to capture if the selection changes, and then I populate my combobox with the corresponding list.
This worked with the ActiveX version, but nor the forms version.
The code below is my latest try at populating the forms combobox.
The Else clause of the If statement still shows the code that used to work for the ActiveX version of the combobox.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("C7")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
If Range("C7").Value = "Particulier" Then
ThisWorkbook.Worksheets(4).Shapes("ComboBox1").List = ThisWorkbook.Worksheets(8).Range("B2:B58").Value 'this does not work
Else
ComboBox1.List = ThisWorkbook.Worksheets(7).Range("B2:B434").Value 'this used to work for a combobox
End If
End Sub
I get error 438.
How do I get this to work with a Forms version of the combobox, or is this behaviour not supported in the forms version?
Let's say that your drop down is named "Drop Down 1", try the following . . .
ThisWorkbook.Worksheets(4).Shapes("Drop Down 1").ControlFormat.List = ThisWorkbook.Worksheets(8).Range("B2:B58").Value
Alternatively, you can refer to your drop down using the DropDowns collection . . .
ThisWorkbook.Worksheets(4).DropDowns("Drop Down 1").List = ThisWorkbook.Worksheets(8).Range("B2:B58").Value
Related
I am working on a project that requires me to disable certain option buttons in excel 2010 based on the results of prior option buttons. Some criteria will need multiple if statements, similar to a nested if. Some info; cell B107 in my example reflects which of the 5 option buttons they might have chosen. I have tried the following code but I have not been able to find success. Additionally, how would I make the disabled option button appear grey.
Private Sub OptionButton22_Change()
If Range ("B108") = 1 Then
OptionButton22.Enabled = False
Else
OptionButton22.Enabled = True
End If
End Sub
I have a Slicer in my excel with the following 2 values
Add Customer
Add Vendor
I want to open 2 different forms when click on those value. Can anyone please help me with this.
Yes, you can call different forms on slicer clicks, check the below code. Hope it suits your requirement and if not you can use to change as per your requirement.
On Writing your code in Worksheet_PivotTableUpdate event, on each slicer click this event will get called.
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
If ActiveWorkbook.SlicerCaches("Slicer_Region").SlicerItems("East").Selected = True Then
UserForm1.Show
ElseIf ActiveWorkbook.SlicerCaches("Slicer_Region").SlicerItems("West").Selected = True Then
UserForm2.Show
End If
End Sub
Where,
Slicer_Region = "Name to use in formulas"
You can get this name by right click on slicer and click slicer settings. and find the name to use in formula.
East or West = "Names of slicers"
For which you want to call different forms.
The solution works BUT THE CODE MUST BE in the code module of the worksheet with the pivot table
It does not work on main slicer, however if you select any particular item it works. In that case it is better to set the macro on the slicer itselft. You just need to click on slicer and it will be run automatically.
I am currently in Excel 2010 and created this spreadsheet and code in this version. My colleagues (using the spreadsheet) are using Excel 2007 (haven't been upgraded yet). They are getting the error message but I am not.
I had the following code set up so that based upon the selection in the ComboBox, the specified cells would populate with "N/A" or remain/become blank.
Private Sub ComboBoxSite6_Change()
If ComboBoxSite6.Value = "N/A" Then
Range("Site6Cells").Select
Selection = "N/A"
Else
If ComboBoxSite6.Value <> "N/A" Then
Range("Site6Cells").Select **This line highlights with the error when debugged
Selection = ""
End If
End If
End Sub
The change of the combobox and the population (or not) of "N/A" is all happening with the same sheet of the workbook so that shouldn't be a problem. But even so, I tried adding the following before each of the "Range..." lines: Sheets("Site Prep"). This did not work either.
Additionally, I have 5 other columns and comboboxes doing the same action, coded in the same way prior to this piece of code and there are no problems indicated.
Can anyone tell me what the problem might be? Is this a backwards compatability issue?
It looks as if the Range called "Site6Cells" doesn not exist in the right way.
Named ranges can be either global (= member of the whole workbook) or local (= member of the worksheet). If the Named range you created is a local one than you cannot call it as if it were a global one.
Check this via the ribbon for Formulas > Name Manager > Property called Scope
Compare this with that for your collegues and see if this is different.
To change it you will have to recreate it (delete the old ane, make a new one), or use a 3rd party Name manager, or change the scope via VBA.
ActiveX combobox objects in Excel do not behave well when their ListFillRange refers to a formula-based Named Range (Defined Name).
I think I have encountered other errors and possibly even Excel crashes thanks to this, but right now all that happens is the combobox_change() event is triggered anytime ANY cell in the workbook is changed.
I am not sure if this is really a bug, or if there is a fix, or a workaround. If it is a bug, how do I report it to the Excel people?
And finally, the real meat of my question is "How do I work around this issue best?" I would like to have some formula-based named ranges, but it seems like this won't be possible.
To reproduce this bug, do the following:
Create a new workbook. On Sheet3, create a small table 3 columns across, and several rows high.
Create a named range with this formula (or an equivalent): =OFFSET(Sheet3!$A$2:$C$36,0,0,COUNTA(Sheet3!$A:$A),COUNTA(Sheet3!$4:$4)) To do this use Input>Name>Define. Name the range something like "demoRange"
Go to Sheet1 and create a combobox, (it must be on a separate sheet). (Use the Control Toolbox menu, not the Forms menu).
Click on the Design Mode button (the blue triangle with pencil), then right click on the combo box and go to Properties.
In the properties window for the combobox, change the ListFillRange property so that it points at the named range you created in step 2 ("demoRange").
You may want to change the ColumnCount property to 3, and the ColumnWidths property to "50,50,50"
Set the linkedCell property to cell "A1" by typing A1 in the linkedCell property.
Close the properties window, and double click on the combobox to define its change() event.
Put a Debug.Assert(false) or Msgbox("demo") line in the subroutine for the new combobox's change event.
Exit design mode
important - Now select an item in the combobox. The event should trigger normally the first time. (The bug will not show if you don't do this step--something must be selected in the combobox)
Edit cells anywhere in the workbook [Edit] or any other open workbook [/edit], on any sheet and any location. Each time you edit any cell, (at least for me), the onchange event for the combo box is run.
Again, is this normal, and what is the best alternative for what I am doing? This combo box gets linked to various cells, and is supposed to be a replacement for the tiny font in the data validation dropdowns excel provides by default.
My advice is to never use ListFillRange and LinkedCell. They are just trouble. Fill your listbox with List and use the Change event to write to the cell. Somewhere, maybe the Workbook_Open event, fill the listbox
Private Sub Workbook_Open()
Sheet2.ListBox1.Clear
Sheet2.ListBox1.List = Sheet1.Range("demoRange").Value
End Sub
Then in the change event in the Sheet2 module, check that something was clicked and write it to the cell
Private Sub ListBox1_Change()
If Me.ListBox1.ListIndex >= 0 Then
Sheet2.Range("A1").Value = Me.ListBox1.Value
End If
End Sub
I have a few options available that I am aware of thus far. The best I can come up with is this:
Avoid directly using formula-based named ranges. Instead, define a subroutine that will check whether the defined range "demoRange" should be changed from what its current value is. Run this subroutine on the workbook_open and sheet3_deactivate events. If needed, prompt the user to ask if it's all right to update the named range. [edit] The macro that updates "demoRange" could probably just copy from a "demoRange_FormulaBased" named range into "demoRange" which would be static. [/edit]
This solution works well because you can keep using the linkedcell property, you don't have to use VBA to populate the comboboxes, and the named range can still be used for whatever other purposes it already had. Avoid using the onchange event to run this new subroutine, since it might end up being triggered thousands of times if a user opens the Find/Replace dialog and chooses "Replace All".
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