I have a listbox within a userform with a few rows of filtered data. I would like to be able to select one of the rows and have it appear in another listbox (just the values within that selected row, none of the others)
My current code is just:
Private Sub SelectHousingList_Click()
HousingList.Text = SelectHousingList.Selected(Row)
End Sub
With 'HousingList' being the listbox that I'd like the values to move to.
And 'SelectHousingList' being the rows of filtered data.
Previously in this code I've used code similar to this to select from a list of values (but not with a whole row of values).
Private Sub MaterialList_Click()
SelectedMaterialText.Value = MaterialList.Text
Worksheets("FSC PSC PFC").Range("D4").Value = SelectedMaterialText.Value
End Sub
The second line of code allows for the selected item in the list to be copied to a textbox.
If you need more of my code I can supply you with it.
This may be a simple question but I can't seem to find the answer anywhere or figure out code that allows it to happen.
Thank you in advance!
If I understand your requirements, this should do what you need for a single column:
Private Sub SelectHousingList_Click()
HousingList.AddItem SelectHousingList.Value
End Sub
If there are 2 columns, then this:
Private Sub SelectHousingList_Click()
HousingList.AddItem SelectHousingList.List(SelectHousingList.ListIndex)
HousingList.List(HousingList.ListCount - 1, 1) = SelectHousingList.List(SelectHousingList.ListIndex, 1)
End Sub
You'll need to add additional lines for every column beyond 2, changing the index for each one.
This code retrieves in an array (or string) all the columns values on the selected row. It fills the second listbox with as many columns first one has:
Private Sub SelectHousingList_Click()
Dim arrRow() As Variant, i As Long
ReDim arrRow(Me.SelectHousingList.ColumnCount)
For i = 0 To SelectHousingList.ColumnCount
arrRow(i) = Me.SelectHousingList.List(i, Me.SelectHousingList.ListIndex)
Next i
Debug.Print Join(arrRow, ", ")
With Me.HousingList
.ColumnCount = Me.SelectHousingList.ColumnCount
.AddItem arrRow(0)
For i = 1 To UBound(arrRow)
.List(.ListCount - 1, i) = arrRow(i)
Next i
End With
End Sub
Related
I have a list of names in a sheet. I set these names as my RowSource for a combobox on a useform.
There are two Comboboxes involved. One starts full, the other starts empty.
I want when I click on a name from the first (full) combobox, said name to be added to the other combobox, and removed from the original combobox (and vice versa eventually).
I can't remove anything with RemoveItem.
I went the 'Menu.ListeAjoutAg.ListIndex' way to get my current selection's index ('Menu' is the UserForm and 'ListeAjoutAg' is the combobox), but it did not work.
Tried inputting through a variable I created real quick, 'b', but same result. No index number works. I checked and I only feed the function integers (0, 1, 3, 4...) that are correct and/or well within the scope of my list (about 45 names).
Private Sub ListeAjoutAg_Change()
a = Menu.ListeAjoutAg.Text
b = Menu.ListeAjoutAg.ListIndex
Menu.ListeRetirer.AddItem (a) ' goes fine till there
Menu.ListeAjoutAg.RemoveItem (b) 'and here it goes wrong
Menu.ListeRetirer.Enabled = True
Menu.ListeRetirer.Visible = True
End Sub
As already mentioned: You can't add or remove items from a Listbox if you have set the Rowsource property.
However, it is rather easy to fill a Listbox from a range - simply copy the cell values into an array and assign the array as List. See for example VBA Excel Populate ListBox with multiple columns
Put the following routine into your form and call it from the form Activate event.
Private Sub fillListBox(r As Range)
Me.ListeAjoutAg.Clear
Me.ListeAjoutAg.ColumnCount = r.Columns.Count
Dim data
data = r.Value
Me.ListeAjoutAg.List = data
End Sub
Private Sub UserForm_Activate()
Dim r As Range
' Replace this with the range where your data is stored.
Set r = ThisWorkbook.Sheets(1).Range("A2:C10")
fillListBox r
End Sub
I have a userform within a userform (userform-ception) where I implement a search function for part list in Row B. Prior to searching I want to check for duplicates, which triggers a second userform when true. I am having trouble populating userform2's list box when it initialises. My logic is
If duplicate count > 1, filter by column B for that search criteria
Open second userform
From the current visible cells, grab the first 7 columns and populate a list box with this (Using visible cells as array)
However when I try to open the userform it turns out blank so I am wondering if anyone can shed some light on this.
This is in my Search Function
Dim Dupl As Integer
Dupl = Application.WorksheetFunction.CountIf(Range("B:B"), PartNumberSearch.Text)
If Dupl > 1 Then
With ActiveSheet.Range("A1:M1")
.AutoFilter Field:=2, Criteria1:=PartNumberSearch
End With
UserForm2.Show
'Exiting the sub here so that when userform 2 is used to disambiguate the part it can continue to search using PartID instead
Exit Sub
This is in my userform2_initialise() function
Private Sub UserForm2_Initialize()
DupList.ColumnWidths = "35;50;60;25;10;20;40"
'Checks last row of the list
DupRow = ActiveSheet.Range("A65536").End(xlUp).Row
'Puts all the result into the ResultsList
DupList.List = Range("A2:G" & DupRow).Value
End Sub
A sample snippet of my table
Any help is appreciated. Thanks!
I have a Userform in which I want the user to select a word in each of two listbox and then save them in a sheet. The user can also (in theory) leave the pre-selected word. Here's the code I have written :
Private Sub UserForm_Activate ()
With ListBox1 'This list is about the stake
.AddItem "Essential"
.AddItem "Important"
.AddItem "Not interesting"
End With
'then I try to initialize the value of the stake.
ListBox1.Value = "Important"
With ListBox2 'This is a second list, about priority
.AddItem "Auto"
.AddItem "Yes"
.AddItem "No"
End With
'then I try to initialize the value of the priority
Listbox2.Value="Yes"
End Sub ()
But my problem is that, even though the two list seem to have been initialize correctly (correct word highlighted in the list when I run the UserForm), I can't extract the value of one of the list. When I run the following code :
Private Sub CommandButton2_Click()
Feuil1.Cells(1,1)=Listbox1.Value
Feuil1.Cells(1,2)=Listbox2.Value
End sub ()
Excel is able to extract the value of Listbox2 (Priority) : "Yes" but not the value of Listbox1 (Stake) : "Important". And I don't get why the code would work for one but not the other!
One more element : if I manually select a word in the list, then Excel is able to give me the value of both listbox.
Any clue?
How to get the current value of a ListBox
Seems that the .Value property recognizes the correct list row, but doesn't react to the second listbox unless it gets focus or is activated manually.
So a brute (and not recommendable) work around would be to set focus each time you have to get the current value of the 2nd listbox, too.
(This seems to be nerve-racking btw and ressembles to a certain extent to permanently selecting or activating cells instead of recommended direct referencing fully qualified ranges.)
'...
Me.ListBox2.SetFocus
Feuil1.Cells(1, 2) = Me.ListBox2.Value
You are on the sure side, however using the listboxes' .List property.
.ListIndex as first argument indicates the current "row" by a zero-based index (0 equals row 1, 1 the 2nd one row, etc.);
the 2nd argument 0 indicates the row index (i.e. column 1; btw the only one here).
Private Sub CommandButton2_Click()
Feuil1.Range("A1:B1") = vbNullString
With Me.ListBox1
If .Listindex >-1 then Feuil1.Cells(1, 1) = .List(.ListIndex, 0)
End With
With Me.ListBox2
If .Listindex >-1 then Feuil1.Cells(1, 2) = .List(.ListIndex, 0)
End With
Try this, it works well for me.
Private Sub CommandButton2_Click()
Feuil1.Cells(1,1)=Listbox1.Value
Feuil1.Cells(1,2)=Listbox2.Value
Unload Me
End sub
I have a UserForm in VBA with two ListBoxes. Each box gets filled during UserForm_Initialize(). I use a loop for this, so that only text items are added to the UserForm and empty cells are ignored:
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 8
If Sheets("Data").Cells(2 + i, 10) <> "" Then
ListBox1.AddItem Sheets("Data").Cells(2 + i, 10)
End If
Next i
For i = 1 To 7
If Sheets("Data").Cells(11 + i, 10) <> "" Then
ListBox2.AddItem Sheets("Data").Cells(11 + i, 10)
End If
Next i
End Sub
The user can either (single-) select one entry in each ListBox or alternatively use a button to load/select the previous values from two cells in a sheet:
Private Sub CommandButtonLoad_Click()
ListBox1.Value = Sheets("Input").Cells(15, 1)
ListBox2.Value = Sheets("Input").Cells(17, 1)
End Sub
Another button will then write the selected ListBox values in the (same) cells that contained the previous values.
Private Sub CommandButtonConfirm_Click()
Sheets("Input").Cells(15, 1) = ListBox1.Value
Sheets("Input").Cells(17, 1) = ListBox2.Value
End Sub
The problem:
As long as I select the ListBox items by hand in the UserForm, everything works as intended an the third code will re-write the correct values (which are text by the way) in the cells (15, 1) and (17, 1). However, if I use the button (code 2) to fill the ListBoxes with previous values from the sheet, only one ListBox gets assigned with the correct value (output "Item XYZ"). The other one gets an empty value (output: "").
The issue vanishes when I use tab to select the ListBoxes in the UserForm. I guess this is equal to selecting the item with the mouse cursor.
I don't know how to solve this problem. Especially since one of the two ListBoxes works as intended. Anyone know the cause for this?
Edit: To clarify: Using the "Load" button, both values get correctly selected in the ListBoxes. But the assigned value of one ListBox ist empty (tested using msgbox ListBox1.value and msgbox ListBox2.value).
One more thing: If I add ListBox1.SetFocus and ListBox2.SetFocus to CommandButtonLoad the problem gets solved. But I still don't know what caused it.
Try:
Private Sub CommandButtonConfirm_Click()
Sheets("Data").Cells(15, 1) = ListBox1.List(ListBox1.ListIndex)
Sheets("Data").Cells(17, 1) = ListBox2.List(ListBox2.ListIndex)
End Sub
Good morning,
I am in yet another rut and need some help. I have created a user form that allows a user to delete an entire rows worth of data on a second sheet (rawdata). Everything works fine using the code below, however the combo box ONLY shows the row number. I am in desperate need of changing the column so it will show the project names of the rows that need to be deleted.
Example:
Row: Project
1 Alpha
2 Beta
I would like the combo box to show Alfa and Beta and have the user be able to select the row they would like to delete based on that criteria.
The code below unhides and then hides the sheet that I want this deletion to occur on. This was done with purpose.
Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim lRw As Long
ActiveWorkbook.Sheets("RAWDATA").Visible = xlSheetVisible
'get the row number. add 1 because ListIndex starts at zero
lRw = Me.ComboBox1.ListIndex + 1
ActiveWorkbook.Sheets("RAWDATA").Select
Cells(lRw, 1).EntireRow.Delete
ActiveWorkbook.Sheets("RAWDATA").Visible = xlSheetHidden
End Sub
Private Sub CommandButton1_Click()
End Sub
Private Sub UserForm_Initialize()
'assumes data starts in A1 and has a header row
Me.ComboBox1.List = ActiveWorkbook.Sheets("RAWDATA").Cells(1, 1).CurrentRegion.Offset(1).Value
End Sub
Thanks for the help!
Change .Cells(1, 1) to .Cells(1, 2)
The Cells() method gives the code co-ordinates to a specific range using the row and the column number like so:
Cells(r, c)
so in your original code, the .Cells(1, 1) points to "A1" and then uses .CurrentRegion to get all cells within the region of A1.
By replacing the column number and using .Cells(1, 2) we tell it to look at "B1" instead - therefore shifting the column over to the right.
EDIT:
You could apply this logic to the Offset(r, c) function to shift the returned value over by 1 column - so:
.Cells(1, 1).CurrentRegion.Offset(1, 1)
This will more than likely be the culprit as the .Cells() method will point to a specific cell, but the .CurrentRegion() method will return the same result regardless unless we offset it.