简体   繁体   English

如果 A 列等于某个值,则在 B 列中生成一个值。检查 A 列中的每个单元格 --> 在 B 列中的每个单元格中生成一个值

[英]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'如果 A 列中的一行 = 'Sandwiches',我希望 B 列显示'It's a Sandwich'
  • If a row in column A = 'Wraps', I would like column B to display 'It's a Wrap'如果 A 列中的一行 = 'Wraps',我希望 B 列显示'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.我在创建 for 循环以遍历所有可用单元格时遇到问题。

Here is what I have so far (was thinking of adding Else If statements for different values later):这是我到目前为止所拥有的(正在考虑稍后为不同的值添加 Else If 语句):

Current If Statement当前 If 语句

在此处输入图片说明

Akash you do not need to write vba codes for this. Akash 您不需要为此编写 vba 代码。 You may use a lookup table and apply Vlookup formula as @BigBen said.您可以使用查找表并应用@BigBen 所说的 Vlookup 公式。

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.在单元格 B2 中输入此内容并向下粘贴。

="It's " & A2

If you prefer the use of VBA code instead a formule as Ozgun Senyuva suggested.如果您更喜欢使用 VBA 代码而不是 Ozgun Senyuva 建议的公式。 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

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

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