简体   繁体   English

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

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

in worksheet 'hitterscalc' cell d2, i want to paste the value of worksheet 'batters' cell ks2 if the value in 'hitterscalc' cell ab2 = 1 在工作表'hitterscalc'单元格d2中,如果'hitterscalc'单元格ab2 = 1,我想粘贴工作表'batters'单元格ks2的值

i think want to do this for all of the rows that have data in the worksheet 'hitterscalc' 我认为要对工作表'hitterscalc'中具有数据的所有行执行此操作

i have come up with 我想出了

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

but is returning application defined or object defined error. 但返回的是应用程序定义的错误或对象定义的错误。

can someone point me in the correct direction to fixing this issue? 有人可以指出我正确的方向来解决此问题吗?

edit: 编辑:

so when i do 所以当我做

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

the expression works properly. 该表达式可以正常工作。 how can i get it so that it checks the cell value of worksheet hitterscalc column ab first? 我如何获取它,以便它首先检查工作表hitterscalc列ab的单元格值?

edit 2 编辑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

works. 作品。 i am confused as to why this one works but not the first one. 我对为什么这个有效但为什么第一个无效感到困惑。

I think the confusion is because of quotes - try to use this code: 我认为造成混淆的原因是引号-尝试使用以下代码:

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

Formula string is generated using Chr(34) for double quotes used in IF . 使用Chr(34)IF使用的双引号生成公式字符串。

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

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