Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hello,
can anyone tell me how to populate a combobox in a sheet with records meeting a criteria set in another combobox, i.e. show only the records (productnames)belonging to the productgroup Ive selected? I've a small product database in Excel. Thanks Mats |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I put two comboboxes from the control toolbox toolbar on a worksheet.
I put this code behind the ThisWorkbook module to populate the first combobox whenever the workbook is opened: Option Explicit Private Sub Workbook_Open() With Worksheets("sheet1").ComboBox1 .Clear .AddItem "A" .AddItem "B" .AddItem "C" .AddItem "D" End With End Sub Then I put this code behind the worksheet that contained the two comboboxes: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("sheet1") Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, 1).Value End If Next myCell End Sub I matched up on column A and took the value from column B. If you're new to macros, you may want to read David McRitchie's intro at: http://www.mvps.org/dmcritchie/excel/getstarted.htm Mats Samson wrote: Hello, can anyone tell me how to populate a combobox in a sheet with records meeting a criteria set in another combobox, i.e. show only the records (productnames)belonging to the productgroup Ive selected? I've a small product database in Excel. Thanks Mats -- Dave Peterson |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Instead of this portion:
If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If Try using: Me.ComboBox1.clear Dave Peterson wrote: I put two comboboxes from the control toolbox toolbar on a worksheet. I put this code behind the ThisWorkbook module to populate the first combobox whenever the workbook is opened: Option Explicit Private Sub Workbook_Open() With Worksheets("sheet1").ComboBox1 .Clear .AddItem "A" .AddItem "B" .AddItem "C" .AddItem "D" End With End Sub Then I put this code behind the worksheet that contained the two comboboxes: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("sheet1") Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, 1).Value End If Next myCell End Sub I matched up on column A and took the value from column B. If you're new to macros, you may want to read David McRitchie's intro at: http://www.mvps.org/dmcritchie/excel/getstarted.htm Mats Samson wrote: Hello, can anyone tell me how to populate a combobox in a sheet with records meeting a criteria set in another combobox, i.e. show only the records (productnames)belonging to the productgroup Ive selected? I've a small product database in Excel. Thanks Mats -- Dave Peterson -- Dave Peterson |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Dave,
Im sorry but I was travelling a lot and didnt have any time to go through your proposal until now, but I dont understand how to apply it in my case. I have a sheet named Calculation where I have the two CBs, one for the product names and one for the product categories. In the sheet Products, I have my small product database where column A is my product number (NOT an index number), column B is the product name and column C is the category (Plastics, Fillers, Solventsヲ..). There are more columns but not important for this question. There are about 200 products belonging to one of 6 categories and as you understand when I select the product, I want to limit the list to the selected category, so I dont need to scroll the entire 200 products list. Can you explain how to accomplish this? Thanks Mats "Dave Peterson" wrote: Instead of this portion: If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If Try using: Me.ComboBox1.clear Dave Peterson wrote: I put two comboboxes from the control toolbox toolbar on a worksheet. I put this code behind the ThisWorkbook module to populate the first combobox whenever the workbook is opened: Option Explicit Private Sub Workbook_Open() With Worksheets("sheet1").ComboBox1 .Clear .AddItem "A" .AddItem "B" .AddItem "C" .AddItem "D" End With End Sub Then I put this code behind the worksheet that contained the two comboboxes: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("sheet1") Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, 1).Value End If Next myCell End Sub I matched up on column A and took the value from column B. If you're new to macros, you may want to read David McRitchie's intro at: http://www.mvps.org/dmcritchie/excel/getstarted.htm Mats Samson wrote: Hello, can anyone tell me how to populate a combobox in a sheet with records meeting a criteria set in another combobox, i.e. show only the records (productnames)belonging to the productgroup Iテ「ぎ┐ve selected? I've a small product database in Excel. Thanks Mats -- Dave Peterson -- Dave Peterson |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Mats,
Take a look here at a great tutorial of what you want to do: http://www.contextures.com/xlDataVal02.html HTH, Jim "Mats Samson" wrote in message ... Hi Dave, I'm sorry but I was travelling a lot and didn't have any time to go through your proposal until now, but I don't understand how to apply it in my case. I have a sheet named Calculation where I have the two CB's, one for the product names and one for the product categories. In the sheet Products, I have my small product database where column A is my product number (NOT an index number), column B is the product name and column C is the category (Plastics, Fillers, Solvents...). There are more columns but not important for this question. There are about 200 products belonging to one of 6 categories and as you understand when I select the product, I want to limit the list to the selected category, so I don't need to scroll the entire 200 products list. Can you explain how to accomplish this? Thanks Mats "Dave Peterson" wrote: Instead of this portion: If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If Try using: Me.ComboBox1.clear Dave Peterson wrote: I put two comboboxes from the control toolbox toolbar on a worksheet. I put this code behind the ThisWorkbook module to populate the first combobox whenever the workbook is opened: Option Explicit Private Sub Workbook_Open() With Worksheets("sheet1").ComboBox1 .Clear .AddItem "A" .AddItem "B" .AddItem "C" .AddItem "D" End With End Sub Then I put this code behind the worksheet that contained the two comboboxes: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("sheet1") Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, 1).Value End If Next myCell End Sub I matched up on column A and took the value from column B. If you're new to macros, you may want to read David McRitchie's intro at: http://www.mvps.org/dmcritchie/excel/getstarted.htm Mats Samson wrote: Hello, can anyone tell me how to populate a combobox in a sheet with records meeting a criteria set in another combobox, i.e. show only the records (productnames)belonging to the productgroup I?Tve selected? I've a small product database in Excel. Thanks Mats -- Dave Peterson -- Dave Peterson |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
This goes behind the ThisWorkbook Module:
Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Change those category# to what you need--and add as many as you need. Then put this behind the Calculation worksheet: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("Products") Set myRng = .Range("c1", .Cells(.Rows.Count, "c").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, -2).Value End If Next myCell End Sub ===== This line took the value from column A: Me.ComboBox2.AddItem myCell.Offset(0, -2).Value If you wanted the value from column B: Me.ComboBox2.AddItem myCell.Offset(0, -1).Value Mats Samson wrote: Hi Dave, Im sorry but I was travelling a lot and didnt have any time to go through your proposal until now, but I dont understand how to apply it in my case. I have a sheet named Calculation where I have the two CBs, one for the product names and one for the product categories. In the sheet Products, I have my small product database where column A is my product number (NOT an index number), column B is the product name and column C is the category (Plastics, Fillers, Solventsヲ..). There are more columns but not important for this question. There are about 200 products belonging to one of 6 categories and as you understand when I select the product, I want to limit the list to the selected category, so I dont need to scroll the entire 200 products list. Can you explain how to accomplish this? Thanks Mats "Dave Peterson" wrote: Instead of this portion: If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If Try using: Me.ComboBox1.clear Dave Peterson wrote: I put two comboboxes from the control toolbox toolbar on a worksheet. I put this code behind the ThisWorkbook module to populate the first combobox whenever the workbook is opened: Option Explicit Private Sub Workbook_Open() With Worksheets("sheet1").ComboBox1 .Clear .AddItem "A" .AddItem "B" .AddItem "C" .AddItem "D" End With End Sub Then I put this code behind the worksheet that contained the two comboboxes: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("sheet1") Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, 1).Value End If Next myCell End Sub I matched up on column A and took the value from column B. If you're new to macros, you may want to read David McRitchie's intro at: http://www.mvps.org/dmcritchie/excel/getstarted.htm Mats Samson wrote: Hello, can anyone tell me how to populate a combobox in a sheet with records meeting a criteria set in another combobox, i.e. show only the records (productnames)belonging to the productgroup Iテ「ぎ┐ve selected? I've a small product database in Excel. Thanks Mats -- Dave Peterson -- Dave Peterson -- Dave Peterson |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thank you Dave,
it works quite fine the way I set it up for you, but with a small discrepancy plus that I run into another problem! The Product CB never shows any values, only when you push the drop-down list so you can select the items, thats the discrepancy! In my original setup I had the following piece of code for CB2: Private Sub ComboBox2_Change() If (IsError(ComboBox2.Value)) Then Range("B3") = ComboBox2.Text Else Range("B3") = ComboBox2.Text Range("A10") = ComboBox2.Value End If End Sub The reason for this part is that I want to be able to write a product that is not listed in the product database. Usually the cell A10 gets the product number and its used for further processing, to orders and invoices etc. But if I just want to calculate for a product we might be doing business with in the future, I dont want to register it. I only save the calculation so I can retrieve it again later. For that I need some reference, i.e. the 忖nregistered product name. So I had to put in this piece of code to get the other cells to display the product name correctly at any time, otherwise it will display, #N/A. So the question is, can they be combined? Best regards Mats "Dave Peterson" wrote: This goes behind the ThisWorkbook Module: Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Change those category# to what you need--and add as many as you need. Then put this behind the Calculation worksheet: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("Products") Set myRng = .Range("c1", .Cells(.Rows.Count, "c").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, -2).Value End If Next myCell End Sub ===== This line took the value from column A: Me.ComboBox2.AddItem myCell.Offset(0, -2).Value If you wanted the value from column B: Me.ComboBox2.AddItem myCell.Offset(0, -1).Value Mats Samson wrote: Hi Dave, Iテ「ぎ┐m sorry but I was travelling a lot and didnテ「ぎ┐t have any time to go through your proposal until now, but I donテ「ぎ┐t understand how to apply it in my case. I have a sheet named Calculation where I have the two CBテ「ぎ┐s, one for the product names and one for the product categories. In the sheet Products, I have my small product database where column A is my product number (NOT an index number), column B is the product name and column C is the category (Plastics, Fillers, Solventsテ「ぎツヲ..). There are more columns but not important for this question. There are about 200 products belonging to one of 6 categories and as you understand when I select the product, I want to limit the list to the selected category, so I donテ「ぎ┐t need to scroll the entire 200 products list. Can you explain how to accomplish this? Thanks Mats "Dave Peterson" wrote: Instead of this portion: If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If Try using: Me.ComboBox1.clear Dave Peterson wrote: I put two comboboxes from the control toolbox toolbar on a worksheet. I put this code behind the ThisWorkbook module to populate the first combobox whenever the workbook is opened: Option Explicit Private Sub Workbook_Open() With Worksheets("sheet1").ComboBox1 .Clear .AddItem "A" .AddItem "B" .AddItem "C" .AddItem "D" End With End Sub Then I put this code behind the worksheet that contained the two comboboxes: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("sheet1") Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, 1).Value End If Next myCell End Sub I matched up on column A and took the value from column B. If you're new to macros, you may want to read David McRitchie's intro at: http://www.mvps.org/dmcritchie/excel/getstarted.htm Mats Samson wrote: Hello, can anyone tell me how to populate a combobox in a sheet with records meeting a criteria set in another combobox, i.e. show only the records (productnames)belonging to the productgroup Iテδ「テ「堋ャテ「楪「ve selected? I've a small product database in Excel. Thanks Mats -- Dave Peterson -- Dave Peterson -- Dave Peterson |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Did you put the code behind the worksheet with the comboboxes?
Did you use comboboxes from the control toolbox toolbar? If you put a breakpoint in the combobox1 change code, can you see why combobox2 is not be populated? I'm not sure why iserror(combobox2.value) ever comes into play. Mats Samson wrote: Thank you Dave, it works quite fine the way I set it up for you, but with a small discrepancy plus that I run into another problem! The Product CB never shows any values, only when you push the drop-down list so you can select the items, thats the discrepancy! In my original setup I had the following piece of code for CB2: Private Sub ComboBox2_Change() If (IsError(ComboBox2.Value)) Then Range("B3") = ComboBox2.Text Else Range("B3") = ComboBox2.Text Range("A10") = ComboBox2.Value End If End Sub The reason for this part is that I want to be able to write a product that is not listed in the product database. Usually the cell A10 gets the product number and its used for further processing, to orders and invoices etc. But if I just want to calculate for a product we might be doing business with in the future, I dont want to register it. I only save the calculation so I can retrieve it again later. For that I need some reference, i.e. the 忖nregistered product name. So I had to put in this piece of code to get the other cells to display the product name correctly at any time, otherwise it will display, #N/A. So the question is, can they be combined? Best regards Mats "Dave Peterson" wrote: This goes behind the ThisWorkbook Module: Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Change those category# to what you need--and add as many as you need. Then put this behind the Calculation worksheet: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("Products") Set myRng = .Range("c1", .Cells(.Rows.Count, "c").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, -2).Value End If Next myCell End Sub ===== This line took the value from column A: Me.ComboBox2.AddItem myCell.Offset(0, -2).Value If you wanted the value from column B: Me.ComboBox2.AddItem myCell.Offset(0, -1).Value Mats Samson wrote: Hi Dave, Iテ「ぎ┐m sorry but I was travelling a lot and didnテ「ぎ┐t have any time to go through your proposal until now, but I donテ「ぎ┐t understand how to apply it in my case. I have a sheet named Calculation where I have the two CBテ「ぎ┐s, one for the product names and one for the product categories. In the sheet Products, I have my small product database where column A is my product number (NOT an index number), column B is the product name and column C is the category (Plastics, Fillers, Solventsテ「ぎツヲ..). There are more columns but not important for this question. There are about 200 products belonging to one of 6 categories and as you understand when I select the product, I want to limit the list to the selected category, so I donテ「ぎ┐t need to scroll the entire 200 products list. Can you explain how to accomplish this? Thanks Mats "Dave Peterson" wrote: Instead of this portion: If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If Try using: Me.ComboBox1.clear Dave Peterson wrote: I put two comboboxes from the control toolbox toolbar on a worksheet. I put this code behind the ThisWorkbook module to populate the first combobox whenever the workbook is opened: Option Explicit Private Sub Workbook_Open() With Worksheets("sheet1").ComboBox1 .Clear .AddItem "A" .AddItem "B" .AddItem "C" .AddItem "D" End With End Sub Then I put this code behind the worksheet that contained the two comboboxes: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("sheet1") Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, 1).Value End If Next myCell End Sub I matched up on column A and took the value from column B. If you're new to macros, you may want to read David McRitchie's intro at: http://www.mvps.org/dmcritchie/excel/getstarted.htm Mats Samson wrote: Hello, can anyone tell me how to populate a combobox in a sheet with records meeting a criteria set in another combobox, i.e. show only the records (productnames)belonging to the productgroup Iテδ「テ「堋ャテ「楪「ve selected? I've a small product database in Excel. Thanks Mats -- Dave Peterson -- Dave Peterson -- Dave Peterson -- Dave Peterson |
#9
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Q1: Yes, Sir!
Q2: Yepp! The only difference I used was to create an array with the product categories in an Auto_Open module of the workbook: With Worksheets("Calculation").ComboBox1 .Clear .List = PGrp the array of categories .LinkedCell = "C3" End With I also had to include: Me.ComboBox1.Clear before the For Each ヲヲ. Otherwise new items was added to the previous list of products for every new selection of category. The CB is populated alright but only the in dropdown-state, there is no value showing in the 徙ff-focus state, before or after the selection?!?!?! Well, the reason for IsError is that the CB2 linked cell B3 will display #N/A if I write something in the CB instead of selecting an item in the list. My original properties for CB2 a Boundcolumn: 1 Columncount: 2 ColumnWidths: 0 pt;130 pt LinkedCell: B3 ListFillRange: PDB 狼he product database defined as a named range. With this setup I get the CB2 populated with the product names (Col2) that goes to B3 as CB2.Text and the productcode (Col1) is the CB2.Value that goes to A10 (through the CB2.Change () procedure). If I write a new product the CB2.Text will still write to B3 but A10 will go empty. When the calculation is saved, to another workbook, the cell related to A10 will display 廸ew (through formula). (I tried to put Range (廣10) = 廸ew in the error-true part of CB2.Change, but it doesnt work! A10 is always empty unless a registered product, with product code, is used) The products are not indexed, the product codes/numbers are alphanumeric and sorted only to product code (Col1). Im getting more and more confused, bhaah! Mats "Dave Peterson" wrote: Did you put the code behind the worksheet with the comboboxes? Did you use comboboxes from the control toolbox toolbar? If you put a breakpoint in the combobox1 change code, can you see why combobox2 is not be populated? I'm not sure why iserror(combobox2.value) ever comes into play. Mats Samson wrote: Thank you Dave, it works quite fine the way I set it up for you, but with a small discrepancy plus that I run into another problem! The Product CB never shows any values, only when you push the drop-down list so you can select the items, thatテ「ぎ┐s the discrepancy! In my original setup I had the following piece of code for CB2: Private Sub ComboBox2_Change() If (IsError(ComboBox2.Value)) Then Range("B3") = ComboBox2.Text Else Range("B3") = ComboBox2.Text Range("A10") = ComboBox2.Value End If End Sub The reason for this part is that I want to be able to write a product that is not listed in the product database. Usually the cell A10 gets the product number and itテ「ぎ┐s used for further processing, to orders and invoices etc. But if I just want to calculate for a product we might be doing business with in the future, I donテ「ぎ┐t want to register it. I only save the calculation so I can retrieve it again later. For that I need some reference, i.e. the テ「ぎナ砥nregisteredテ「ぎツ product name. So I had to put in this piece of code to get the other cells to display the product name correctly at any time, otherwise it will display, #N/A. So the question is, can they be combined? Best regards Mats "Dave Peterson" wrote: This goes behind the ThisWorkbook Module: Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Change those category# to what you need--and add as many as you need. Then put this behind the Calculation worksheet: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("Products") Set myRng = .Range("c1", .Cells(.Rows.Count, "c").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, -2).Value End If Next myCell End Sub ===== This line took the value from column A: Me.ComboBox2.AddItem myCell.Offset(0, -2).Value If you wanted the value from column B: Me.ComboBox2.AddItem myCell.Offset(0, -1).Value Mats Samson wrote: Hi Dave, Iテδ「テ「堋ャテ「楪「m sorry but I was travelling a lot and didnテδ「テ「堋ャテ「楪「t have any time to go through your proposal until now, but I donテδ「テ「堋ャテ「楪「t understand how to apply it in my case. I have a sheet named Calculation where I have the two CBテδ「テ「堋ャテ「楪「s, one for the product names and one for the product categories. In the sheet Products, I have my small product database where column A is my product number (NOT an index number), column B is the product name and column C is the category (Plastics, Fillers, Solventsテδ「テ「堋ャテつヲ..). There are more columns but not important for this question. There are about 200 products belonging to one of 6 categories and as you understand when I select the product, I want to limit the list to the selected category, so I donテδ「テ「堋ャテ「楪「t need to scroll the entire 200 products list. Can you explain how to accomplish this? Thanks Mats "Dave Peterson" wrote: Instead of this portion: If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If Try using: Me.ComboBox1.clear Dave Peterson wrote: I put two comboboxes from the control toolbox toolbar on a worksheet. I put this code behind the ThisWorkbook module to populate the first combobox whenever the workbook is opened: Option Explicit Private Sub Workbook_Open() With Worksheets("sheet1").ComboBox1 .Clear .AddItem "A" .AddItem "B" .AddItem "C" .AddItem "D" End With End Sub Then I put this code behind the worksheet that contained the two comboboxes: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("sheet1") Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, 1).Value End If Next myCell End Sub I matched up on column A and took the value from column B. If you're new to macros, you may want to read David McRitchie's intro at: http://www.mvps.org/dmcritchie/excel/getstarted.htm Mats Samson wrote: Hello, can anyone tell me how to populate a combobox in a sheet with records meeting a criteria set in another combobox, i.e. show only the records (productnames)belonging to the productgroup Iテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「ve selected? I've a small product database in Excel. Thanks Mats -- Dave Peterson -- Dave Peterson -- Dave Peterson -- Dave Peterson |
#10
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
This code in the ThisWorkbook module is used instead of the code in your
auto_open procedu Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Pick one and use that. Also, try dropping the linked cell and assigning the value directly in code. And get rid of those properties in the combobox2. Instead of using listfillrange, the code is looping through the list to get the items to display in that combobox. Mats Samson wrote: Q1: Yes, Sir! Q2: Yepp! The only difference I used was to create an array with the product categories in an Auto_Open module of the workbook: With Worksheets("Calculation").ComboBox1 .Clear .List = PGrp the array of categories .LinkedCell = "C3" End With I also had to include: Me.ComboBox1.Clear before the For Each ヲヲ. Otherwise new items was added to the previous list of products for every new selection of category. The CB is populated alright but only the in dropdown-state, there is no value showing in the 徙ff-focus state, before or after the selection?!?!?! Well, the reason for IsError is that the CB2 linked cell B3 will display #N/A if I write something in the CB instead of selecting an item in the list. My original properties for CB2 a Boundcolumn: 1 Columncount: 2 ColumnWidths: 0 pt;130 pt LinkedCell: B3 ListFillRange: PDB 狼he product database defined as a named range. With this setup I get the CB2 populated with the product names (Col2) that goes to B3 as CB2.Text and the productcode (Col1) is the CB2.Value that goes to A10 (through the CB2.Change () procedure). If I write a new product the CB2.Text will still write to B3 but A10 will go empty. When the calculation is saved, to another workbook, the cell related to A10 will display 廸ew (through formula). (I tried to put Range (廣10) = 廸ew in the error-true part of CB2.Change, but it doesnt work! A10 is always empty unless a registered product, with product code, is used) The products are not indexed, the product codes/numbers are alphanumeric and sorted only to product code (Col1). Im getting more and more confused, bhaah! Mats "Dave Peterson" wrote: Did you put the code behind the worksheet with the comboboxes? Did you use comboboxes from the control toolbox toolbar? If you put a breakpoint in the combobox1 change code, can you see why combobox2 is not be populated? I'm not sure why iserror(combobox2.value) ever comes into play. Mats Samson wrote: Thank you Dave, it works quite fine the way I set it up for you, but with a small discrepancy plus that I run into another problem! The Product CB never shows any values, only when you push the drop-down list so you can select the items, thatテ「ぎ┐s the discrepancy! In my original setup I had the following piece of code for CB2: Private Sub ComboBox2_Change() If (IsError(ComboBox2.Value)) Then Range("B3") = ComboBox2.Text Else Range("B3") = ComboBox2.Text Range("A10") = ComboBox2.Value End If End Sub The reason for this part is that I want to be able to write a product that is not listed in the product database. Usually the cell A10 gets the product number and itテ「ぎ┐s used for further processing, to orders and invoices etc. But if I just want to calculate for a product we might be doing business with in the future, I donテ「ぎ┐t want to register it. I only save the calculation so I can retrieve it again later. For that I need some reference, i.e. the テ「ぎナ砥nregisteredテ「ぎツ product name. So I had to put in this piece of code to get the other cells to display the product name correctly at any time, otherwise it will display, #N/A. So the question is, can they be combined? Best regards Mats "Dave Peterson" wrote: This goes behind the ThisWorkbook Module: Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Change those category# to what you need--and add as many as you need. Then put this behind the Calculation worksheet: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("Products") Set myRng = .Range("c1", .Cells(.Rows.Count, "c").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, -2).Value End If Next myCell End Sub ===== This line took the value from column A: Me.ComboBox2.AddItem myCell.Offset(0, -2).Value If you wanted the value from column B: Me.ComboBox2.AddItem myCell.Offset(0, -1).Value Mats Samson wrote: Hi Dave, Iテδ「テ「堋ャテ「楪「m sorry but I was travelling a lot and didnテδ「テ「堋ャテ「楪「t have any time to go through your proposal until now, but I donテδ「テ「堋ャテ「楪「t understand how to apply it in my case. I have a sheet named Calculation where I have the two CBテδ「テ「堋ャテ「楪「s, one for the product names and one for the product categories. In the sheet Products, I have my small product database where column A is my product number (NOT an index number), column B is the product name and column C is the category (Plastics, Fillers, Solventsテδ「テ「堋ャテつヲ..). There are more columns but not important for this question. There are about 200 products belonging to one of 6 categories and as you understand when I select the product, I want to limit the list to the selected category, so I donテδ「テ「堋ャテ「楪「t need to scroll the entire 200 products list. Can you explain how to accomplish this? Thanks Mats "Dave Peterson" wrote: Instead of this portion: If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If Try using: Me.ComboBox1.clear Dave Peterson wrote: I put two comboboxes from the control toolbox toolbar on a worksheet. I put this code behind the ThisWorkbook module to populate the first combobox whenever the workbook is opened: Option Explicit Private Sub Workbook_Open() With Worksheets("sheet1").ComboBox1 .Clear .AddItem "A" .AddItem "B" .AddItem "C" .AddItem "D" End With End Sub Then I put this code behind the worksheet that contained the two comboboxes: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("sheet1") Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, 1).Value End If Next myCell End Sub I matched up on column A and took the value from column B. If you're new to macros, you may want to read David McRitchie's intro at: http://www.mvps.org/dmcritchie/excel/getstarted.htm Mats Samson wrote: Hello, can anyone tell me how to populate a combobox in a sheet with records meeting a criteria set in another combobox, i.e. show only the records (productnames)belonging to the productgroup Iテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「ve selected? I've a small product database in Excel. Thanks Mats -- Dave Peterson -- Dave Peterson -- Dave Peterson -- Dave Peterson -- Dave Peterson |
#11
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hello Jim,
thank you for the tip but as you see by the correspondence below, I don't think my case can be solved with named lists. Best regards Mats "Jim May" wrote: Mats, Take a look here at a great tutorial of what you want to do: http://www.contextures.com/xlDataVal02.html HTH, Jim "Mats Samson" wrote in message ... Hi Dave, I'm sorry but I was travelling a lot and didn't have any time to go through your proposal until now, but I don't understand how to apply it in my case. I have a sheet named Calculation where I have the two CB's, one for the product names and one for the product categories. In the sheet Products, I have my small product database where column A is my product number (NOT an index number), column B is the product name and column C is the category (Plastics, Fillers, Solvents...). There are more columns but not important for this question. There are about 200 products belonging to one of 6 categories and as you understand when I select the product, I want to limit the list to the selected category, so I don't need to scroll the entire 200 products list. Can you explain how to accomplish this? Thanks Mats "Dave Peterson" wrote: Instead of this portion: If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If Try using: Me.ComboBox1.clear Dave Peterson wrote: I put two comboboxes from the control toolbox toolbar on a worksheet. I put this code behind the ThisWorkbook module to populate the first combobox whenever the workbook is opened: Option Explicit Private Sub Workbook_Open() With Worksheets("sheet1").ComboBox1 .Clear .AddItem "A" .AddItem "B" .AddItem "C" .AddItem "D" End With End Sub Then I put this code behind the worksheet that contained the two comboboxes: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("sheet1") Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, 1).Value End If Next myCell End Sub I matched up on column A and took the value from column B. If you're new to macros, you may want to read David McRitchie's intro at: http://www.mvps.org/dmcritchie/excel/getstarted.htm Mats Samson wrote: Hello, can anyone tell me how to populate a combobox in a sheet with records meeting a criteria set in another combobox, i.e. show only the records (productnames)belonging to the productgroup Iテ「?Tve selected? I've a small product database in Excel. Thanks Mats -- Dave Peterson -- Dave Peterson |
#12
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Dave,
sorry, I forgot to mention that I already dropped the ListFillRange and the other properties of the CB. It was of course necessary, otherwise it wont work at all! But the reason why it didnt work was that I forgot to remove the TextColumn property that was set to 2. So now it works OK and CB2 displays the selected item. This TextColumn setting belonged to my original setup through which I got 2 忻alues out of one CB. The product name as the Text property and the Product code as the Value property at the same time it served as index for the other values in a record. Then I could look up other values of the particular record, like supplier and units, etc. and present them in my calculation) The only remaining problem is that since we kind of removed the 彡onnection to the product database and only creating a list based on a criteria, I cannot get the product code into cell A10. It must not be a formula in the cell, only the value. Can we do that? When I select the product name (ColB) that goes to B3 I get at the same time the Product code from ColA going into A10? I would appreciate your help very much! Best regards Mats "Dave Peterson" wrote: This code in the ThisWorkbook module is used instead of the code in your auto_open procedu Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Pick one and use that. Also, try dropping the linked cell and assigning the value directly in code. And get rid of those properties in the combobox2. Instead of using listfillrange, the code is looping through the list to get the items to display in that combobox. Mats Samson wrote: Q1: Yes, Sir! Q2: Yepp! The only difference I used was to create an array with the product categories in an Auto_Open module of the workbook: With Worksheets("Calculation").ComboBox1 .Clear .List = PGrp テ「ぎヒ the array of categories .LinkedCell = "C3" End With I also had to include: Me.ComboBox1.Clear before the For Each テ「ぎツヲテ「ぎツヲ. Otherwise new items was added to the previous list of products for every new selection of category. The CB is populated alright but only the in dropdown-state, there is no value showing in the テ「ぎナ登ff-focus stateテ「ぎツ, before or after the selection?!?!?! Well, the reason for IsError is that the CB2 linked cell B3 will display #N/A if I write something in the CB instead of selecting an item in the list. My original properties for CB2 a Boundcolumn: 1 Columncount: 2 ColumnWidths: 0 pt;130 pt LinkedCell: B3 ListFillRange: PDB テ「ぎヒ弋he product database defined as a named range. With this setup I get the CB2 populated with the product names (Col2) that goes to B3 as CB2.Text and the productcode (Col1) is the CB2.Value that goes to A10 (through the CB2.Change () procedure). If I write a new product the CB2.Text will still write to B3 but A10 will go empty. When the calculation is saved, to another workbook, the cell related to A10 will display テ「ぎナ哲ewテ「ぎツ (through formula). (I tried to put Range (テ「ぎナ鄭10テ「ぎツ) = テ「ぎナ哲ewテ「ぎツ in the error-true part of CB2.Change, but it doesnテ「ぎ┐t work! A10 is always empty unless a registered product, with product code, is used) The products are not indexed, the product codes/numbers are alphanumeric and sorted only to product code (Col1). Iテ「ぎ┐m getting more and more confused, bhaah! Mats "Dave Peterson" wrote: Did you put the code behind the worksheet with the comboboxes? Did you use comboboxes from the control toolbox toolbar? If you put a breakpoint in the combobox1 change code, can you see why combobox2 is not be populated? I'm not sure why iserror(combobox2.value) ever comes into play. Mats Samson wrote: Thank you Dave, it works quite fine the way I set it up for you, but with a small discrepancy plus that I run into another problem! The Product CB never shows any values, only when you push the drop-down list so you can select the items, thatテδ「テ「堋ャテ「楪「s the discrepancy! In my original setup I had the following piece of code for CB2: Private Sub ComboBox2_Change() If (IsError(ComboBox2.Value)) Then Range("B3") = ComboBox2.Text Else Range("B3") = ComboBox2.Text Range("A10") = ComboBox2.Value End If End Sub The reason for this part is that I want to be able to write a product that is not listed in the product database. Usually the cell A10 gets the product number and itテδ「テ「堋ャテ「楪「s used for further processing, to orders and invoices etc. But if I just want to calculate for a product we might be doing business with in the future, I donテδ「テ「堋ャテ「楪「t want to register it. I only save the calculation so I can retrieve it again later. For that I need some reference, i.e. the テδ「テ「堋ャテ忖nregisteredテδ「テ「堋ャテつ product name. So I had to put in this piece of code to get the other cells to display the product name correctly at any time, otherwise it will display, #N/A. So the question is, can they be combined? Best regards Mats "Dave Peterson" wrote: This goes behind the ThisWorkbook Module: Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Change those category# to what you need--and add as many as you need. Then put this behind the Calculation worksheet: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("Products") Set myRng = .Range("c1", .Cells(.Rows.Count, "c").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, -2).Value End If Next myCell End Sub ===== This line took the value from column A: Me.ComboBox2.AddItem myCell.Offset(0, -2).Value If you wanted the value from column B: Me.ComboBox2.AddItem myCell.Offset(0, -1).Value Mats Samson wrote: Hi Dave, Iテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「m sorry but I was travelling a lot and didnテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「t have any time to go through your proposal until now, but I donテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「t understand how to apply it in my case. I have a sheet named Calculation where I have the two CBテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「s, one for the product names and one for the product categories. In the sheet Products, I have my small product database where column A is my product number (NOT an index number), column B is the product name and column C is the category (Plastics, Fillers, Solventsテθ津つ「テδ「テ「ぎナ。テつャテム堙つヲ..). There are more columns but not important for this question. There are about 200 products belonging to one of 6 categories and as you understand when I select the product, I want to limit the list to the selected category, so I donテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「t need to scroll the entire 200 products list. Can you explain how to accomplish this? Thanks Mats "Dave Peterson" wrote: Instead of this portion: If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If Try using: Me.ComboBox1.clear Dave Peterson wrote: I put two comboboxes from the control toolbox toolbar on a worksheet. I put this code behind the ThisWorkbook module to populate the first combobox whenever the workbook is opened: Option Explicit Private Sub Workbook_Open() With Worksheets("sheet1").ComboBox1 .Clear .AddItem "A" .AddItem "B" .AddItem "C" .AddItem "D" End With End Sub Then I put this code behind the worksheet that contained the two comboboxes: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("sheet1") Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, 1).Value End If Next myCell End Sub I matched up on column A and took the value from column B. If you're new to macros, you may want to read David McRitchie's intro at: http://www.mvps.org/dmcritchie/excel/getstarted.htm Mats Samson wrote: Hello, can anyone tell me how to populate a combobox in a sheet with records meeting a criteria set in another combobox, i.e. show only the records (productnames)belonging to the productgroup Iテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム堙つ ャテθ津つ「テδ「テ「堋ャテツセテム堙つ「ve selected? I've a small product database in Excel. Thanks Mats -- Dave Peterson -- Dave Peterson -- Dave Peterson -- Dave Peterson -- Dave Peterson |
#13
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Why not use a formula to return the product code?
Put this in A10: =if(b3="","",index(products!b:b,match(b3,products! c:c,0)) When you populuate B3 with the product name, the formula will evaluate to the product code. ========== Alternatively... I put this behind the ThisWorkbook module: Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With With Worksheets("Calculation").ComboBox2 .Boundcolumn = 1 .Columncount = 2 .ColumnWidths = "0 pt;130 pt" End With End Sub I put this behind the calculation worksheet module: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("Products") Set myRng = .Range("c1", .Cells(.Rows.Count, "c").End(xlUp)) End With With Me.ComboBox2 .Clear For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then .AddItem myCell.Offset(0, -2).Value .List(.ListCount - 1, 1) = myCell.Offset(0, -1).Value End If Next myCell End With End Sub Private Sub ComboBox2_Change() With Me.ComboBox2 If .ListIndex < 0 Then Me.Range("b3,a10").ClearContents Else Me.Range("b3").Value = .List(.ListIndex, 0) Me.Range("a10").Value = .List(.ListIndex, 1) End If End With End Sub I'm not sure if I got the product code/item number in the right position. But that shouldn't be too difficult to fix. Notice that I did add a .clear in the combobox1_change event. No matter which routine (today's or yesterday's), I would think that this would be a good thing to have. Mats Samson wrote: Hi Dave, sorry, I forgot to mention that I already dropped the ListFillRange and the other properties of the CB. It was of course necessary, otherwise it wont work at all! But the reason why it didnt work was that I forgot to remove the TextColumn property that was set to 2. So now it works OK and CB2 displays the selected item. This TextColumn setting belonged to my original setup through which I got 2 忻alues out of one CB. The product name as the Text property and the Product code as the Value property at the same time it served as index for the other values in a record. Then I could look up other values of the particular record, like supplier and units, etc. and present them in my calculation) The only remaining problem is that since we kind of removed the 彡onnection to the product database and only creating a list based on a criteria, I cannot get the product code into cell A10. It must not be a formula in the cell, only the value. Can we do that? When I select the product name (ColB) that goes to B3 I get at the same time the Product code from ColA going into A10? I would appreciate your help very much! Best regards Mats "Dave Peterson" wrote: This code in the ThisWorkbook module is used instead of the code in your auto_open procedu Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Pick one and use that. Also, try dropping the linked cell and assigning the value directly in code. And get rid of those properties in the combobox2. Instead of using listfillrange, the code is looping through the list to get the items to display in that combobox. Mats Samson wrote: Q1: Yes, Sir! Q2: Yepp! The only difference I used was to create an array with the product categories in an Auto_Open module of the workbook: With Worksheets("Calculation").ComboBox1 .Clear .List = PGrp テ「ぎヒ the array of categories .LinkedCell = "C3" End With I also had to include: Me.ComboBox1.Clear before the For Each テ「ぎツヲテ「ぎツヲ. Otherwise new items was added to the previous list of products for every new selection of category. The CB is populated alright but only the in dropdown-state, there is no value showing in the テ「ぎナ登ff-focus stateテ「ぎツ, before or after the selection?!?!?! Well, the reason for IsError is that the CB2 linked cell B3 will display #N/A if I write something in the CB instead of selecting an item in the list. My original properties for CB2 a Boundcolumn: 1 Columncount: 2 ColumnWidths: 0 pt;130 pt LinkedCell: B3 ListFillRange: PDB テ「ぎヒ弋he product database defined as a named range. With this setup I get the CB2 populated with the product names (Col2) that goes to B3 as CB2.Text and the productcode (Col1) is the CB2.Value that goes to A10 (through the CB2.Change () procedure). If I write a new product the CB2.Text will still write to B3 but A10 will go empty. When the calculation is saved, to another workbook, the cell related to A10 will display テ「ぎナ哲ewテ「ぎツ (through formula). (I tried to put Range (テ「ぎナ鄭10テ「ぎツ) = テ「ぎナ哲ewテ「ぎツ in the error-true part of CB2.Change, but it doesnテ「ぎ┐t work! A10 is always empty unless a registered product, with product code, is used) The products are not indexed, the product codes/numbers are alphanumeric and sorted only to product code (Col1). Iテ「ぎ┐m getting more and more confused, bhaah! Mats "Dave Peterson" wrote: Did you put the code behind the worksheet with the comboboxes? Did you use comboboxes from the control toolbox toolbar? If you put a breakpoint in the combobox1 change code, can you see why combobox2 is not be populated? I'm not sure why iserror(combobox2.value) ever comes into play. Mats Samson wrote: Thank you Dave, it works quite fine the way I set it up for you, but with a small discrepancy plus that I run into another problem! The Product CB never shows any values, only when you push the drop-down list so you can select the items, thatテδ「テ「堋ャテ「楪「s the discrepancy! In my original setup I had the following piece of code for CB2: Private Sub ComboBox2_Change() If (IsError(ComboBox2.Value)) Then Range("B3") = ComboBox2.Text Else Range("B3") = ComboBox2.Text Range("A10") = ComboBox2.Value End If End Sub The reason for this part is that I want to be able to write a product that is not listed in the product database. Usually the cell A10 gets the product number and itテδ「テ「堋ャテ「楪「s used for further processing, to orders and invoices etc. But if I just want to calculate for a product we might be doing business with in the future, I donテδ「テ「堋ャテ「楪「t want to register it. I only save the calculation so I can retrieve it again later. For that I need some reference, i.e. the テδ「テ「堋ャテ忖nregisteredテδ「テ「堋ャテつ product name. So I had to put in this piece of code to get the other cells to display the product name correctly at any time, otherwise it will display, #N/A. So the question is, can they be combined? Best regards Mats "Dave Peterson" wrote: This goes behind the ThisWorkbook Module: Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Change those category# to what you need--and add as many as you need. Then put this behind the Calculation worksheet: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("Products") Set myRng = .Range("c1", .Cells(.Rows.Count, "c").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, -2).Value End If Next myCell End Sub ===== This line took the value from column A: Me.ComboBox2.AddItem myCell.Offset(0, -2).Value If you wanted the value from column B: Me.ComboBox2.AddItem myCell.Offset(0, -1).Value Mats Samson wrote: Hi Dave, Iテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「m sorry but I was travelling a lot and didnテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「t have any time to go through your proposal until now, but I donテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「t understand how to apply it in my case. I have a sheet named Calculation where I have the two CBテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「s, one for the product names and one for the product categories. In the sheet Products, I have my small product database where column A is my product number (NOT an index number), column B is the product name and column C is the category (Plastics, Fillers, Solventsテθ津つ「テδ「テ「ぎナ。テつャテム堙つヲ..). There are more columns but not important for this question. There are about 200 products belonging to one of 6 categories and as you understand when I select the product, I want to limit the list to the selected category, so I donテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「t need to scroll the entire 200 products list. Can you explain how to accomplish this? Thanks Mats "Dave Peterson" wrote: Instead of this portion: If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If Try using: Me.ComboBox1.clear Dave Peterson wrote: I put two comboboxes from the control toolbox toolbar on a worksheet. I put this code behind the ThisWorkbook module to populate the first combobox whenever the workbook is opened: Option Explicit Private Sub Workbook_Open() With Worksheets("sheet1").ComboBox1 .Clear .AddItem "A" .AddItem "B" .AddItem "C" .AddItem "D" End With End Sub Then I put this code behind the worksheet that contained the two comboboxes: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("sheet1") Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, 1).Value End If Next myCell End Sub I matched up on column A and took the value from column B. If you're new to macros, you may want to read David McRitchie's intro at: http://www.mvps.org/dmcritchie/excel/getstarted.htm Mats Samson wrote: Hello, can anyone tell me how to populate a combobox in a sheet with records meeting a criteria set in another combobox, i.e. show only the records (productnames)belonging to the productgroup Iテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム堙つ ャテθ津つ「テδ「テ「堋ャテツセテム堙つ「ve selected? I've a small product database in Excel. Thanks Mats -- Dave Peterson -- Dave Peterson -- Dave Peterson -- Dave Peterson -- Dave Peterson -- Dave Peterson |
#14
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thats exactly what I dont want to do!
The reason is that column A and B are used to put all the conditions for the business calculation, deliveries, quantity, purchase price, currencies, freight, insurances, etc, etc,ヲ..A calculation contains parameters of different kind in nearly all cells in the range A3:B28. The actual calculation is taking place in columns F to J. The Column A and B VALUES are saved in a 徨egister and because there are so many parameters they are saved in two lines to be readable when you look into the saved calculations list. I can retrieve an old calculation by copying it back to the calculation sheet, transposing 2 lines back to 2 columns and pasting VALUES only. Any formula in A10 will be destroyed by a retrieved calculation. Accordingly the A10 value has to be set by a function/code, not a formula. Regards Mats "Dave Peterson" wrote: Why not use a formula to return the product code? Put this in A10: =if(b3="","",index(products!b:b,match(b3,products! c:c,0)) When you populuate B3 with the product name, the formula will evaluate to the product code. ========== Alternatively... I put this behind the ThisWorkbook module: Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With With Worksheets("Calculation").ComboBox2 .Boundcolumn = 1 .Columncount = 2 .ColumnWidths = "0 pt;130 pt" End With End Sub I put this behind the calculation worksheet module: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("Products") Set myRng = .Range("c1", .Cells(.Rows.Count, "c").End(xlUp)) End With With Me.ComboBox2 .Clear For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then .AddItem myCell.Offset(0, -2).Value .List(.ListCount - 1, 1) = myCell.Offset(0, -1).Value End If Next myCell End With End Sub Private Sub ComboBox2_Change() With Me.ComboBox2 If .ListIndex < 0 Then Me.Range("b3,a10").ClearContents Else Me.Range("b3").Value = .List(.ListIndex, 0) Me.Range("a10").Value = .List(.ListIndex, 1) End If End With End Sub I'm not sure if I got the product code/item number in the right position. But that shouldn't be too difficult to fix. Notice that I did add a .clear in the combobox1_change event. No matter which routine (today's or yesterday's), I would think that this would be a good thing to have. Mats Samson wrote: Hi Dave, sorry, I forgot to mention that I already dropped the ListFillRange and the other properties of the CB. It was of course necessary, otherwise it wonテ「ぎ┐t work at all! But the reason why it didnテ「ぎ┐t work was that I forgot to remove the TextColumn property that was set to 2. So now it works OK and CB2 displays the selected item. This TextColumn setting belonged to my original setup through which I got 2 テ「ぎナ砺aluesテ「ぎツ out of one CB. The product name as the Text property and the Product code as the Value property at the same time it served as index for the other values in a record. Then I could look up other values of the particular record, like supplier and units, etc. and present them in my calculation) The only remaining problem is that since we kind of removed the テ「ぎナ田onnectionテ「ぎツ to the product database and only creating a list based on a criteria, I cannot get the product code into cell A10. It must not be a formula in the cell, only the value. Can we do that? When I select the product name (ColB) that goes to B3 I get at the same time the Product code from ColA going into A10? I would appreciate your help very much! Best regards Mats "Dave Peterson" wrote: This code in the ThisWorkbook module is used instead of the code in your auto_open procedu Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Pick one and use that. Also, try dropping the linked cell and assigning the value directly in code. And get rid of those properties in the combobox2. Instead of using listfillrange, the code is looping through the list to get the items to display in that combobox. Mats Samson wrote: Q1: Yes, Sir! Q2: Yepp! The only difference I used was to create an array with the product categories in an Auto_Open module of the workbook: With Worksheets("Calculation").ComboBox1 .Clear .List = PGrp テδ「テ「堋ャテ暁 the array of categories .LinkedCell = "C3" End With I also had to include: Me.ComboBox1.Clear before the For Each テδ「テ「堋ャテつヲテδ「テ「堋ャテつヲ. Otherwise new items was added to the previous list of products for every new selection of category. The CB is populated alright but only the in dropdown-state, there is no value showing in the テδ「テ「堋ャテ徙ff-focus stateテδ「テ「堋ャテつ, before or after the selection?!?!?! Well, the reason for IsError is that the CB2 linked cell B3 will display #N/A if I write something in the CB instead of selecting an item in the list. My original properties for CB2 a Boundcolumn: 1 Columncount: 2 ColumnWidths: 0 pt;130 pt LinkedCell: B3 ListFillRange: PDB テδ「テ「堋ャテ暁典he product database defined as a named range. With this setup I get the CB2 populated with the product names (Col2) that goes to B3 as CB2.Text and the productcode (Col1) is the CB2.Value that goes to A10 (through the CB2.Change () procedure). If I write a new product the CB2.Text will still write to B3 but A10 will go empty. When the calculation is saved, to another workbook, the cell related to A10 will display テδ「テ「堋ャテ廸ewテδ「テ「堋ャテつ (through formula). (I tried to put Range (テδ「テ「堋ャテ廣10テδ「テ「堋ャテつ) = テδ「テ「堋ャテ廸ewテδ「テ「堋ャテつ in the error-true part of CB2.Change, but it doesnテδ「テ「堋ャテ「楪「t work! A10 is always empty unless a registered product, with product code, is used) The products are not indexed, the product codes/numbers are alphanumeric and sorted only to product code (Col1). Iテδ「テ「堋ャテ「楪「m getting more and more confused, bhaah! Mats "Dave Peterson" wrote: Did you put the code behind the worksheet with the comboboxes? Did you use comboboxes from the control toolbox toolbar? If you put a breakpoint in the combobox1 change code, can you see why combobox2 is not be populated? I'm not sure why iserror(combobox2.value) ever comes into play. Mats Samson wrote: Thank you Dave, it works quite fine the way I set it up for you, but with a small discrepancy plus that I run into another problem! The Product CB never shows any values, only when you push the drop-down list so you can select the items, thatテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「s the discrepancy! In my original setup I had the following piece of code for CB2: Private Sub ComboBox2_Change() If (IsError(ComboBox2.Value)) Then Range("B3") = ComboBox2.Text Else Range("B3") = ComboBox2.Text Range("A10") = ComboBox2.Value End If End Sub The reason for this part is that I want to be able to write a product that is not listed in the product database. Usually the cell A10 gets the product number and itテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「s used for further processing, to orders and invoices etc. But if I just want to calculate for a product we might be doing business with in the future, I donテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「t want to register it. I only save the calculation so I can retrieve it again later. For that I need some reference, i.e. the テθ津つ「テδ「テ「ぎナ。テつャテムヲテ「ぎナ砥nregisteredテθ 津つ「テδ「テ「ぎナ。テつャテム堙つ product name. So I had to put in this piece of code to get the other cells to display the product name correctly at any time, otherwise it will display, #N/A. So the question is, can they be combined? Best regards Mats "Dave Peterson" wrote: This goes behind the ThisWorkbook Module: Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Change those category# to what you need--and add as many as you need. Then put this behind the Calculation worksheet: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("Products") Set myRng = .Range("c1", .Cells(.Rows.Count, "c").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, -2).Value End If Next myCell End Sub ===== This line took the value from column A: Me.ComboBox2.AddItem myCell.Offset(0, -2).Value If you wanted the value from column B: Me.ComboBox2.AddItem myCell.Offset(0, -1).Value Mats Samson wrote: Hi Dave, Iテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム堙つ ャテθ津つ「テδ「テ「堋ャテツセテム堙つ「m sorry but I was travelling a lot and didnテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム テつャテθ津つ「テδ「テ「堋ャテツセテム堙つ「t have any time to go through your proposal until now, but I donテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム堙 つャテθ津つ「テδ「テ「堋ャテツセテム堙つ「t understand how to apply it in my case. I have a sheet named Calculation where I have the two CBテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム堙 ツャテθ津つ「テδ「テ「堋ャテツセテム堙つ「s, one for the product names and one for the product categories. In the sheet Products, I have my small product database where column A is my product number (NOT an index number), column B is the product name and column C is the category (Plastics, Fillers, Solventsテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テ ム堙つャテθ津「ぎナ。テム堙つヲ..). There are more columns but not important for this question. There are about 200 products belonging to one of 6 categories and as you understand when I select the product, I want to limit the list to the selected category, so I donテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム堙 つャテθ津つ「テδ「テ「堋ャテツセテム堙つ「t need to scroll the entire 200 products list. Can you explain how to accomplish this? Thanks Mats "Dave Peterson" wrote: Instead of this portion: If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If Try using: Me.ComboBox1.clear Dave Peterson wrote: I put two comboboxes from the control toolbox toolbar on a worksheet. |
#15
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
So the code didn't work either.
I guess I don't understand what you want. Sorry. Mats Samson wrote: Thats exactly what I dont want to do! The reason is that column A and B are used to put all the conditions for the business calculation, deliveries, quantity, purchase price, currencies, freight, insurances, etc, etc,ヲ..A calculation contains parameters of different kind in nearly all cells in the range A3:B28. The actual calculation is taking place in columns F to J. The Column A and B VALUES are saved in a 徨egister and because there are so many parameters they are saved in two lines to be readable when you look into the saved calculations list. I can retrieve an old calculation by copying it back to the calculation sheet, transposing 2 lines back to 2 columns and pasting VALUES only. Any formula in A10 will be destroyed by a retrieved calculation. Accordingly the A10 value has to be set by a function/code, not a formula. Regards Mats "Dave Peterson" wrote: Why not use a formula to return the product code? Put this in A10: =if(b3="","",index(products!b:b,match(b3,products! c:c,0)) When you populuate B3 with the product name, the formula will evaluate to the product code. ========== Alternatively... I put this behind the ThisWorkbook module: Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With With Worksheets("Calculation").ComboBox2 .Boundcolumn = 1 .Columncount = 2 .ColumnWidths = "0 pt;130 pt" End With End Sub I put this behind the calculation worksheet module: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("Products") Set myRng = .Range("c1", .Cells(.Rows.Count, "c").End(xlUp)) End With With Me.ComboBox2 .Clear For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then .AddItem myCell.Offset(0, -2).Value .List(.ListCount - 1, 1) = myCell.Offset(0, -1).Value End If Next myCell End With End Sub Private Sub ComboBox2_Change() With Me.ComboBox2 If .ListIndex < 0 Then Me.Range("b3,a10").ClearContents Else Me.Range("b3").Value = .List(.ListIndex, 0) Me.Range("a10").Value = .List(.ListIndex, 1) End If End With End Sub I'm not sure if I got the product code/item number in the right position. But that shouldn't be too difficult to fix. Notice that I did add a .clear in the combobox1_change event. No matter which routine (today's or yesterday's), I would think that this would be a good thing to have. Mats Samson wrote: Hi Dave, sorry, I forgot to mention that I already dropped the ListFillRange and the other properties of the CB. It was of course necessary, otherwise it wonテ「ぎ┐t work at all! But the reason why it didnテ「ぎ┐t work was that I forgot to remove the TextColumn property that was set to 2. So now it works OK and CB2 displays the selected item. This TextColumn setting belonged to my original setup through which I got 2 テ「ぎナ砺aluesテ「ぎツ out of one CB. The product name as the Text property and the Product code as the Value property at the same time it served as index for the other values in a record. Then I could look up other values of the particular record, like supplier and units, etc. and present them in my calculation) The only remaining problem is that since we kind of removed the テ「ぎナ田onnectionテ「ぎツ to the product database and only creating a list based on a criteria, I cannot get the product code into cell A10. It must not be a formula in the cell, only the value. Can we do that? When I select the product name (ColB) that goes to B3 I get at the same time the Product code from ColA going into A10? I would appreciate your help very much! Best regards Mats "Dave Peterson" wrote: This code in the ThisWorkbook module is used instead of the code in your auto_open procedu Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Pick one and use that. Also, try dropping the linked cell and assigning the value directly in code. And get rid of those properties in the combobox2. Instead of using listfillrange, the code is looping through the list to get the items to display in that combobox. Mats Samson wrote: Q1: Yes, Sir! Q2: Yepp! The only difference I used was to create an array with the product categories in an Auto_Open module of the workbook: With Worksheets("Calculation").ComboBox1 .Clear .List = PGrp テδ「テ「堋ャテ暁 the array of categories .LinkedCell = "C3" End With I also had to include: Me.ComboBox1.Clear before the For Each テδ「テ「堋ャテつヲテδ「テ「堋ャテつヲ. Otherwise new items was added to the previous list of products for every new selection of category. The CB is populated alright but only the in dropdown-state, there is no value showing in the テδ「テ「堋ャテ徙ff-focus stateテδ「テ「堋ャテつ, before or after the selection?!?!?! Well, the reason for IsError is that the CB2 linked cell B3 will display #N/A if I write something in the CB instead of selecting an item in the list. My original properties for CB2 a Boundcolumn: 1 Columncount: 2 ColumnWidths: 0 pt;130 pt LinkedCell: B3 ListFillRange: PDB テδ「テ「堋ャテ暁典he product database defined as a named range. With this setup I get the CB2 populated with the product names (Col2) that goes to B3 as CB2.Text and the productcode (Col1) is the CB2.Value that goes to A10 (through the CB2.Change () procedure). If I write a new product the CB2.Text will still write to B3 but A10 will go empty. When the calculation is saved, to another workbook, the cell related to A10 will display テδ「テ「堋ャテ廸ewテδ「テ「堋ャテつ (through formula). (I tried to put Range (テδ「テ「堋ャテ廣10テδ「テ「堋ャテつ) = テδ「テ「堋ャテ廸ewテδ「テ「堋ャテつ in the error-true part of CB2.Change, but it doesnテδ「テ「堋ャテ「楪「t work! A10 is always empty unless a registered product, with product code, is used) The products are not indexed, the product codes/numbers are alphanumeric and sorted only to product code (Col1). Iテδ「テ「堋ャテ「楪「m getting more and more confused, bhaah! Mats "Dave Peterson" wrote: Did you put the code behind the worksheet with the comboboxes? Did you use comboboxes from the control toolbox toolbar? If you put a breakpoint in the combobox1 change code, can you see why combobox2 is not be populated? I'm not sure why iserror(combobox2.value) ever comes into play. Mats Samson wrote: Thank you Dave, it works quite fine the way I set it up for you, but with a small discrepancy plus that I run into another problem! The Product CB never shows any values, only when you push the drop-down list so you can select the items, thatテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「s the discrepancy! In my original setup I had the following piece of code for CB2: Private Sub ComboBox2_Change() If (IsError(ComboBox2.Value)) Then Range("B3") = ComboBox2.Text Else Range("B3") = ComboBox2.Text Range("A10") = ComboBox2.Value End If End Sub The reason for this part is that I want to be able to write a product that is not listed in the product database. Usually the cell A10 gets the product number and itテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「s used for further processing, to orders and invoices etc. But if I just want to calculate for a product we might be doing business with in the future, I donテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「t want to register it. I only save the calculation so I can retrieve it again later. For that I need some reference, i.e. the テθ津つ「テδ「テ「ぎナ。テつャテムヲテ「ぎナ砥nregisteredテθ 津つ「テδ「テ「ぎナ。テつャテム堙つ product name. So I had to put in this piece of code to get the other cells to display the product name correctly at any time, otherwise it will display, #N/A. So the question is, can they be combined? Best regards Mats "Dave Peterson" wrote: This goes behind the ThisWorkbook Module: Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Change those category# to what you need--and add as many as you need. Then put this behind the Calculation worksheet: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("Products") Set myRng = .Range("c1", .Cells(.Rows.Count, "c").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, -2).Value End If Next myCell End Sub ===== This line took the value from column A: Me.ComboBox2.AddItem myCell.Offset(0, -2).Value If you wanted the value from column B: Me.ComboBox2.AddItem myCell.Offset(0, -1).Value Mats Samson wrote: Hi Dave, Iテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム堙つ ャテθ津つ「テδ「テ「堋ャテツセテム堙つ「m sorry but I was travelling a lot and didnテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム テつャテθ津つ「テδ「テ「堋ャテツセテム堙つ「t have any time to go through your proposal until now, but I donテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム堙 つャテθ津つ「テδ「テ「堋ャテツセテム堙つ「t understand how to apply it in my case. I have a sheet named Calculation where I have the two CBテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム堙 ツャテθ津つ「テδ「テ「堋ャテツセテム堙つ「s, one for the product names and one for the product categories. In the sheet Products, I have my small product database where column A is my product number (NOT an index number), column B is the product name and column C is the category (Plastics, Fillers, Solventsテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テ ム堙つャテθ津「ぎナ。テム堙つヲ..). There are more columns but not important for this question. There are about 200 products belonging to one of 6 categories and as you understand when I select the product, I want to limit the list to the selected category, so I donテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム堙 つャテθ津つ「テδ「テ「堋ャテツセテム堙つ「t need to scroll the entire 200 products list. Can you explain how to accomplish this? Thanks Mats "Dave Peterson" wrote: Instead of this portion: If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If Try using: Me.ComboBox1.clear Dave Peterson wrote: I put two comboboxes from the control toolbox toolbar on a worksheet. -- Dave Peterson |
#16
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Oh yes, it works! If you mean the code discussion, yes perfectly alright!
But the formula doesnt, as explained above. The remaining problem is that I want the product code as well, at the same time as I choose the product. The product codes you can design so the connected product comes in blocks, in my case supplier wise. Its simple database handling, whether its products, customer or suppliers, you need a unique common denominator. We created a new problem when extracting a list out of the product db, we lost touch with the 彿ndex value, the product code. Still, I want the product code as well, for the other formulas to work correctly. If you need further explanation, Id better send you my files so you can see what I want to achieve. In such case, please send me an email address where I can reach you. Best regards Mats "Dave Peterson" wrote: So the code didn't work either. I guess I don't understand what you want. Sorry. Mats Samson wrote: Thatテ「ぎ┐s exactly what I donテ「ぎ┐t want to do! The reason is that column A and B are used to put all the conditions for the business calculation, deliveries, quantity, purchase price, currencies, freight, insurances, etc, etc,テ「ぎツヲ..A calculation contains parameters of different kind in nearly all cells in the range A3:B28. The actual calculation is taking place in columns F to J. The Column A and B VALUES are saved in a テ「ぎナ途egisterテ「ぎツ and because there are so many parameters they are saved in two lines to be readable when you look into the saved calculationテ「ぎ┐s list. I can retrieve an old calculation by copying it back to the calculation sheet, transposing 2 lines back to 2 columns and pasting VALUES only. Any formula in A10 will be destroyed by a retrieved calculation. Accordingly the A10 value has to be set by a function/code, not a formula. Regards Mats "Dave Peterson" wrote: Why not use a formula to return the product code? Put this in A10: =if(b3="","",index(products!b:b,match(b3,products! c:c,0)) When you populuate B3 with the product name, the formula will evaluate to the product code. ========== Alternatively... I put this behind the ThisWorkbook module: Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With With Worksheets("Calculation").ComboBox2 .Boundcolumn = 1 .Columncount = 2 .ColumnWidths = "0 pt;130 pt" End With End Sub I put this behind the calculation worksheet module: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("Products") Set myRng = .Range("c1", .Cells(.Rows.Count, "c").End(xlUp)) End With With Me.ComboBox2 .Clear For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then .AddItem myCell.Offset(0, -2).Value .List(.ListCount - 1, 1) = myCell.Offset(0, -1).Value End If Next myCell End With End Sub Private Sub ComboBox2_Change() With Me.ComboBox2 If .ListIndex < 0 Then Me.Range("b3,a10").ClearContents Else Me.Range("b3").Value = .List(.ListIndex, 0) Me.Range("a10").Value = .List(.ListIndex, 1) End If End With End Sub I'm not sure if I got the product code/item number in the right position. But that shouldn't be too difficult to fix. Notice that I did add a .clear in the combobox1_change event. No matter which routine (today's or yesterday's), I would think that this would be a good thing to have. Mats Samson wrote: Hi Dave, sorry, I forgot to mention that I already dropped the ListFillRange and the other properties of the CB. It was of course necessary, otherwise it wonテδ「テ「堋ャテ「楪「t work at all! But the reason why it didnテδ「テ「堋ャテ「楪「t work was that I forgot to remove the TextColumn property that was set to 2. So now it works OK and CB2 displays the selected item. This TextColumn setting belonged to my original setup through which I got 2 テδ「テ「堋ャテ忻aluesテδ「テ「堋ャテつ out of one CB. The product name as the Text property and the Product code as the Value property at the same time it served as index for the other values in a record. Then I could look up other values of the particular record, like supplier and units, etc. and present them in my calculation) The only remaining problem is that since we kind of removed the テδ「テ「堋ャテ彡onnectionテδ「テ「堋ャテつ to the product database and only creating a list based on a criteria, I cannot get the product code into cell A10. It must not be a formula in the cell, only the value. Can we do that? When I select the product name (ColB) that goes to B3 I get at the same time the Product code from ColA going into A10? I would appreciate your help very much! Best regards Mats "Dave Peterson" wrote: This code in the ThisWorkbook module is used instead of the code in your auto_open procedu Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Pick one and use that. Also, try dropping the linked cell and assigning the value directly in code. And get rid of those properties in the combobox2. Instead of using listfillrange, the code is looping through the list to get the items to display in that combobox. Mats Samson wrote: Q1: Yes, Sir! Q2: Yepp! The only difference I used was to create an array with the product categories in an Auto_Open module of the workbook: With Worksheets("Calculation").ComboBox1 .Clear .List = PGrp テθ津つ「テδ「テ「ぎナ。テつャテムケテ the array of categories .LinkedCell = "C3" End With I also had to include: Me.ComboBox1.Clear before the For Each テθ津つ「テδ「テ「ぎナ。テつャテム堙つヲテθ津つ「テδ「テ「ぎナ 。テつャテム堙つヲ. Otherwise new items was added to the previous list of products for every new selection of category. The CB is populated alright but only the in dropdown-state, there is no value showing in the テθ津つ「テδ「テ「ぎナ。テつャテムヲテ「ぎナ登ff-focus stateテθ津つ「テδ「テ「ぎナ。テつャテム堙つ, before or after the selection?!?!?! Well, the reason for IsError is that the CB2 linked cell B3 will display #N/A if I write something in the CB instead of selecting an item in the list. My original properties for CB2 a Boundcolumn: 1 Columncount: 2 ColumnWidths: 0 pt;130 pt LinkedCell: B3 ListFillRange: PDB テθ津つ「テδ「テ「ぎナ。テつャテムケテ弋he product database defined as a named range. With this setup I get the CB2 populated with the product names (Col2) that goes to B3 as CB2.Text and the productcode (Col1) is the CB2.Value that goes to A10 (through the CB2.Change () procedure). If I write a new product the CB2.Text will still write to B3 but A10 will go empty. When the calculation is saved, to another workbook, the cell related to A10 will display テθ津つ「テδ「テ「ぎナ。テつャテムヲテ「ぎナ哲ewテθ津つ「テδ「 テ「ぎナ。テつャテム堙つ (through formula). (I tried to put Range (テθ津つ「テδ「テ「ぎナ。テつャテムヲテ「ぎナ鄭10テθ津つ「テδ 「テ「ぎナ。テつャテム堙つ) = テθ津つ「テδ「テ「ぎナ。テつャテムヲテ「ぎナ哲ewテθ津つ「テδ「 テ「ぎナ。テつャテム堙つ in the error-true part of CB2.Change, but it doesnテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「t work! A10 is always empty unless a registered product, with product code, is used) The products are not indexed, the product codes/numbers are alphanumeric and sorted only to product code (Col1). Iテθ津つ「テδ「テ「ぎナ。テつャテδ「テ「ぎナセテつ「m getting more and more confused, bhaah! Mats "Dave Peterson" wrote: Did you put the code behind the worksheet with the comboboxes? Did you use comboboxes from the control toolbox toolbar? If you put a breakpoint in the combobox1 change code, can you see why combobox2 is not be populated? I'm not sure why iserror(combobox2.value) ever comes into play. Mats Samson wrote: Thank you Dave, it works quite fine the way I set it up for you, but with a small discrepancy plus that I run into another problem! The Product CB never shows any values, only when you push the drop-down list so you can select the items, thatテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム テつャテθ津つ「テδ「テ「堋ャテツセテム堙つ「s the discrepancy! In my original setup I had the following piece of code for CB2: Private Sub ComboBox2_Change() If (IsError(ComboBox2.Value)) Then Range("B3") = ComboBox2.Text Else Range("B3") = ComboBox2.Text Range("A10") = ComboBox2.Value End If End Sub The reason for this part is that I want to be able to write a product that is not listed in the product database. Usually the cell A10 gets the product number and itテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム堙 ツャテθ津つ「テδ「テ「堋ャテツセテム堙つ「s used for further processing, to orders and invoices etc. But if I just want to calculate for a product we might be doing business with in the future, I donテθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム堙 つャテθ津つ「テδ「テ「堋ャテツセテム堙つ「t want to register it. I only save the calculation so I can retrieve it again later. For that I need some reference, i.e. the テθ津テム堙つ「テθ津つ「テδ「テ「堋ャテツ。テム堙つャ テθ津「ぎツヲテδ「テ「堋ャテ忖nregisteredテθ津テ 堙つ「テθ津つ「テδ「テ「堋ャテツ。テム堙つャテθ津「ぎナ。 テム堙つ product name. So I had to put in this piece of code to get the other cells to display the product name correctly at any time, otherwise it will display, #N/A. So the question is, can they be combined? Best regards Mats "Dave Peterson" wrote: This goes behind the ThisWorkbook Module: Option Explicit Private Sub Workbook_Open() With Worksheets("Calculation").ComboBox1 .Clear .AddItem "category1" .AddItem "category2" .AddItem "category3" .AddItem "category4" End With End Sub Change those category# to what you need--and add as many as you need. Then put this behind the Calculation worksheet: Option Explicit Private Sub ComboBox1_Change() Dim myRng As Range Dim myCell As Range If Me.ComboBox1.ListIndex < 0 Then Me.ComboBox2.ListIndex = -1 End If With Worksheets("Products") Set myRng = .Range("c1", .Cells(.Rows.Count, "c").End(xlUp)) End With For Each myCell In myRng.Cells If LCase(myCell.Value) = LCase(Me.ComboBox1.Value) Then Me.ComboBox2.AddItem myCell.Offset(0, -2).Value End If Next myCell End Sub ===== This line took the value from column A: Me.ComboBox2.AddItem myCell.Offset(0, -2).Value If you wanted the value from column B: Me.ComboBox2.AddItem myCell.Offset(0, -1).Value Mats Samson wrote: Hi Dave, Iテθ津テム*テ「ぎ┐テθ津「ぎナ。テム堙つ「テθ津 テム堙つ「テθ津つ「テδ「テ「ぎナ。テつャテムヲテつ。テθ津「 ぎナ。テム堙つャテθ津テム堙つ「テθ津つ「テδ「テ「ぎナ。 テつャテムヲテつセテθ津「ぎナ。テム堙つ「m sorry but I was travelling a lot and didnテθ津テム*テ「ぎ┐テθ津「ぎナ。テム堙つ「テθ テテム堙つ「テθ津つ「テδ「テ「ぎナ。テつャテムヲテつ。テθ テ「ぎナ。テム堙つャテθ津テム堙つ「テθ津つ「テδ「テ「 ャナ。テつャテムヲテつセテθ津「ぎナ。テム堙つ「t have any time to go through your proposal until now, but I donテθ津テム*テ「ぎ┐テθ津「ぎナ。テム堙つ「テθ津 テム堙つ「テθ津つ「テδ「テ「ぎナ。テつャテムヲテつ。テθ津 「ぎナ。テム堙つャテθ津テム堙つ「テθ津つ「テδ「テ「ぎ ナ。テつャテムヲテつセテθ津「ぎナ。テム堙つ「t understand how to apply it in my case. |
#17
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I don't open attachments or workbooks from others.
I thought that the code populated both the product description and product code. Mats Samson wrote: Oh yes, it works! If you mean the code discussion, yes perfectly alright! But the formula doesnt, as explained above. The remaining problem is that I want the product code as well, at the same time as I choose the product. The product codes you can design so the connected product comes in blocks, in my case supplier wise. Its simple database handling, whether its products, customer or suppliers, you need a unique common denominator. We created a new problem when extracting a list out of the product db, we lost touch with the 彿ndex value, the product code. Still, I want the product code as well, for the other formulas to work correctly. If you need further explanation, Id better send you my files so you can see what I want to achieve. In such case, please send me an email address where I can reach you. Best regards Mats |
#18
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
OK!
But I think I can use a HLookup formula to get the leftmost cell-value in the row of the selected CB2-value. If you have any ideas youre heartily welcome! Regards Mats "Dave Peterson" wrote: I don't open attachments or workbooks from others. I thought that the code populated both the product description and product code. Mats Samson wrote: Oh yes, it works! If you mean the code discussion, yes perfectly alright! But the formula doesnテ「ぎ┐t, as explained above. The remaining problem is that I want the product code as well, at the same time as I choose the product. The product codes you can design so the connected product comes in blocks, in my case supplier wise. Itテ「ぎ┐s simple database handling, whether itテ「ぎ┐s products, customer or suppliers, you need a unique common denominator. We created a new problem when extracting a list out of the product db, we lost touch with the テ「ぎナ妬ndex valueテ「ぎツ, the product code. Still, I want the product code as well, for the other formulas to work correctly. If you need further explanation, Iテ「ぎ┐d better send you my files so you can see what I want to achieve. In such case, please send me an email address where I can reach you. Best regards Mats |
#19
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Depending on how many fields are used, why not just keep adding them to the
combobox list (but hide them). That's what that code did. Mats Samson wrote: OK! But I think I can use a HLookup formula to get the leftmost cell-value in the row of the selected CB2-value. If you have any ideas youre heartily welcome! Regards Mats "Dave Peterson" wrote: I don't open attachments or workbooks from others. I thought that the code populated both the product description and product code. Mats Samson wrote: Oh yes, it works! If you mean the code discussion, yes perfectly alright! But the formula doesnテ「ぎ┐t, as explained above. The remaining problem is that I want the product code as well, at the same time as I choose the product. The product codes you can design so the connected product comes in blocks, in my case supplier wise. Itテ「ぎ┐s simple database handling, whether itテ「ぎ┐s products, customer or suppliers, you need a unique common denominator. We created a new problem when extracting a list out of the product db, we lost touch with the テ「ぎナ妬ndex valueテ「ぎツ, the product code. Still, I want the product code as well, for the other formulas to work correctly. If you need further explanation, Iテ「ぎ┐d better send you my files so you can see what I want to achieve. In such case, please send me an email address where I can reach you. Best regards Mats -- Dave Peterson |
#20
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Dave!
I found the solution in your suggested formula: myPC = Application.Index(myWS.Range("a:a"), Application.Match(myPN, myWS.Range("b:b"), 0)) myPC refers to cell A10 of course. Thanks a mille! Mats "Dave Peterson" wrote: Depending on how many fields are used, why not just keep adding them to the combobox list (but hide them). That's what that code did. Mats Samson wrote: OK! But I think I can use a HLookup formula to get the leftmost cell-value in the row of the selected CB2-value. If you have any ideas youテ「ぎ┐re heartily welcome! Regards Mats "Dave Peterson" wrote: I don't open attachments or workbooks from others. I thought that the code populated both the product description and product code. Mats Samson wrote: Oh yes, it works! If you mean the code discussion, yes perfectly alright! But the formula doesnテδ「テ「堋ャテ「楪「t, as explained above. The remaining problem is that I want the product code as well, at the same time as I choose the product. The product codes you can design so the connected product comes in blocks, in my case supplier wise. Itテδ「テ「堋ャテ「楪「s simple database handling, whether itテδ「テ「堋ャテ「楪「s products, customer or suppliers, you need a unique common denominator. We created a new problem when extracting a list out of the product db, we lost touch with the テδ「テ「堋ャテ彿ndex valueテδ「テ「堋ャテつ, the product code. Still, I want the product code as well, for the other formulas to work correctly. If you need further explanation, Iテδ「テ「堋ャテ「楪「d better send you my files so you can see what I want to achieve. In such case, please send me an email address where I can reach you. Best regards Mats -- Dave Peterson |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
fill combobox depending on selection from another combobox | Excel Discussion (Misc queries) | |||
sort/filter combobox | Excel Discussion (Misc queries) | |||
ComboBox list reliant on the entry from a different ComboBox | Excel Programming | |||
Tricky ComboBox / Filter query | Excel Discussion (Misc queries) | |||
How Do I Load A ComboBox RowSource From The Results Of Another ComboBox | Excel Programming |