【问题标题】:I need to use a Formula in a cell that is part of a for loop我需要在作为 for 循环一部分的单元格中使用公式
【发布时间】:2016-08-15 21:16:27
【问题描述】:

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

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

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

编辑:这是新代码。请参阅下面的 cmets 以了解发生了什么。

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

【问题讨论】:

  • 您的IF 公式中缺少右括号。如果你把它加到最后,它会起作用吗?
  • @AlexisOlson 没有运气同样的问题
  • 如果在单元格中手动输入,您是否检查过公式是否有效?
  • @AlexisOlson 在此之前我很长一段时间都在使用这个公式。我使用单元格引用示例而不是单词:"BQ3"。我只是将单元格更改为过去的正确位置,但我正在尝试更改它,以便通过按钮自动执行。公式是正确的。引用是否正确是另一回事。
  • 尝试使用"=IF(OR(AND(" &amp; first.Address &amp; "&lt;(" &amp; third.Address &amp; "-0.33)," &amp; first.Address &amp; "&lt;&gt;0," &amp; first.Address &amp; "&lt;1.33),AND(" &amp; second.Address &amp; "&lt;(" &amp; fourth.Address &amp; "-0.33)," &amp; second.Address &amp; "&lt;&gt;0," &amp; second.Address &amp; "&lt;1.33)),TRUE,FALSE)" - 您在 VBA 代码中创建的范围将无法用于 Excel,因此您需要将它们作为地址传递。 (我还更正了fourth的拼写)

标签: vba excel


【解决方案1】:

好的,我想通了。在我上次更新后,需要first 代码和.Address 代码来使单词首先成为位置而不是文本。我遇到的另一个问题是我的公式涉及i 是错误的。这是一个更新的代码。

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, -12)
    Set second = ws.Rows(169 - i).Cells.Find("QQQ").Offset(0, -11)
    Set third = ws.Rows(169 - i).Cells.Find("QQQ").Offset(0, -8)
    Set forth = ws.Rows(169 - i).Cells.Find("QQQ").Offset(0, -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

【讨论】:

    猜你喜欢
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多