I am sure someone else has asked this, but I can't find a post with an answer that fits my situation. I am trying to fill and excel sheet from a userform. The first cell will be filled with one of several choices that are available. Each choice is allotted its own checkbox. This is due to the format of the paper form that my database is based off of. The picture shows the separate checkboxes. How would I go about this? And is it possible to ensure that only one checkbox is used at a time?
Any assistance will be appreciated.
Please disregard this question. I found a simple stacked method of achieving my goal here. I used this example I found on a forum and tried stacking more than one. It worked, but only one can be checked at a time to create a record.
If "X" = True Then
Sheets("Y").Cells(emptyRow, 1).Value = ("Z")
End If
X = Checkbox Name
Y = Destination Sheet
Z = Checkbox Caption (or other desired caption)
If more than one is checked, the form will spit out the following error:
"Run-Time Error '9':
Subscript out of range."
Related
I actually just created my own account here because I was not able to find an answer to the problem I currently have with Excel VBA..
I am creating a Userform with a ListBox, which is being populated via an multi-dimensional array. In certain cases, the ListBox is also repopulated after clicking on the ListBox, for example in a new row of that List (which automatically leads to a click-event).
Referring to the headline of my question, I discovered that when clicking on a new row of the list, both the previous and the selected row are underlined (i.e. "clicked", even though the MultiSelect option of the ListBox is set to "0-fmMultiSelectSingle"). If I request the .ListIndex Value, it just shows me the value of the previous row (not of the now new selected row).
The biggest problem though is the repopulation of the ListBox, which only (!)does not seem to work when clicking (and creating this multiselection) in a new row of the list.
I provided an example code of my situation. Naturally, my code is much more complex so I tried to shorten it up to the main aspects. I would love to send you guys a screenshot of my userform, but the data is sensible (I hope you understand my problem anyways).
The Question: Is there a solution or sufficient workaround to populate the ListBox in a situation, where -by a Click-Event-, you choose a new row in the ListBox?
Thank you in advance for your kind help. I have been trying and searching for quite some time now; maybe somebody has faced a similar problem before!
-Graf_Rasen
Sub ListBox_Click()
intListIndex = Userform.ListBox.ListIndex
'When evaluating intListIndex in combination with clicking in a new row of 'the ListBox (which executes "Sub ListBox_Click"), intListIndex merely returns the value of the previously selected ListIndex
'defaults ListIndex, needs to be done for repopulation
Userform.ListBox.ListIndex = -1
'This does not work when changing into a new row in the ListBox
Userform.ListBox.List() = arr_DataListBox
'Redefines ListIndex
Userform.ListBox.ListIndex = intListIndex
End Sub
You cannot click in a "new" row in a listbox. It may look like you are selecting a new row, but really the event that fires is just the fact that you clicked the listbox. Hence the ListIndex value still shows the previous selection's index (which is actually still the current selection.
I have an Order Sheet which includes a combination of check boxes and also option buttons.
There are 10 rows of 3 option buttons and 1 checkbox. Each row is grouped together.
The check boxes work fine and I am able to refer to them using a variable i which refers to the row number.
I have tried to do the same for the Option buttons. I.e I am trying to refer to the option buttons using variable i. However, I have been unable to find a solution.
This is the code I am using to refer to the check-boxes using variable i. This works fine.
If Sheets("Order Sheet").Shapes("Check Box " & i).ControlFormat.Value = 1 Then
I have used a similar logic to try and refer to the option buttons. However, all variations of my trial and error based on vaguely similar things that I have found online have failed.
If OLEObjects("OptionButton" & i + 30).Object.Value = True Then
The current error with the code snippet above is "Sub or function not defined". Highlighting the OLEObjects line of code.
Any help or direction on where to go with including a variable in the naming of the option buttons would be greatly appreciated.
You need to specify in which worksheet your OLEObjects is:
ThisWorkbook.Worksheets("Sheet1").OLEObjects
Also see the documentation: OLEObjects object.
I have made two active X combo boxes for a list of 220 sites. Before making the active x combo box i have made a sheet that searches through my data and finds the sites that match with what i am typing as i go.
I then used the name manage, refering to the formula in the first cell of the list
=Sheet1!$G$2:INDEX(Sheet1!$G$2:$G$220,COUNTIF(Sheet1!$G$2:$G$220,"?*"))
I have then writen this in the ListFillRange in the properties of my combo box.
It works fine for one, but once i had made the second one and selected the site the first one will no longer let me search through.
I have used the same formulas on both but they originate from different sheets to see if this fixed the problem however that was unsuccessful. (the boxes are on different sheets) When i click on the next sheet after using the box on the first sheet, it still shows part of it as if it is crashing.
The boxes are independent so I'm not sure what to do next as i need to add another 3 on separate sheets.
I am also using this code for each box
Private Sub ComboBox1_Change()
ComboBox1.ListFillRange = "MList"
Me.ComboBox1.DropDown
End Sub
and similar for the other button but with a different range.
There is no need to use VBA for this, the Change Events specifically, if you just want to use and fill the combo boxes with Named Ranges.
The scenario I think you try to do is:
Create Named Ranges that will be the source of your combobox:
Fill the range with your data, select the range, Right Click, Select Define Name and give the range a name. MList in your case I believe.
Create Combobox:
Goto Developer Tab, Insert in your case ActiveX ComboBox, Draw it on your sheet, right click the ComboBox, select properties, find ListFillRange in properties and enter the name of the Named Range you created in step one
Repeat for Combobox 2, with the same or a different Named Range depending on what you try to do
Leave Design Mode
Boths Comboboxes can now be used to type in what you are looking for.
If this is not what you tried to do, please try edit your question and in detail try to explain what you try to do and what you like to accomplish by doing so.
I'm trying to solve this problem along 2 days but I've not found the solution.
I have a lot of listboxes on my excel and each of these listboxes are filled with different data, also I use these listboxes to change some filters at a pivot table using a VB code.
The problem is that after some usage these listboxes stop to work, they stop to get the values that are selected.
I've discovered that two properties, 'Value' and 'Text' has been disappeared after the problem happened. To use the listboxes again I need to close excel and open it again, also if I try to save the workbook, it stop to work.
I've tried to bring some images of it, but I got no reputation yet.
Who can help me?
Updating:
Here is the images:
And here is the code behind the listboxes (all listboxes has a similar code):
If ListFaturaFatura.List(ListFaturaFatura.ListIndex) = "Total" Or _
ListFaturaFatura.List(ListFaturaFatura.ListIndex) = "" Then
Sheets("Fatura_Data").PivotTables("DinamicaFatura1").PivotFields("FATURA").ClearAllFilters
Else
Sheets("Fatura_Data").PivotTables("DinamicaFatura1").PivotFields("FATURA").CurrentPage = _
ListFaturaFatura.List(ListFaturaFatura.ListIndex)
End If
Also, I think that is an excel problem, because when it happens all workbooks are affected with the same problem.
I saw that one of the listboxes was filled with a named range, I just removed the named range and filled the listbox manually, this seems to work fine and didn't give me more problems.
I have a question about freezing certain cells. But first let me explain the situation.
I have made a search box in my excel sheet and when you search for a letter or word; the results show up in cells below the search box. Now I want to freeze those cells, so that wherever I go in my sheet. I can always use the search box and see the results.
The cells for the searchbox and results are B2:B25. Those are the ones I want to freeze. Also the only sheet I want to use this on is the sheet "Reading". On the rest of my sheets I do not use a search function.
So my question(s) is : Do I need to put the code inside a module or on that sheet, and how do I do this?
Now I have tried the following
Range(Cells(2,2), Cells(25, 2)).Select
ActiveWindow(or maybe Reading?).FreezePanes = True
Inside a module. But it did not work and I do not know what else to do.
Any help is much appreciated! Since I am very new to VBA.
Almost there. Problem is that the 'range' and 'cells' needs to be directed to the 'Reading' sheet, like so:
Sheets("Reading").Range(Sheets("Reading").Cells(2,2), Sheets("Reading").Cells(25, 2)).Select
ActiveWindow.FreezePanes = True
but if it always is B2->B25, why not use:
Sheets("Reading").Range("B2:B25").Select
ActiveWindow.FreezePanes = True
This should work. Select is not very desirable, because it is very slow, but in this case, you need to (as far as I know).
EDIT
BTW, you can do this from within a code module or from within a sheet, but if you choose to do it from within a sheet, you cannot select another sheet. So just use the range.
EDIT 2
whoopsy, typo corrected. 'Sheet' should have been 'Sheets'