0
Col A  Col B
Fruit  Grapes
Fruit  Mango 
Fruit  Mango
Veg    Carrot
Veg    Brinjal
Fruit  Banana
Veg    Carrot

我有类似的要求,如this thread set drop-down values based on vlookup中所示

这很好用,但是现在对我的要求是在 B 列中会有重复项,并且下拉列表必须只显示不同的值。谁能帮我这个

请参阅此处的文件

我想通过纯 Excel 公式而不是 VBA 代码来实现这一点。

4

1 回答 1

1

Part 1 - This just shows how to create a unique list in a drop-down.

Here's a screenshot, but currently it requires that there is a row above the data(?). This provides a unique list of items in a Data Validation drop-down. (All the formulas below can be made easier to create by naming some ranges beforehand.)

enter image description here

The array formula in E2 is shown in the comment. Use Ctrl-Shift-Enter to enter an array formula, then drag this down as far as necessary - the #N/As will begin to appear at the bottom.

Gosh, it's hard to describe :). The COUNTIF essentially generates a sequence of 1s and 0s to indicate whereabouts in column B the previous (above) values in column E are positioned in column B. This sequence always starts with a 0 (for the formula in E2) because it's looking for a blank which doesn't occur in column B - so this will grab the first item, Grapes.

The MATCH then finds the first 0 in this sequence of 0s and 1s, which indicates that a value (in column B) hasn't already appeared in column E.

INDEX then uses this MATCHed value to retrieve the new unique item from column B.

Then a Defined Name is created (on the Formulas tab) that gets all the values in column E, but only down to the first occurrence of '#', which indicates there are no more unique values in the list.

This Defined Name is then used in the Data Validation.

Part 2 - The Answer (with VBA)

The following VBA code is required so that clicking in a cell in C11:C18 will change the value in F1, which generates the unique list underneath, which populates the data-validation list in C11:C18.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Application.Intersect(Target, Range("C11:C18")) Is Nothing Then
        Sheets("Sheet1").Range("F1").Value = ActiveCell.Offset(0, -1).Value
    End If
End Sub

It requires the formulas and defined-name as shown in the following screenshot. The formula in F2 is different to F3, which is then copied down.

The number 10 in the formula in F3 is just a large number (of rows).

enter image description here

  • Column E are the unique items.
  • Column D are the corresponding categories.
  • Column F are the unique items for the category in cell F1 (extracted form column E).

Part 3 - Without VBA

Use the formula =INDIRECT(ADDRESS(CELL("row"),CELL("col")-1)) in cell F1. When you click into a cell in C11:C18 you need to press F9 to recalculate the worksheet, which updates the drop-down list.

Recalculating is necessary to update the CELL("row") and CELL("col") values.

Part 4 - Without pressing F9

It can be achieved without having to press F9, but it means spreading all the categories across different columns (G1 and to the right in the below screenshot). This can also be achieved with the array formula =INDEX($A$2:$A$8,MATCH(0,COUNTIF($F$1:F1,$A$2:$A$8),0)) in G1.

The #N/As can also be removed the the drop-down lists with strategic use of IFERROR() in the formulas from G2 onwards, substituting "". Alternatively, using:

=OFFSET(Sheet1!$G$1,1,MATCH(Sheet1!B11,Sheet1!$G$1:$J$1,0)-1,MATCH("#",OFFSET(Sheet1!$G$1,1,MATCH(Sheet1!B11,Sheet1!$G$1:$J$1,0)-1,COUNTA(Sheet1!$G:$G)-1,1),-1),1)

as the defined-name Items would not only remove the #N/As but also the redundant ("") values that IFERROR() would leave in the drop-down lists. (The cursor needs to be positioned in C11 when creating this defined-name.)

enter image description here

Apologies for the length of this, but hope it is of interest to someone.

于 2013-10-15T19:28:59.340 回答