如何使用 VBA 将 If Then 应用于一系列单元格
How to apply If Then to a range of cells using VBA
我是 VBA 的新手,我正在处理的示例之一(如下)正在使用 If 函数;在这种情况下,根据 A1 中 'score' 的内容,某些文本将显示在 B1 中。
我明白了,但我想知道您如何将其应用于一系列单元格。比如说从 A1 到 A10,根据分数,文本显示在 B1 到 B10 中。
我试过搜索,也试过使用 FOR 循环,但它只显示 A 列中最后一个条目的文本。
任何人都可以帮助解释 'standard' 将这种类型的东西应用于一系列单元格的方法吗?
谢谢
Dim note As Integer, score_comment As String
note = Range("A1")
'Comments based on the score
If note = 6 Then
score_comment = "Excellent score !"
ElseIf note = 5 Then
score_comment = "Good score"
ElseIf note = 4 Then
score_comment = "Satisfactory score"
ElseIf note = 3 Then
score_comment = "Unsatisfactory score"
ElseIf note = 2 Then
score_comment = "Bad score"
ElseIf note = 1 Then
score_comment = "Terrible score"
Else
score_comment = "Zero score"
End If
'Comments in B1
Range("B1") = score_comment
这是一个例子:
Sub LoopExample()
Dim rng As Range, cl As Range, note as integer
Set rng = Range("A1:A10")
For Each cl In rng
note = cl.value
If note = 6 Then
cl.Offset(0, 1) = "Excellent score !"
ElseIf note = 5 Then
cl.Offset(0, 1) = "Good score"
ElseIf note = 4 Then
cl.Offset(0, 1) = "Satisfactory score"
ElseIf note = 3 Then
cl.Offset(0, 1) = "Unsatisfactory score"
ElseIf note = 2 Then
cl.Offset(0, 1) = "Bad score"
ElseIf note = 1 Then
cl.Offset(0, 1) = "Terrible score"
Else
cl.Offset(0, 1) = "Zero score"
End If
Next cl
End Sub
备注:
note
定义在循环
offset(0,1)
获取 B 列中的相邻单元格。现在不需要 score_comment
Dim lastRow As Integer
Dim i As Integer
'lastRow is the amount of rows in columns 1 (dynamic)
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To lastRow
If Cells(i, 1).Value = 1 Then
Cells(i, 1).Offset(0, 1).Value = "Comment ONE"
ElseIf Cells(i, 1).Value = 2 Then
Cells(i, 1).Offset(0, 1).Value = "Comment TWO"
ElseIf Cells(i, 1).Value = 3 Then
Cells(i, 1).Offset(0, 1).Value = "Comment THREE"
ElseIf Cells(i, 1).Value = 4 Then
Cells(i, 1).Offset(0, 1).Value = "Comment FOUR"
ElseIf Cells(i, 1).Value = 5 Then
Cells(i, 1).Offset(0, 1).Value = "Comment FIVE"
ElseIf Cells(i, 1).Value = 6 Then
Cells(i, 1).Offset(0, 1).Value = "Comment SIX"
End If
Next i
我是 VBA 的新手,我正在处理的示例之一(如下)正在使用 If 函数;在这种情况下,根据 A1 中 'score' 的内容,某些文本将显示在 B1 中。
我明白了,但我想知道您如何将其应用于一系列单元格。比如说从 A1 到 A10,根据分数,文本显示在 B1 到 B10 中。
我试过搜索,也试过使用 FOR 循环,但它只显示 A 列中最后一个条目的文本。
任何人都可以帮助解释 'standard' 将这种类型的东西应用于一系列单元格的方法吗?
谢谢
Dim note As Integer, score_comment As String
note = Range("A1")
'Comments based on the score
If note = 6 Then
score_comment = "Excellent score !"
ElseIf note = 5 Then
score_comment = "Good score"
ElseIf note = 4 Then
score_comment = "Satisfactory score"
ElseIf note = 3 Then
score_comment = "Unsatisfactory score"
ElseIf note = 2 Then
score_comment = "Bad score"
ElseIf note = 1 Then
score_comment = "Terrible score"
Else
score_comment = "Zero score"
End If
'Comments in B1
Range("B1") = score_comment
这是一个例子:
Sub LoopExample()
Dim rng As Range, cl As Range, note as integer
Set rng = Range("A1:A10")
For Each cl In rng
note = cl.value
If note = 6 Then
cl.Offset(0, 1) = "Excellent score !"
ElseIf note = 5 Then
cl.Offset(0, 1) = "Good score"
ElseIf note = 4 Then
cl.Offset(0, 1) = "Satisfactory score"
ElseIf note = 3 Then
cl.Offset(0, 1) = "Unsatisfactory score"
ElseIf note = 2 Then
cl.Offset(0, 1) = "Bad score"
ElseIf note = 1 Then
cl.Offset(0, 1) = "Terrible score"
Else
cl.Offset(0, 1) = "Zero score"
End If
Next cl
End Sub
备注:
note
定义在循环offset(0,1)
获取 B 列中的相邻单元格。现在不需要score_comment
Dim lastRow As Integer
Dim i As Integer
'lastRow is the amount of rows in columns 1 (dynamic)
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To lastRow
If Cells(i, 1).Value = 1 Then
Cells(i, 1).Offset(0, 1).Value = "Comment ONE"
ElseIf Cells(i, 1).Value = 2 Then
Cells(i, 1).Offset(0, 1).Value = "Comment TWO"
ElseIf Cells(i, 1).Value = 3 Then
Cells(i, 1).Offset(0, 1).Value = "Comment THREE"
ElseIf Cells(i, 1).Value = 4 Then
Cells(i, 1).Offset(0, 1).Value = "Comment FOUR"
ElseIf Cells(i, 1).Value = 5 Then
Cells(i, 1).Offset(0, 1).Value = "Comment FIVE"
ElseIf Cells(i, 1).Value = 6 Then
Cells(i, 1).Offset(0, 1).Value = "Comment SIX"
End If
Next i