我有A列和B列,他们比较他们以前的值和Iam将它与以下公式进行比较
=IF((OR(AND(A2=A1,B2=B1),K2=0),0,1) it puts the 0 or 1 on the coresponding Q column
所以当它进入第5个单元格然后它就成了
=IF((OR(AND(A5=A4,B5=B4),K5=0),0,1)
但是我试图在我的VBA代码中应用它,比如这些
For numm = 2 To lastRow
Sheet1.Cells(numm, "Q").Value = ="IF(OR(AND(sheet1.cells(numm,""A"").value=sheet1.cells(numm-1,""A"")simlar way becolumn),sheet1.cells(numm,""k"").value=0),1,0)"
Next numm
但我无法执行它所说的1004错误和对象错误的操作 我如何在我的VBA公式中使用单元格(numm,“A”)或至少以任何其他方式放置我的公式并使其工作
答案 0 :(得分:2)
公式中对循环numm的引用需要超出字符串。
也许你可以在VBA中设置单元格公式本身......
For numm = 2 To lastRow
Sheet1.Cells("Q" & numm).Formula = _
"=IF((OR(AND(A" & numm & "=A" & numm - 1 & ",B" & numm & _
"=B" & numm - 1 & "),K" & numm & "=0),0,1)"
Next numm
就个人而言,我会在VBA(ifs,ors,ands)中完成整个声明,然后将值放回Excel。使用Excel公式会使代码更难阅读。
答案 1 :(得分:-1)
您正在寻找类似的东西:
Public Sub test()
Dim Sheet1 As Excel.Worksheet
Set Sheet1 = ActiveSheet
Dim numm As Integer, lastrow As Integer
lastrow = 5
For numm = 2 To lastrow
Sheet1.Cells(numm, "Q").Value = "=IF(OR(AND(" & Sheet1.Cells(numm, "A").Address & "=" & Sheet1.Cells(numm - 1, "A").Address & "," & Sheet1.Cells(numm, "B").Address & "=" & Sheet1.Cells(numm - 1, "B").Address & ")," & Sheet1.Cells(numm, "k").Address & "=0),1,0)"
Next numm
End Sub
Sheet1不能在单元格的值内引用,它只存在于您正在创建的vba函数中,因此您可以将这些字符串附加在一起,只需将单元格地址输出以匹配你之前创造的功能。