Jump to an item in Listbox - vba

One question, it is possible to jump somehow to a certain index in a specif listbox as in the image below?
I already tried the following code
Listbox.ListIndex = index
But it drives me to the error You've used the ListIndex property incorrectly
One property of my list that might be important to mention.
Row source type : Table/Query
Thank you in advance.

Try ListBox.Selected(index) = True. If it is a multiselect listbox, you also need to loop through the other elements and unselect them in the same way.

Create a standard module with the code
Sub Main()
UserForm1.Show
Unload UserForm1
End Sub
Insert a userform and visually do something like
Go into the userform code and add
Private Sub CommandButton1_Click()
Dim v As Long
For v = 0 To ListBox1.ListCount - 1
If TextBox1 = ListBox1.List(v) Then
ListBox1.Selected(v) = True
End If
Next v
End Sub
Private Sub UserForm_Initialize()
With ListBox1
.AddItem ("text1")
.AddItem ("text2")
.AddItem ("text3")
End With
End Sub
Run Main Macro
Type in the box : text2
The text2 will be selected in the list

Related

Get the values from a Userform to a Module inside an Excel Macro

I have moderate knowledge in SQL and able to manage simple queries...
Basic intention was to add a drop-down on a Excel macro I created (instead of a text box where user give whatever values he wants) but after so much research I came to know that only a Userform can help me to satisfy my requirement, Now I want to know how a value selected under a Userform, can be made available under a Module, so that I can use those values selected from a drop down for any other purpose. I have created a sample user-form with below codes
For USER-FORM
Private Sub cmdAdd_Click()
Call Try1
Unload Me
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "Please use the Close Form button!"
End If
End Sub
Private Sub UserForm_Initialize()
Dim cPart As Range
Dim cLoc1 As Range
Dim ws As Worksheet
Set ws = Worksheets("LookupLists")
For Each cPart In ws.Range("PartIDList")
With Me.cboPart
.AddItem cPart.Value
.List(.ListCount - 1, 1) = cPart.Offset(0, 1).Value
End With
Next cPart
For Each cLoc1 In ws.Range("LocationList")
With Me.cboLocation
.AddItem "Cluster1"
.AddItem "Cluster2"
.AddItem "Location 3"
End With
Next cLoc1
Me.txtDate.Value = Format(Date, "Medium Date")
Me.txtQty.Value = 1
Me.cboPart.SetFocus
End Sub
For Module
Sub Rectangle1_Click()
frmPartLoc.Show
End Sub
Sub Try1()
Dim Location
Location = cboLocation.Value
Part = cboPart.Text
MsgBox Location.Value
MsgBox Part.Text
End Sub
actually this code have some issue here
"Location = cboLocation.Value Part = cboPart.Text"
I guess its possible to get the values on a MODULE that we selected using a USER-FORM. But I am having some mistake with my code, or understand this stuff little differently. Please help me to correct my code.
I take help from a website to create this User form (not using the complete code) if you want to please use this link to get the zip file of the same or you can browse there using below link
http://www.contextures.com/xlUserForm01.html
Thanks in advance for helping me.
Thanks to Tigeravatar who help to find the solution.
We have to mention the USER-FORM name as well prior to combobox name from which the vale we are populating from USER-FORM.
Please find the Corrected code below
Sub Try1()
Dim Location
Location = frmPartLoc.cboLocation.Value
Part = frmPartLoc.cboPart.Value
MsgBox Location
MsgBox Part
End Sub
Thanks again for all the comments

filter a ListBox with a Combobox on VBA

I'm working on a Userform and I have weeks trying to develop a code to filter a listbox depending of the value of a combobox.
The closest I have done is make a commandbutton to filter the table where the listbox feeds but it doesn't refresh the listbox.
I have seen on forums people doing things like I want but I have tried all of them with no results.
Private Sub CommandButton1_Click()
If ComboBox1.Value <> "" Then ActiveSheet.ListObjects("Tabla2").Range.AutoFilter field:=2, Criteria1:=ComboBox1.Value
End Sub
Private Sub CommandButton2_Click()
ActiveSheet.ListObjects("Tabla2").Range.AutoFilter field:=2
End Sub
Private Sub CommandButton3_Click()
Unload UserForm2
UserForm3.Show
End Sub
Private Sub UserForm_Initialize()
For i = 2 To 30
ComboBox1.AddItem Sheets("Proyectos - J.P.").Range("A" & i).Value
Next i
End Sub
04-05-2017
Workbook Link
https://drive.google.com/open?id=0B4B7v0UZxizCYnY2bVNTNURyLVU
In the WorkBook you will see 3 userforms, the Userform1 is Ok.
The userform2 have the Combobox (Proyect Code) and the ListBox i want to filter.
The userform3 is not ready yet, because i need to have on 100% the Userform2 to take a decision.
Hope it helps.
Regards
I have already faced a similar situation, but instead of a ComboBox, I needed to filter the ListBox based on the selection of other ListBox and the selection of an Option. The way I found to meet my need was to use a Pivot Table in a hidden sheet. It worked fine for me, bit not all data can be rearranged in a Pivot Table, so I will understand if my suggestion does not work for you.
First Step: set up a Pivot Table with your data source to be used in your ListBox. Pull the fields you want to filter in the Filters area. Create a dinamic named range with your data, like in the image:
=OFFSET('Certificates Pivot'!$A$5;0;0;COUNTA('Certificates Pivot'!$A$5:$A$50);2)
Second Step: create your UserForm. I set up 2 ComboBoxes as filters to the ListBox, but you can remove or add as many as you can, you'll just need to adjust your code. I also named the ranges that will be available in the list options of the ComboBoxes. So we'll have:
The UserForm's code will be something like this:
Private Sub UserForm_Initialize()
ComboBox1.RowSource = "CustomerID"
ComboBox2.RowSource = "SalesOrg"
With ListBox1
.RowSource = "Consult_List"
.ColumnCount = 2
.ColumnWidths = "60;90"
End With
End Sub
Private Sub ComboBox1_Change()
Dim SelectedCID As String
Dim SelectedSO As String
SelectedCID = ComboBox1.Text
SelectedSO = ComboBox2.Text
With Sheets("Certificates Pivot").PivotTables("Certificates_PivotTable")
.ClearAllFilters
If Not SelectedCID = "" Then .PivotFields("Customer ID").CurrentPage = SelectedCID
If Not SelectedSO = "" Then .PivotFields("Sales Org.").CurrentPage = SelectedSO
End With
ListBox1.RowSource = "Consult_List"
End Sub
Private Sub ComboBox2_Change()
Call ComboBox1_Change
End Sub
You can hide the sheet where your Pivot Table is, so when you filter it through your UserForm, it will update in the background. You should also set up your Pivot Table to update its cache to capture new inputs in your data source.
I hope it works for you! Let me know what were the results.

