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
Is it possible to save changes to a Label Caption in a userform in Excel VBA, so that they are permanently saved, and only changed when you enter a new change?
I have checked, that the code is changing the caption, but I cannot get it to stick, so that it is still there next time I open the userform.
Thank you in advance
Private Sub cmdSubmit_Click()
'resets participants email and name
If Me.optProg.Value = True Then
Me.NameLabelProg.Caption = Me.CB_Part.Value
Me.MailLabelProg.Caption = Me.TB_Mail.Value
ElseIf Me.optTester.Value = True Then
Me.MailLabelTest.Caption = Me.CB_Part.Value
Me.NameLabelTest.Caption = Me.TB_Mail.Value
End If
End Sub
Check out this userform example and you'll find answers to many of your questions.
Source of userform
Forgive the lengthy answer initially, but there is a bit of a risk involved with making permanent changes to UserForms through VBA.
In order to change the caption of a Label (or UserForm or any other Control permanently, you will have to "Trust access to the VBA project object model" in order to do it via VBA code. Now, while this is possible it is not usually recommended because it can seriously put a user's PC at Risk should they encounter a Macro developed for nefarious purposes.
(To clarify in case the question is raised, your end user(s) will have to make this Trust setting change on their PC's as well . . . you cannot make the change on your PC, setup the code to work and then hand the file over to another user and have it work on their PC without them making the same change.)
There are methods to do this programmatically, but, this falls into the "nefarious Macro" rabbit hole and would need to be disclosed to the end user you are making this change. . . Research at your own risk.
If you are OK with putting yourself at risk, you can do it using VBA similar to the following snippet I found here. You will have to substitute your UserForm name and Label name as appropriate. I tested it on my own UserForm and it works as expected.
Sub Change_Userform()
ThisDocument.VBProject.VBComponents("Userform1").Designer.Controls("Label1").Caption = "Some new caption text"
End Sub
You will need to do some research on how to "Trust access to the VBA project object model" yourself and understand the risks to do this in order for the above code to work.
If I understand the intent of what you are trying to accomplish correctly though, you could achieve this effect without having to put yourself or your end user(s) at risk.
(Most end user(s) typically would not have direct access to the VBA Designer where they would see the UserForm's un-initialized environment.) To do this, you would have to place your code in the UserForm's event.
The following assumes your Me.optProg.Value and Me.optTester.Value are Option Buttons which a user would change. If you create a "Settings" sheet in the file, you can place values in the cells of this sheet and then hide it so the users do not modify them directly. Then, reference the values of the cells and change the appearances of the Option Buttons at the same time as the UserForm's launch. (Additionally, you can set the Click events of the Option Buttons to change the values of the same cells and provide that change to affect the UserForm's Initialize event when called, but this should get you going in the right direction.)
Sub UserForm_Initialize()
'The Range below is completely up to you.
'Since you are using Boolean True/False, a simple "1" or "0" _
is easy to use to make the changes.
If Thisworkbook.Sheets("Some_Settings").Range("A1").Value = "1" Then
Me.optProg.Value = True
Else
Me.optTester.Value = True
End If
'do some other code here as needed to finish initializing the UserForm
If Me.optProg.Value = True Then
Me.NameLabelProg.Caption = Me.CB_Part.Value
Me.MailLabelProg.Caption = Me.TB_Mail.Value
ElseIf Me.optTester.Value = True Then
Me.MailLabelTest.Caption = Me.CB_Part.Value
Me.NameLabelTest.Caption = Me.TB_Mail.Value
End If
End Sub
I’m writing a simple macro and am a bit rusty in my VBA skills. The macro has a list of options with “yes”, “no” and “maybe” options to choose from. I used checkboxes rather than option buttons so each option doesn’t have to be in an individual frame.
The code I have written (below) deselects the other options when one is selected, but I have to select it again for it to actually be selected which will be frustrating for the user.
Private Sub Yes_Click()
No.Value = False
Maybe.Value = False
End Sub
I have tried adding the additional line
Yes.Value = True
But this then crashes excel when the macro is run.
I can’t find any advice for this problem so any help is greatly appreciated.
I have been playing around with form designs and now I have constructed a form, which is almost complete, just one problem left. Let me explain first:
Form is bound to a "Join Table", which has only 2 fields - ID from "Table1" and ID from "Table2". Based on those two fields I have added fields from "Table1" & "Table2", on same form. Then I have added 2 option buttons, 1 Combobox and 2 Subforms.
This allows me to watch records from two different tables that are joined, from each point of view - "Table1" or "Table2" view. I am selecting this views with Option buttons, which changes Combobox rowsource, so you can navigate to records from Combobox.
Here is code :
Private Sub OptButton0_Click()
If Me.OptButtonO.Value = True Then
Me.OptButton1.Value = False
Me.Cmbox.RowSource = "SELECT [Table1].[Field1], [Table1].[Field2], [Table1].[Field3] FROM Table1 ORDER BY [Field1];"
Me.Cmbox.SetFocus
Me.Cmbox = Me.Cmbox.ItemData(0)
End If
End Sub
Private Sub Cmbox_AfterUpdate()
If Me.OptButton0.Value = True Then
If IsNull(Me!Cmbox) Then Exit Sub
With Me.RecordsetClone
.FindFirst "[Field1] = " & Me!Cmbox
If Not .NoMatch Then
If Me.Dirty Then Me.Dirty = False
Me.MySubform.Width = 8280
Me.MySubform.SourceObject = "MySubform"
Me.Bookmark = .Bookmark
Else
Me.MySubform.Width = 8000
Me.MySubform.SourceObject = ""
End If
End With
Me.Cmbox.SetFocus
DoCmd.Requery
End If
End Sub
This posted code is only for one Option button, second one is same, just opposite. Now what is problem ?
Problem is that when I navigate through record via Combobox, click second Option button for another view AND THEN RETURN to same view, my subform results stays same as they were when I clicked another Option button, although Combobox listIndex is 0. If I select combobox Listindex from Combobox, code works again.
SO BASICALLY - I NEED CODE THAT WILL TRIGGER COMBOBOX CHANGE WHEN OPTION BUTTONS ARE CLICKED. It works when you're clicking in Combobox, but not when clicking in Option button.
I know It's complicated to understand, please take a look at code, and ask anything. Any help appreciated.
Another option: I had a similar issue: with an unbound combobox. In the '*_Change' event in a combobox, if I pick a value from the dropdown, the value is there; but if I delete the existing value, the prior value still shows up.
...
If Me.series_filter > "" Then
lstrMetric = lstrMetric & "and X.series_name = '" & Me.series_filter & "' "
End If
...
Me.Filter = Mid(lstrMetric, 5)
...
I have a dropdown for a filter on the form: picking a value is meant to set (or clear) the filter. It worked when I picked a value, but would not clear if I delete it.
I added a line to the start of the code to commit updates:
Me.dirty = false
The code now recognizes the null value when the combobox is cleared. This works in my case - of course it would be a problem if you didn't want any updated fields written to the database.
call the the combobox afterupdate event in the option button click event:
private sub optbutton0_click()
...
cmbox_afterupdate()
end sub
PS: Rather than having events for option buttons directly, you should put them in a frame, (even if you then have to make the frame transparent to stop it from appearing) and use the afterupdate or click events of the frame, whereby you can get the selected option button by option value:
private sub frame0_click()
select case frame0
case 0 'option button 0 is selected
...
case 1
...
end select
end sub
In Access, controls can be funny. I mean, when you change the option, you see it change, it changes on the screen... but the control doesn't always change its .value right away... meaning it doesn't Update. For a simpler illustration of this principle, create a textbox, type some stuff into it, and look at the textbox.value in the immediate window. Then, with the cursor still in the textbox, backspace some of it. Check the value again. Do that a few more times. Start including textbox.text in your tests. There are multiple ways around this, but it is most important to understand when controls actually update, so you can understand better when and which work arounds to use.
Now, in this situation, you typed 'click' every time when you referenced selecting an option. I think I will hold you to that. Personally, I use Tab and the arrow keys sometimes, which would be a slightly more complicated answer, but one you will be better equipped to solve yourself, after understanding the textbox example above. But, if you change AfterUpdate above to Click, you should be good.
That said, there are events besides mouse clicks that can activate the Click event. Usually not a problem, but since you are changing the look of your form, specifically the width, you may want to be aware that the subform width may flash back and forth sometimes while using your tool. Not often, probably won't be too much of an annoyance, but I might rethink why I needed to change width at all, or if there might be better triggers for it.
I'm creating an Excel vba-based application in which specific data should be shown, and for filtering this information I'm trying to use ActiveX Chekboxes.
After I create the checkboxes I can check/uncheck them normally, and I use some macros to perform somethings I want, for example, to make sure only one option can be checked I use:
Private Sub CheckBox1_Click()
If Worksheets("Cover").CheckBox1.Value = True Then Range("O8") = 1
If Worksheets("Cover").CheckBox1.Value = False Then Range("O8") = 0
If Worksheets("Cover").Range("O8") = 1 Then
Worksheets("Cover").CheckBox2.Value = False
Worksheets("Cover").CheckBox3.Value = False
Worksheets("Cover").CheckBox4.Value = False
End If
End Sub
The problem is that, after I save this worksheet and reopen it to start working again, I can't check or uncheck the boxes anymore. I also can't format them on Designer Mode anymore (if I right-click one of the checkboxes and select 'Properties' a window to change the properties of the active worksheet appears). And, of course, the macros don't work anymore (Runtime error '438').
Does anyone have a clue about what may be the problem and how to solve it?
Thanks in advance!