简体   繁体   English

Excel vba如何在行上重复if then else条件

[英]Excel vba how to repeat an if then else condition on rows

I want to repeat my 'if then else' condition on the 30 rows that follow.我想在接下来的 30 行中重复我的“if then else”条件。 This is my code (I am new to this).这是我的代码(我是新手)。

Dim score As Range, result As Double
If Range("A2") = "2a" Then
Range("C2") = 20

Else
If Range("A2") = "3c" Then
Range("C2") = 23

So at the moment when I enter 2a / 3c in cell A2, the number 20 / 23 comes up in cell C2.因此,当我在单元格 A2 中输入 2a / 3c 时,数字 20 / 23 出现在单元格 C2 中。 I would like the same thing to happen in row 3, 4, 5 ... 30. When I enter 2a / 3c in cell A5, the number 20 / 23 comes up in cell C5.我希望在第 3、4、5...30 行发生同样的事情。当我在单元格 A5 中输入 2a / 3c 时,数字 20 / 23 出现在单元格 C5 中。 Is it possible with a loop or another type of code?是否可以使用循环或其他类型的代码? Or will I have to copy this over and over for each row?还是我必须为每一行一遍又一遍地复制它?

Any help is really appreciated.任何帮助都非常感谢。 Thanks!谢谢!

Here's a basic for-next loop that would accomplish what you ask...这是一个基本的for-next循环,可以完成您的要求......

For i = 2 To 30
    If Range("A" & i) = "2a" Then
        Range("C" & i) = 20
    End If
    If Range("A" & i) = "3c" Then
        Range("C" & i) = 23
    End If
Next

Here's a tutorial I picked at random from a Google search.这是我从 Google 搜索中随机挑选的教程

Try this as well也试试这个

  Dim score As Range, c As Range, x
Set score = Range("A2:A32").SpecialCells(xlCellTypeConstants, 23)
For Each c In score.Cells
    x = IIf(c = "2a", 20, IIf(c = "3c", 23, ""))
    c.Offset(0, 2) = x
Next c
Private Sub Worksheet_Change(ByVal Target As Range)
 if not intersect(target,range("A2:A32")) is Nothing then target.offset(,2)=iif(instr("2a3a",target)>0,20+3*abs(target="3c"),"")
End sub

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

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