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!
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 currently have a userform with 3 inputs (DevID, DevSel and LocSel). The Idea is to copy a template sheet based off combobox DevSel, and rename the sheet based on the DevID text box. The issue I have is I have combobox LocSel poplated with the defined range "Headers" (A1:F1) and want to use V&HLooup, or HLookup & Match to return first blank cell under the matched LocSel header, and make it equal DevID.
Private Sub Execute_Btn_Click()
Dim Headers As Range
Dim LocID As Range
Select Case DevSel
Case "HVBKR"
'Make a copy of the selected sheet
Sheets("HVCIRCUITBREAKER_TEMP").Visible = True
Sheets("HVCIRCUITBREAKER_TEMP").Copy After:=Sheets("Master")
Sheets("HVCIRCUITBREAKER_TEMP").Visible = False
ActiveSheet.Name = Me.DevID.Text
'Autopopulate site list
WorksheetFunction.HLookup(LocSel, Range("Headers"), WorksheetFunction.Match(LocSel, Range("Headers").End(xlUp).Offset(1), False), False) = DevID
End Select
Unload Me
Sheets("Master").Activate
End Sub
In the Debugger, LocSel and DevID are populating with the correct information. based on my box selections, but I keep getting Runtime Error1004 - Unable to get the Match property of the worksheetfunction class. Any help would be appreciated!
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
I have input a combobox in an Excel sheet. I want it to work so that the user who does not have access to the VBA can select a value from the dropdown and then the value in another cell will perform a vlookup on this value.
In the first instance I have inserted a box and am trying to set a cell value based on this.
Sub InsertComboBox()
#inserts dropdown box on the front page and sets the values as the list of DMA from the pipe_totals sheet
#this should be the most complete list so will not change dependant on asset
Dim arearange As Range
Set arearange = Sheets("pipe_totals").Range("a:a")
lastrowi = Application.WorksheetFunction.CountA(arearange)
Sheets("front page").Activate
With Range("f5:g5")
Set Combo = ActiveSheet.DropDowns.Add(.Left, .Top, .Width, .Height)
End With
Combo.List() = Sheets("pipe_totals").Range("A2:A" & lastrowi).Value
.Range("k9").Value = Combo.Value 'only works on current combobox value which is 0
End Sub
Is there a way I can set this so the vlookup is dynamic depending on the users selection?
In this example, just set the right combo name. It should be ok, provided that your combobox lists values from "Range("A2:A" & lastrowi)" as you mention above.
Sub "comboname"_Change()
Dim list_val As Long
list_val = Worksheets("front page").Shapes("comboname").ControlFormat.Value
Range("K9") = Worksheets("pipe_totals").Cells((list_val + 1), 1)
End Sub
Sub test()
Dim z As Shape
For Each z In Worksheets("front page").Shapes
Debug.Print z.Name
Next z
End Sub
As far as I understand, you want that everytime the combobox value changes, cell K9 will have the same value also. Is that right? If this the case, then right click on the combobox and select "Assign Macro". Then select "Create". Then inside the sub created, which should look like this:
Sub "comboname"_Change()
End Sub
You should also paste the final code line.
.Range("k9").Value = Combo.Value
Doing so, means you want that line of code executed every time the combobox value changes.
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.