繁体   English   中英

VBA Excel 2007中转移条件的单元格的值

[英]VBA excel 2007 Transfer a Cell's value with a condition

在工作表'hitterscalc'单元格d2中,如果'hitterscalc'单元格ab2 = 1,我想粘贴工作表'batters'单元格ks2的值

我认为要对工作表'hitterscalc'中具有数据的所有行执行此操作

我想出了

With Worksheets("Hitterscalc")
    With .Range("d2:d" & .Range("a" & Rows.Count).End(xlUp).Row)
        .Formula = "=IF($AB2=1,Batters!KS2,""))"
        .Value = .Value
    End With
End With

但返回的是应用程序定义的错误或对象定义的错误。

有人可以指出我正确的方向来解决此问题吗?

编辑:

所以当我做

With Worksheets("Hitterscalc")
        With .Range("d2:d" & .Range("ab" & Rows.Count).End(xlUp).Row)
            .Formula = "=Batters!KS2"
            .Value = .Value
        End With
    End With

该表达式可以正常工作。 我如何获取它,以便它首先检查工作表hitterscalc列ab的单元格值?

编辑2

With Worksheets("Hitterscalc")
        With .Range("d2:d" & .Range("ab" & Rows.Count).End(xlUp).Row)
            .Formula = "=IF(AND(A2<>"""",AB2=1),Batters!KS2,"""")"
            '.Formula = "=Batters!KS2"
            .Value = .Value
        End With
    End With

作品。 我对为什么这个有效但为什么第一个无效感到困惑。

我认为造成混淆的原因是引号-尝试使用以下代码:

With Worksheets("Sheet1")
    With .Range("d2:d" & .Range("ab" & Rows.Count).End(xlUp).Row)
            r = "=IF($AB2=1,Batters!KS2," & Chr(34) & Chr(34) & ")"
            .Formula = r
            .Value = .Value
    End With
End With

使用Chr(34)IF使用的双引号生成公式字符串。

暂无
暂无

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

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