简体   繁体   English

在每个参数VBA excel中设置值

[英]Set value in each parameter VBA excel

Example

Column : 栏位:

A       B       C
A001    OK
A002    OK
A003    OK
A004    OK
A004    OK
A004    OK
A004    OK
A004    OK
A004    OK
A004    OK
A004    OK
A004    OK
A004    OK
A004    OK
A004    OK
A004    OK
A004    OK
A004    OK
A004    OK
A004    NO
A004    OK
A004    OK
A004    OK
A004    OK
A006    OK
A007    OK
A008    OK
A010    OK
A010    OK
A015    OK
A015    NO

I want to check the value on Column A and B. If Column B has 'OK' value, column C value in that parameter is 'OK' But If has only 'NO' in each on All of copy parameters values are 'NO' in column C. I am using the below code. 我想检查A列和B列的值。如果B列的值为“ OK”,则该参数中的C列的值为“ OK”,但是如果每个副本参数中只有“ NO”,则所有复制参数的值均为“ NO”在C列中。我正在使用以下代码。

for i= 1 to 100
    If Cells(i,1).value="A001" and cells(i,2).value="OK" then
        Cells(i,3).value "OK"
    end if
next i

But It's not have the value in column C 但这在C列中没有值

How should I do this?? 我应该怎么做?

I would recommend using formulas for this. 我建议为此使用公式。 So you can use 所以你可以使用

=IF(AND(A1="A001",B1="OK"),"OK","")

And now to answer your question... 现在回答您的问题...

  1. That may be happening because you are missing an "=" Sign in Cells(i,3).Value "OK" 这可能是因为您缺少“ =“登录Cells(i,3).Value "OK"

  2. That may be happening because the sheet that you want to write to may not be active? 这可能是因为您要写入的工作表可能没有激活? And hence you should always qualify your objects. 因此,您应该始终限定对象。 Something like ThisWorkbook.Sheets("Sheet1").Cells(i,1) So your code becomes 类似于ThisWorkbook.Sheets("Sheet1").Cells(i,1)这样的代码

    Code

     For i = 1 To 100 With ThisWorkbook.Sheets("Sheet1") If .Cells(i, 1).Value = "A001" And .Cells(i, 2).Value = "OK" Then .Cells(i, 3).Value = "OK" End If End With Next i 
  3. If it still doesn't work then maybe there is space in text of Col A and Col B. You may want to then amend the above code to this 如果仍然不起作用,则Col A和Col B的文本中可能有空格。您可能需要将上面的代码修改为此

    Code

     For i = 1 To 100 With ThisWorkbook.Sheets("Sheet1") If UCase(Trim(.Cells(i, 1).Value)) = "A001" And UCase(Trim(.Cells(i, 2).Value)) = "OK" Then .Cells(i, 3).Value = "OK" End If End With Next i 

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

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