jnd082505

New Member
Joined
Aug 9, 2019
Messages
1
Good afternoon! I'm still very very new to VBA and user forms in Excel so I'm hoping to get some help. I'd like to create a user form that would allow for the selection of 2 independent defined variables and produce all possible results. Example: I have a remote that can use multiple battery types. I want to select a technology type ("Remote" in this case), a number of hours I'd like the remote to last, and click a button to display all battery options that will satisfy my requirements. Below is my chart. I would greatly appreciate any help! Thank you!

CR123AAAAA LithiumAA Lithium
1 cell2 cell +4 cell3 cell3 cell +3 cell3 cell +6 cell1 cell3 cell4 cell
Watch8518010065130155210
Remote200105180240
Calculator105210420250250110110220120365480

<tbody>
</tbody>
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
So here is what I've done. First off, I used PowerQuery to transform your table into a proper data set. The following M code can be used for the transformations.

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    TypeI = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"CR123", type any}, {"Column2", type any}, {"Column3", type any}, {"AA", type any}, {"Column4", type any}, {"AAA Lithium", type any}, {"Column5", type any}, {"Column6", type any}, {"AA Lithium", type any}, {"Column7", type any}, {"Column8", type any}}),
    Demote = Table.DemoteHeaders(TypeI),
    TypeII = Table.TransformColumnTypes(Demote,{{"Column1", type text}, {"Column2", type any}, {"Column3", type any}, {"Column4", type any}, {"Column5", type any}, {"Column6", type any}, {"Column7", type any}, {"Column8", type any}, {"Column9", type any}, {"Column10", type any}, {"Column11", type any}, {"Column12", type any}}),
    Transposed = Table.Transpose(TypeII),
    AddedCustom = Table.AddColumn(Transposed, "Custom", each if Text.Contains([Column1],"Column") then null else [Column1]),
    FilledDown = Table.FillDown(AddedCustom,{"Custom"}),
    RemovedColumns = Table.RemoveColumns(FilledDown,{"Column1"}),
    ReorderedColumns = Table.ReorderColumns(RemovedColumns,{"Custom", "Column2", "Column3", "Column4", "Column5"}),
    ReplacedValue = Table.ReplaceValue(ReorderedColumns,null,"Cell",Replacer.ReplaceValue,{"Column2"}),
    PromotedHeaders = Table.PromoteHeaders(ReplacedValue, [PromoteAllScalars=true]),
    TypeIII = Table.TransformColumnTypes(PromotedHeaders,{{"Column1", type text}, {"Cell", type text}, {"Watch", Int64.Type}, {"Remote", Int64.Type}, {"Calculator", Int64.Type}}),
    Unpivot = Table.UnpivotOtherColumns(TypeIII, {"Column1", "Watch", "Remote", "Calculator"}, "Attribute", "Value"),
    RemovedColumnsII = Table.RemoveColumns(Unpivot,{"Attribute"}),
    ReorderedColumnsII = Table.ReorderColumns(RemovedColumnsII,{"Watch", "Remote", "Calculator", "Column1", "Value"}),
    UnpivotedII = Table.UnpivotOtherColumns(ReorderedColumnsII, {"Column1", "Value"}, "Attribute", "Value.1"),
    ReorderedColumnsIII = Table.ReorderColumns(UnpivotedII,{"Attribute", "Column1", "Value", "Value.1"}),
    RenamedColumns = Table.RenameColumns(ReorderedColumnsIII,{{"Attribute", "Item"}, {"Column1", "Battery"}, {"Value", "Cell"}, {"Value.1", "Life"}})
in
    RenamedColumns

You will need to name the query 'Raw', because that name will be referenced in the VBA later on. It will now look like this.

