Hi,
I have a spreadsheet containing a list of products and Promotions. Each product can have multiple promotions, and one promotion can be found in different products (many-to-many). This spreadsheet is updated constantly.
I created a UserForm with a dropdown list containing unique products. For that, I created a second spreadsheet and used "=UNIQUE(Price[[#All],[Product ID]]&" - "&Price[[#All],[Product_Description]])". I added this range to the Name Manager and populated the dropdown. I did it like that because the products change a lot, the dropdown needs to reflect that, and I was out of ideas.
The initial idea for this was to use VBA to insert a new line after the last row of the product from the dropdown list, something like:
where the italic and underlined line is the inserted line.
But the promotions have a very different type of order that needs to be respected, so I was asked if I could create a second dropdown where they pick the product and the promotion, this way if they pick Product A, and Promotion 1, the line will be inserted after Promotion 1, and not at the end, just like that:
My solution was to just create another UNIQUE range for the promotion, save it under the name manager and use it to add a new line. But then, when I pick Product A and I go to the second dropdown to pick promotion, I still see "Promotion 3" because they are not tied together.
What I want to do now is to have the promotions dropdown tied to the products dropdown, so it would only show the promotions for the product they selected before, instead of showing all promotions from every product.
This is how my code looks like now:
Any ideas on how make the code better is also welcome.
I have a spreadsheet containing a list of products and Promotions. Each product can have multiple promotions, and one promotion can be found in different products (many-to-many). This spreadsheet is updated constantly.
I created a UserForm with a dropdown list containing unique products. For that, I created a second spreadsheet and used "=UNIQUE(Price[[#All],[Product ID]]&" - "&Price[[#All],[Product_Description]])". I added this range to the Name Manager and populated the dropdown. I did it like that because the products change a lot, the dropdown needs to reflect that, and I was out of ideas.
The initial idea for this was to use VBA to insert a new line after the last row of the product from the dropdown list, something like:
Product | Promotion |
Product A | Promotion 1 |
Product A | Promotion 2 |
Product A | New Promotion |
Product B | Promotion 3 |
But the promotions have a very different type of order that needs to be respected, so I was asked if I could create a second dropdown where they pick the product and the promotion, this way if they pick Product A, and Promotion 1, the line will be inserted after Promotion 1, and not at the end, just like that:
Product | Promotion |
Product A | Promotion 1 |
Product A | New Promotion |
Product A | Promotion 2 |
Product B | Promotion 3 |
My solution was to just create another UNIQUE range for the promotion, save it under the name manager and use it to add a new line. But then, when I pick Product A and I go to the second dropdown to pick promotion, I still see "Promotion 3" because they are not tied together.
What I want to do now is to have the promotions dropdown tied to the products dropdown, so it would only show the promotions for the product they selected before, instead of showing all promotions from every product.
This is how my code looks like now:
VBA Code:
Private Sub UserForm_Initialize()
Dim cProd As Range
Dim cPromo As Range
Dim ws As Worksheet
Dim i As Long
Set ws = ThisWorkbook.Worksheets("DO NOT DELETE")
For Each cProd In ws.Range("ProdList")
With Me.dropProd
.AddItem cProd.Value
End With
Next cProd
For Each cPromo In ws.Range("PromoList")
With Me.dropPromo
.AddItem cPromo.Value
End With
Next cPromo
Me.dropProd.SetFocus
End Sub
------------------------------------------------------------------------------------------------------
Private Sub dropProd_Change()
Dim ws As Worksheet
Dim i As Long
Set ws = ThisWorkbook.Worksheets("DO NOT DELETE")
If Not IsArrow Then
With Me.dropProd
.List = ws.Range("ProdList").Value
.ListRows = Application.WorksheetFunction.Min(6, .ListCount)
.DropDown
If Len(.Text) Then
For i = .ListCount - 1 To 0 Step -1
If InStr(1, .List(i), .Text, vbTextCompare) = 0 Then .RemoveItem i
Next
.DropDown
End If
End With
End If
End Sub
------------------------------------------------------------------------------------------------------
Private Sub dropPromo_Change()
Dim ws As Worksheet
Dim i As Long
Set ws = ThisWorkbook.Worksheets("DO NOT DELETE")
If Not IsArrow Then
With Me.dropPromo
.List = ws.Range("PromoList").Value
.ListRows = Application.WorksheetFunction.Min(6, .ListCount)
.DropDown
If Len(.Text) Then
For i = .ListCount - 1 To 0 Step -1
If InStr(1, .List(i), .Text, vbTextCompare) = 0 Then .RemoveItem i
Next
.DropDown
End If
End With
End If
End Sub
------------------------------------------------------------------------------------------------------
Private Sub cmdAdd_Click()
Dim LastRow As Long, lProd As Long, lPromo As Long
Dim ProdCell As Range, PromoCell As Range
Dim wsTable As Worksheet
Set wsTable = ThisWorkbook.Worksheets("Table View")
Application.ScreenUpdating = False
LastRow = wsTable.Cells(Rows.Count, 1).End(-4162).Row
lProd = Me.dropProd.ListIndex
lPromo = Me.dropPromo.ListIndex
'Check for a Product Description
If Trim(Me.dropProd.Value) = "" Then
Me.dropProd.SetFocus
MsgBox "Please, select a product"
Exit Sub
End If
If Trim(Me.dropPromo.Value) = "" Then
Me.dropPromo.SetFocus
MsgBox "Please, select a promotion"
Exit Sub
End If
Set ProdCell = wsTable.Range("B13:B" & LastRow).Find(CInt(Trim(Split(Me.dropProd.Value, " - ")(0))))
For Each PromoCell In ProdCell.Offset(0, 7)
If PromoCell.Value = Me.dropPromo.Value Then
'Add Row
wsTable.Rows(PromoCell.Row).Insert
'Copy data
wsTable.Rows(PromoCell.Row).Copy Rows(PromoCell.Row - 1)
wsTable.Cells(PromoCell.Row, "H").Formula = ""
wsTable.Cells(PromoCell.Row, "I").Formula = "New Promo"
wsTable.Cells(PromoCell.Row, "J").Formula = ""
Application.ScreenUpdating = True
MsgBox ("A new promotion was successfully added!")
Exit Sub
Else
MsgBox "There's no Promo with this name for the selected product!"
End If
Next PromoCell
'Clear Prompt Box
Me.dropProd.Value = ""
Application.ScreenUpdating = True
End Sub
Any ideas on how make the code better is also welcome.