我想编写代码来查看AD
列中的数字,并根据列AG
中的数值返回一个值。 Access Database语句和我写的返回错误的代码如下。非常感谢任何帮助。
IIf([Days between File Date and CA Due Date]<30,"5",IIf(Eval([Days between File Date and CA Due Date] Between 31 And 60),"4",IIf(Eval([Days between File Date and CA Due Date] Between 61 And 90),"3",IIf(Eval([Days between File Date and CA Due Date] Between 91 And 150),"2",IIf([Days between File Date and CA Due Date]>151,"1","0")))))
Sub DayScore()
Dim LastRow As Long
Dim i As Long
LastRow = Range("AD" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Range("AD" & i).Value >= "30" Then
Range("AG" & i).Value = "5"
End If
Next i
End Sub
答案 0 :(得分:1)
尝试,
Sub DayScore()
Dim i As Long
with worksheets("sheet1")
For i = 2 To .cells(.rows.count, "AD").End(xlUp).Row
If isnumeric(.cells(i, "AD").Value2) And Not IsEmpty(.Cells(i, "AD")) Then
.cells(i, "AG") = 5 + int(.cells(i, "AD").Value2 >30) _
+ int(.cells(i, "AD").Value2 >60) _
+ int(.cells(i, "AD").Value2 >90) _
+ int(.cells(i, "AD").Value2 >150)
else
.cells(i, "AG") = 0
End If
Next i
end with
End Sub
以数学方式使用时,VBA True为-1
。
将数字保持为真实数字(例如5
)而非文字看起来像数字(例如"5"
)被视为“最佳做法”。