ABCD
1ItemBatteryCellLife
2CalculatorAA3 cell250
3CalculatorAA3 cell +250
4CalculatorAA Lithium1 cell120
5CalculatorAA Lithium3 cell365
6CalculatorAA Lithium4 cell480
7CalculatorAAA Lithium3 cell110
8CalculatorAAA Lithium3 cell +110
9CalculatorAAA Lithium6 cell220
10CalculatorCR1231 cell105
11CalculatorCR1232 cell +210
12CalculatorCR1234 cell420
13RemoteAA Lithium3 cell180
14RemoteAA Lithium4 cell240
15RemoteAAA Lithium6 cell105
16RemoteCR1234 cell200
17WatchAA3 cell +100
18WatchAA Lithium3 cell155
19WatchAA Lithium4 cell210
20WatchAAA Lithium3 cell +65
21WatchAAA Lithium6 cell130
22WatchCR1232 cell +85
23WatchCR1234 cell180

<caption>LEGO HTML</caption><colgroup><col style="width: 54px"><col width="128"><col width="139"><col width="113"><col width="110"></colgroup><tbody>
</tbody>


I loaded this transformed table to a new worksheet 'Worksheet2'.

Then I made a UserForm with a combobox for watch, calc, remote. 2 textboxes for battery life. 1 for the low hours and 1 for the high. So, for example, I select Calculator from the combobox. Then say I want batteries that will last from 200-400 hours, 200 will go in textbox1, 400 will go in textbox2.

Then I have a button. The button code will filter the transformed table to show the data that matches the criteria from the combobox and textboxes.

The code for the UserForm Initialize Event:
Code:
Private Sub UserForm_Initialize()
Dim AL As Object: Set AL = CreateObject("System.Collections.ArrayList")
Dim AR() As Variant: AR = Sheets("Sheet2").ListObjects("Raw").DataBodyRange.Value
Dim Product() As Variant


For i = LBound(AR) To UBound(AR)
    If Not AL.contains(AR(i, 1)) Then AL.Add AR(i, 1)
Next i


Me.ComboBox1.List = AL.toarray
End Sub

The code for the command button:
Code:
Dim Product As String: Product = Me.ComboBox1.Value
Dim tFrom As Single: tFrom = Me.TextBox1.Value
Dim tTo As Single: tTo = Me.TextBox2.Value
Dim LO As ListObject: Set LO = Sheets("Sheet2").ListObjects("Raw")
Dim AR() As Variant


LO.Range.AutoFilter 1, Product, xlAnd
LO.Range.AutoFilter 4, ">=100", xlAnd, "<=190"
 
Upvote 0
small change

Code:
Private Sub CommandButton1_Click()Dim Product As String: Product = Me.ComboBox1.Value
Dim tFrom As Single: tFrom = Me.TextBox1.Value
Dim tTo As Single: tTo = Me.TextBox2.Value
Dim LO As ListObject: Set LO = Sheets("Sheet2").ListObjects("Raw")
Dim AR() As Variant


LO.Range.AutoFilter 1, Product, xlAnd
[COLOR=#0000ff][B]LO.Range.AutoFilter 4, ">=" & tFrom, xlAnd, "<=" & tTo[/B][/COLOR]


End Sub
 
Upvote 0
Cleaned up the M code a bit too. Cut down the steps by a third.

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Demote = Table.DemoteHeaders(Source),
    Transposed = Table.Transpose(Demote),
    AddedCustom = Table.AddColumn(Transposed, "Custom", each if Text.Contains([Column1],"Column") then null else [Column1]),
    FilledDown = Table.FillDown(AddedCustom,{"Custom"}),
    RemovedColumns = Table.RemoveColumns(FilledDown,{"Column1"}),
    ReplacedValue = Table.ReplaceValue(RemovedColumns,null,"Cell",Replacer.ReplaceValue,{"Column2"}),
    PromotedHeaders = Table.PromoteHeaders(ReplacedValue, [PromoteAllScalars=true]),
    Unpivot = Table.UnpivotOtherColumns(PromotedHeaders, {"Column5", "Cell"}, "Attribute", "Value"),
    Rename = Table.RenameColumns(Unpivot,{{"Column5", "Battery"}, {"Value", "Life"}, {"Attribute", "Item"}}),
    Reorder = Table.ReorderColumns(Rename,{"Item", "Battery", "Cell", "Life"}),
    Sort = Table.Sort(Reorder,{{"Item", Order.Descending}, {"Battery", Order.Ascending}, {"Life", Order.Ascending}})
in
    Sort
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top