简体   繁体   中英

If column A equals a certain value, generate a value in column B. Check each cell in column A --> Generate a value in each cell in column B

I have a very simple question that I can't find the answer to.

Here is an example of what I'd like to do:

  • Sample Data

在此处输入图片说明

  • If a row in column A = 'Sandwiches', I would like column B to display 'It's a Sandwich'
  • If a row in column A = 'Wraps', I would like column B to display 'It's a Wrap'
  • etc. etc.

So far, I am able to do this for the first row. I'm having trouble creating a for loop to loop through all the available cells.

Here is what I have so far (was thinking of adding Else If statements for different values later):

Current If Statement

在此处输入图片说明

Akash you do not need to write vba codes for this. You may use a lookup table and apply Vlookup formula as @BigBen said.

Apart from this, this formula can make you happy if you ignore some spelling errors. Enter this in cell B2 and paste along the way down.

="It's " & A2

If you prefer the use of VBA code instead a formule as Ozgun Senyuva suggested. You can try this code:

Dim myLine As Double
Dim myFood As String
myLine=2
myFood=" an other thing"
With SavedData
  Do While .Cells(myLine, 1)<>"" 'Assume that Category is in column A, and Generated Asset Name is in column B
    If .Cells(myLine, 1)="Sandwiches" Then
       myFood=" a Sandwich"
    Else If  .Cells(myLine, 1)=" Wraps" Then
       myFood=" a Wrap"
    End If
    .Cells(myLine, 2)="It's" & myFood
    myLine=myLine+1
  Loop
End With

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