我需要在作为for循环一部分的单元格中使用公式

时间:2016-08-15 21:16:28

标签: excel vba excel-vba

由于我引用公式的单元格的方式,这变得非常复杂。第一,第二,第三和第四范围参考动态小区位置。这是我遇到问题的代码。

Dim first As Range
Dim second As Range
Dim third As Range
Dim fourth As Range
Dim i As Long
Dim k As Long

For i = 0 To -170
For k = 0 To 170

    Set first = ws.Rows(3 + k).Cells.Find("QQQ").Offset(0 + i, -12)
    Set second = ws.Rows(3 + k).Cells.Find("QQQ").Offset(0 + i, -11)
    Set third = ws.Rows(3 + k).Cells.Find("QQQ").Offset(0 + i, -8)
    Set forth = ws.Rows(3 + k).Cells.Find("QQQ").Offset(0 + i, -7)

ws.Rows(3 + k).Cells.Find("QQQ").Offset(0, -6).Formula = "=IF(OR(AND(first<(third-0.33),first<>0,first<1.33),AND(second<(forth-0.33),second<>0,second<1.33)),TRUE,FALSE)"

Next k
Next i

它没有出现任何错误和运行,但在工作表上找不到公式。它旨在为我的每一行数据运行我的公式。它在我指定的列中放置true或false值。谁能看出我做错了什么?感谢。

这是文件。宏被分配到Capability Report工作表上的按钮。宏的名称是button_click 7.

https://drive.google.com/open?id=0B-Cnpvgoos3cTzQ4dVRpMHJ1Zjg

宏不会运行,因为它引用了我的驱动器,但至少你可以查看报告。

编辑:这是新代码。请参阅下面的评论,看看发生了什么。

Dim first As Range
Dim second As Range
Dim third As Range
Dim fourth As Range
Dim i As Long
Dim k As Long

For i = 0 To 167

    Set first = ws.Rows(169 - i).Cells.Find("QQQ").Offset(0 + i, -12)
    Set second = ws.Rows(169 - i).Cells.Find("QQQ").Offset(0 + i, -11)
    Set third = ws.Rows(169 - i).Cells.Find("QQQ").Offset(0 + i, -8)
    Set forth = ws.Rows(169 - i).Cells.Find("QQQ").Offset(0 + i, -7)

ws.Rows(169 - i).Cells.Find("QQQ").Offset(0, -6).Formula = "=IF(OR(AND(" & first.Address & "<(" & third.Address & "-0.33)," & first.Address & "<>0," & first.Address & "<1.33),AND(" & second.Address & "<(" & forth.Address & "-0.33)," & second.Address & "<>0," & second.Address & "<1.33)),TRUE,FALSE)"

Next i

0 个答案:

没有答案