如果F列包含“ A”,则使C列中的单元格显示“ Apples”

时间:2019-04-22 15:21:04

标签: excel vba

我试图在以下情况下创建VBA代码:

  1. 如果F列显示“ Anna”,则C列显示“ Apples”
  2. 如果F列显示“ Mike”,“ Jake”,“ Dan”,“ Angie”,则C列显示“ Banana”
  3. 如果F列显示“莫莉”,则C列将显示“橙色”
  4. 如果F列显示“ Tony”,“ Kevin”,则C列显示“ Kiwi”

2 个答案:

答案 0 :(得分:0)

您可以尝试:

Option Explicit

Sub test()

    With ThisWorkbook.Worksheets("Sheet1")

        If .Range("F1").Value = "Anna" Then
            .Range("C1").Value = "Apples"
        ElseIf .Range("F1").Value = "Mike" Or .Range("F1").Value = "Jake" Or .Range("F1").Value = "Dan" Or .Range("F1").Value = "Angie" Then
            .Range("C1").Value = "Banana"
        ElseIf .Range("F1").Value = "Molly" Then
            .Range("C1").Value = "Orange"
        ElseIf .Range("F1").Value = "Tony" Or .Range("F1").Value = "Kevin" Then
            .Range("C1").Value = "Kiwi"
        End If

    End With

End Sub

答案 1 :(得分:0)

您可以尝试

Sub fruit()

Dim name As String
Dim lastrow As Long
Dim i As Long

lastrow = Cells(Rows.Count, 6).End(xlUp).Row

For i = 1 To lastrow

    name = Cells(i, 6).Value
    Select Case name

    Case "Anna"
        Cells(i, 3).Value = "Apples"

    Case "Mike", "Jake", "Dan", "Angie"
        Cells(i, 3).Value = "Banana"

    Case "Molly"
        Cells(i, 3).Value = "Orange"

    Case "Tony", "Kevin"
        Cells(i, 3).Value = "Kiwi"

    Case Else
        Cells(i, 3).Value = "not defined"

    End Select

Next i

End Sub