VBA - 每个单元格的多个条件

时间:2017-06-19 14:07:49

标签: excel vba if-statement multiple-conditions

我正在尝试解决此代码的问题,我无法运行:

'========================================================================
' CHECKS IF MARKET SECTOR IS EMPTY (FOR LEDGER)
'========================================================================

Private Sub Fill_MarketSector()

Dim LastRow As Long
Dim rng As Range, C As Range

With Worksheets("Ready to upload") ' <-- here should be the Sheet's name
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' last row in column A
    Set rng = .Range("A2:A" & LastRow) ' set the dynamic range to be searched
    Set rng2 = .Range("F2:F" & LastRow)

    ' loop through all cells in column A and column F
    For Each C In rng and For Each C in rng2
        If rng.C.Value = "Ledger" and rng2.C.value IsEmpty Then
            C.Value = "599" ' use offset to put the formula on column "L"
        End If
    Next C
End With

End Sub

代码应该检查A列是否包含单词“Ledger”而F列是否为空,然后它应该放入F“599”列。它应该始终检查到最后一行。你能帮帮我吗?

非常感谢!

1 个答案:

答案 0 :(得分:0)

您可以通过循环A列中的单元格并使用.Offset作为F列来访问F列中的伴随单元格,然后再次偏移以将值放在L列中。

' loop through all cells in column A and column F
For Each C In rng
    If LCase(C.Value) = "ledger" and IsEmpty(C.Offset(0, 5) Then
        C.Offset(0, 11) = 599  'use offset to put the number on column "L"
    End If
Next C