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

VBA - multiple conditions for each cell

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

'========================================================================
' 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”。它应该始终检查到最后一行。你能帮帮我吗?

非常感谢!

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

' 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