2017-06-19 98 views
-1

我试图解决这个代码的问题,我不能运行多个条件:VBA - 每个细胞

'======================================================================== 
' 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列中包含单词“总帐”和F列是空的,那么它应该放在F栏“599”中。它应该总是检查到最后一行。请问你能帮帮我吗?

非常感谢!

+0

重:“\ *。 '应放入F栏“599”'* - F栏或L栏?你的叙述与你的代码中的评论冲突。 – Jeeped

+0

你有什么错误?提供有关您卡在哪里的信息。 – PankajR

+0

你是对的,我在评论和代码之间有差异。它应该将“599”放在F列中。 我收到语法错误,并且这部分代码已被突出显示: 对于每个C In rng和对于每个C in rng2 如果rng.C.Value =“Ledger “和rng2.C.value IsEmpty然后 – Srpic

回答

0

您可以通过细胞在A列循环使用.Offset为F列,然后再偏移把值列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 
+0

谢谢你!它工作顺利。 IsEmpty后面只剩下分隔符(C.Offset(0,5))。非常感谢你! – Srpic

+0

不幸的是,它只有时才起作用,大部分时间它什么都不做。你知道为什么吗? – Srpic

+0

*'它只有时才有效'* - 这既不是有效的错误代码,也不是有效的错误描述。这也不是 - *'大部分时间,它什么都不做'*。读取[mcve]后提供样本数据。 – Jeeped