【发布时间】:2020-01-25 15:58:07
【问题描述】:
我想根据结果在 E3 中写入文本 a、b 或 c。
代码使 Excel 崩溃。
Sub calculate()
Range("B2").Activate
Do While ActiveCell.Value <> ""
If ActiveCell.Value + ActiveCell.Offset(0, 1) + ActiveCell.Offset(0, 2) = 3 Then
ActiveCell.Offset(0, 3).Value = "Text A"
ElseIf ActiveCell.Value + ActiveCell.Offset(0, 1) + ActiveCell.Offset(0, 2) = 2 Then
ActiveCell.Offset(0, 3).Value = "Text B"
ElseIf ActiveCell.Value + ActiveCell.Offset(0, 1) + ActiveCell.Offset(0, 2) < 2 Then
ActiveCell.Offset(0, 3).Value = "Text C"
End If
Loop
End Sub
【问题讨论】:
-
您打算向下移动列还是只是一遍又一遍地检查B3??
-
我已经编辑了代码。我想根据结果在 E3 中写文本 a、b 或 c
-
在我看来你的 Do While .. Loop 应该只是一个 If .. Endif 块。除非在循环内更改 ActiveCell 值,否则它永远不会退出。
-
1. 找到最后一行,如图所示HERE 2. 使用
FOR循环遍历该列中的单元格。如果您使用Do While ActiveCell.Value <> "",那么如果中间有一个空白单元格,代码将在中途停止。 3. 使用Select Case而不是If-EndIf。它更容易阅读和维护。 注意这些只是我的建议,并不是必须遵循的硬性规定。
标签: excel vba loops if-statement