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

Time:11-09

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

enter image description here

  • 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

enter image description here

CodePudding user response:

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

CodePudding user response:

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

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