简体   繁体   English

VBA .formula = If()以Cell(x,y)作为动态参考

[英]VBA .formula= If() with Cell(x, y) as dynamic reference

I'm trying hard to fix this since some time but I can't on my own apparently. 自一段时间以来,我一直在努力解决此问题,但显然我不能独自解决。

ThisWorkbook.Sheets("GesamteListe").Cells(X, 18).Formula = _
                "=If(" & Cells(X, 16) & " " = " " & Cells(X, 17) & ",""fine"",""not fine"")"

I want to compare two cells, which are always in column 16 and 17 but both are dynamic by X which is determined by actions before. 我想比较两个单元格,它们始终位于第16列和第17列,但是它们都是由X动态的,而X由之前的操作确定。 I can't make him accepting my Cells(X, 16) and Cells(X, 17) which in the end should look like this (exemplary): 我不能让他接受我的Cells(X,16)和Cells(X,17),它们最终看起来应该像这样(示例性):

if(P3=Q3;"fine";"not fine") if(P3 = Q3;“罚款”;“不罚款”)

Pretty easy I guess but I tried various versions and none works. 我猜很容易,但是我尝试了各种版本,但没有用。

Thanks in advance! 提前致谢!

You need to use .Address 您需要使用.Address

 "=If(" & Cells(X, 16).Address & "  =  " & Cells(X, 17).Address & ",""fine"",""not fine"")"

You also had too many " around the = 您在=周围也有太多"

Maybe this is also a viable alternative to the solution provided by @ScottCraner: 也许这也是@ScottCraner提供的解决方案的可行替代方案:

ThisWorkbook.Sheets("GesamteListe").Cells(x, 18).FormulaR1C1 = _
                "=If(RC[-2]=RC[-1],""fine"",""not fine"")"

Note, in case that X is part of a loop of the following type: 请注意,如果X是以下类型的循环的一部分:

Dim x As Long

For x = 2 To 20
    ThisWorkbook.Worksheets("GesamteListe").Cells(x, 18).FormulaR1C1 = _
        "=If(RC[-2]=RC[-1],""fine"",""not fine"")"
Next x

the above formula can short-cut this loop with the following formula: 上面的公式可以使用以下公式简化此循环:

ThisWorkbook.Worksheets("GesamteListe").Range("R2:R20").FormulaR1C1 = _
    "=If(RC[-2]=RC[-1],""fine"",""not fine"")"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM