【问题标题】:How to autofill based on values in multiple cells如何根据多个单元格中的值自动填充
【发布时间】:2019-03-28 09:50:08
【问题描述】:

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

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

  • 单元格 A2 = 苹果
  • 单元格 B2 = 香蕉
  • 单元格 C3 = 番茄

Cell D3 中,我希望将其 Autofill 设置为“水果”。如果 A:C 中的任何单元格不是这些选择之一,则返回空白。 目的是为单元格 A:C 提供下拉列表,并根据它们的组合返回某些值。使用 VBA 而不是公式,这怎么可能轻松实现?

我可以对单个单元格执行此操作,而不是多个单元格。

【问题讨论】:

  • 欢迎来到 SO。你已经用 VBA 标记了这个问题,所以请发布你尝试过的代码。

标签: excel vba autofill


【解决方案1】:

在下面的代码中,我使用 Step 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 2022-01-09
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多