将if + or +间接函数转换为vba

时间:2018-07-21 01:11:57

标签: excel vba excel-vba if-statement excel-indirect

您好,我正在尝试转换此函数:

=IF(OR(INDIRECT("'sheet1'!C8")="Nouveau locataire",INDIRECT("'sheet1'!C8")="Décès"),CELL("contents",INDIRECT("'sheet1'!B8")),"")

类似于:

Sub if_orfuction()

Dim i As Integer
j = 2

    For i = 2 To Sheets("Sheet1").Range("A1").SpecialCells(xlLastCell).Row

        If Sheets("Sheet1").Cells(i, 2).Value = "Nouveau Locataire" Or Sheets("Sheet1").Cells(i, 2).Value = "Décès" Then

            Sheets("Sheet3").Cells(j, 1) = Sheets("Sheet1").Cells(i, 1)
            j = j + 1

        End If

    Next i


End Sub

我该如何实现间接功能?

1 个答案:

答案 0 :(得分:1)

尝试

Sub if_orfuction()

    Dim i As long,  j as long

    j = 2
    with workSheets("Sheet1")
        For i = 2 To .cells.SpecialCells(xlLastCell).Row

            If .range(.range("B" & i).Value).Value = "Nouveau Locataire" Or _
               .range(.range("B" & i).Value).Value = "Décès" Then

                workSheets("Sheet3").Cells(j, "A") = .Cells(i, "A").Value
                j = j + 1

            End If

        Next i
    end with

End Sub