如何根据多个单元格中的值自动填充

How to autofill based on values in multiple cells

第一个问题,希望我做对了...

我在单元格中有一系列数据A:C 让我们举个例子:

单元格 D3 中,我希望将其 Autofill 作为 'Fruit'。如果 A:C 中的任何单元格不是这些选择之一,return 空白。 目的是根据这些组合为单元格 A:C 和 return 设置某些值的下拉列表。使用 VBA 而不是公式怎么可能轻松做到这一点?

我可以对单个细胞执行此操作,绝不能对多个细胞执行此操作。

在下面的代码中,我使用 步骤 2 每两行测试一次值。如果你想测试每一行,你可以删除它。

Option Explicit

Sub test()

    Dim LastRow As Long, Row As Long

    With ThisWorkbook.Worksheets("Sheet1")

        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

        For Row = 2 To LastRow Step 2

            If .Range("A" & Row).Value = "Apple" Or .Range("A" & Row).Value = "Banana" Or .Range("A" & Row).Value = "Tomato" Then
                If .Range("B" & Row).Value = "Apple" Or .Range("B" & Row).Value = "Banana" Or .Range("B" & Row).Value = "Tomato" Then
                    If .Range("C" & Row + 1).Value = "Apple" Or .Range("C" & Row + 1).Value = "Banana" Or .Range("C" & Row + 1).Value = "Tomato" Then
                        .Range("D" & Row + 1).Value = "Fruits"
                    End If
                Else
                    Exit For
                End If
            Else
                Exit For
            End If

        Next Row

    End With

End Sub