简体   繁体   中英

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.

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

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"

  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

    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

    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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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