binding buttom to get strings from multiselect listbox in VBA

do anybody know how to get the strings from a multiselect listbox if the user click on a button in excel(VBA)?
And if i click on the button it take the strings into specific cells like D18,D19 and so on..
here is a little overview.
http://fs1.directupload.net/images/150625/o8xhqsll.jpg
I think the best bet is to take a look at this code, and try it out. Come back if you have more questions.
Private Sub UserForm_Initialize()
Me.ComboBox1.AddItem "a"
Me.ComboBox1.AddItem "b"
Me.ComboBox1.AddItem "c"
Me.ComboBox1.AddItem "d"
End Sub
Private Sub CommandButton1_Click()
Dim intComboItem As Integer
For intComboItem = 0 To Me.ComboBox1.ListCount - 1
If Me.ComboBox1.List(intComboItem).Selected = True then
''''Do something
End If
Next
End Sub

VBA combobox additem and getting a runtime error 70

I have created a userform that has two buttons on it. One is called CmdCon6 and the other is called CmdLbs6. When clicked, they are suppose to close the current userform, pull up another userform, and pull values from the 4th column in sheet18 and add them to a combobox named x48 (both of the new userforms have a combobox named x48)in the new userform. The range of cell values to be added to the combobox x48 will flucuate, but never exceed 20 (hence why I added a loop). Everything works great and does what it is suppose to do when I click the CmdCon6 button, but when I click the CmdLbs button, it gives me a run-time error '70' Permission denied and highlights the 20th line of code (line between the If and end if in the Sub CmdLbs_Click()).
I have tried to change the name of the combobox x48 in the frmInputLbs6 userform and keep it as x48 for the frmInputCon6 userform, but I still received the same error.
Any suggestions to fix this issue? I'm stumped, and can't think of a way around it. Thanks in advance!
Private Sub CmdCon6_Click()
Unload Me
For x = 1 To 20
If Sheet18.Cells(x, 4).Value <> "" Then
frmInputCon6.x48.AddItem Sheet18.Cells(x, 4)
End If
Next x
frmInputCon6.Show
End Sub
Private Sub CmdLbs6_Click()
Unload Me
For x = 1 To 20
If Sheet18.Cells(x, 4).Value <> "" Then
frmInputLbs6.x48.AddItem Sheet18.Cells(x, 4)
End If
Next x
frmInputLbs6.Show
End Sub
Controls on UserForms are private by default. You need to access them through the Controls collection:
Private Sub CmdLbs6_Click()
Unload Me
For x = 1 To 20
If Sheet18.Cells(x, 4).Value <> "" Then
frmInputLbs6.Controls("x48").AddItem Sheet18.Cells(x, 4)
End If
Next x
frmInputLbs6.Show
End Sub
I'd also note that although you mention that "they are suppose to close the current userform", this isn't what happens. Your forms also aren't actually being fully unloaded until the other form is closed. The .Show method defaults to modal so in the code above frmInputCon6 doesn't fully unload until after frmInputLbs6 is closed.
Just something to keep in mind, because it really messes up your event stack. You can see the results by with this simple test code. Add UserForm1 and UserForm2, and put a button on each of them and the following code:
UserForm1:
Private Sub CommandButton1_Click()
Unload Me
UserForm2.Show
End Sub '<--Put a breakpoint here.
Private Sub UserForm_Terminate()
Debug.Print "UserForm1 closed"
End Sub
UserForm2:
Private Sub CommandButton1_Click()
Unload Me
UserForm1.Show
End Sub '<--Put a breakpoint here.
Private Sub UserForm_Terminate()
Debug.Print "UserForm2 closed"
End Sub
Put a breakpoint on the End Subs of each Click() event, fire up one of the forms and hit the buttons to hop back and forth a few times. Then close one of the forms and count how many times you hit the breakpoints before you actually exit.

ComboBox, added items not showing up in list when executing code

I got a small problem, I'm adding items to my combobox in my userform but they don't show up in the combobox when I execute the code...
I'm trying the following code
Private Sub period_input_Change()
With period_input
.AddItem "Apple"
.AddItem "Sugar"
End With
End Sub
Also tried this one
Private Sub box_action()
With Sheets(1).period_input
.AddItem "hello"
.AddItem "mongoasd"
End With
End Sub
It simply shows up as an empty column ( no string is there ). Anyone knows the issue?
You probably want to add the items during the initialization phase of the form:
Private Sub UserForm_Initialize()
With Me.period_input
.AddItem "hello"
.AddItem "mongoasd"
End With
End Sub
Otherwise, I see no problem with the code.