我有多个if语句,它们逐列检查单元格是否存在值“是”或“中等”。如果选择“是”,则将结果添加+1;如果选择“中等”,则应添加+0.5。
所以我的问题如下:当我的代码需要检查下一列时,它需要执行相同的操作,但是将+1或+0.5添加到同一单元格中同一行的上一个结果中。
这需要在每一行上进行。
最终,当它检查整条线时,结果需要为一个百分比:将是并计算为中等,然后将其除以9,再乘以100。
我在下面看到了一个图片:
我得到以下代码:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Range("D2:L4"), Range(Target.Address)) Is Nothing Then
'If you add (an)other row(s) edit this code above
Call DeleteP2P4
'If you add (an)other row(s) edit this code above
Call SampleMacro
End If
End Sub
Sub DeleteP2P4()
Range("P2:P4").Select
'If you add (an)other row(s) edit this code above
Selection.ClearContents
End Sub
Sub SampleMacro()
' Get the last row
Dim startRow As Long, lastRow As Long
startRow = 2
lastRow = Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row
For i = startRow To lastRow
' If there's Yes/Mediocre in D column, then append next number
If Sheet1.Range("D" & i).Value = "Yes" Then
Sheet1.Range("P" & i).Value = "+1"
ElseIf Sheet1.Range("D" & i).Value = "Mediocre" Then
Sheet1.Range("P" & i).Value = "+0.5"
End If
' If there's Yes/Mediocre in E column, then append next number
If Sheet1.Range("E" & i).Value = "Yes" Then
Sheet1.Range("P" & i).Value = "+1"
ElseIf Sheet1.Range("E" & i).Value = "Mediocre" Then
Sheet1.Range("P" & i).Value = "+0.5"
End If
'It continious here with the rest of the If statements
Next
End Sub
答案 0 :(得分:0)
如棕色怪物所指出的,更改您的
Sheet1.Range("P" & i).Value = "+1"
到
Sheet1.Range("P" & i).Value = val(Sheet1.Range("P" & i).Value) + 1
以及平庸
Sheet1.Range("P" & i).Value = val(Sheet1.Range("P" & i).Value) + 0.5