gpt4 book ai didi

If Else 条件下的 Vba 问题

转载 作者:行者123 更新时间:2023-12-04 21:15:01 25 4
gpt4 key购买 nike

Set ws1 = wb.Worksheets("MacroGeneratedReport")
LastRow = ws1.Cells(Rows.Count, "A").End(xlUp).Row
For i = LastRow To 1 Step -1
If ws1.Cells(i, "I").Value = "A" Or ws1.Cells(i, "I").Value = "B" Or
ws1.Cells(i, "I").Value = "C" Or ws1.Cells(i, "I").Value = "D" Or
ws1.Cells(i, "I").Value = "E" Or ws1.Cells(i, "I").Value = "F" Or
ws1.Cells(i, "I").Value = "Y" Then
ws1.Cells(i, "AO").Value = ws1.Cells(i, "Y").Value
Else
**ws1.Cells(i, "AO").Value = ws1.Cells(i, "AP").Value * ws1.Cells(i, "W").Value**
End If
Next i

在上面的代码中,粗体突出显示的“Else condition”给出了运行时错误,而代码根据条件给出了正确的输出和正确的值。请提出必要的更改。

最佳答案

您的代码“尖叫”使用Select Case而不是你的多个 Or s。

代码

Set ws1 = wb.Worksheets("MacroGeneratedReport")
With ws1
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = LastRow To 1 Step -1
Select Case .Cells(i, "I").Value
Case "A", "B", "C", "D", "E", "F", "Y"
.Cells(i, "AO").Value = .Cells(i, "Y").Value

Case Else
' check that both values are numeric
If IsNumeric(.Cells(i, "AP").Value) And IsNumeric(.Cells(i, "W").Value) Then
.Cells(i, "AO").Value = .Cells(i, "AP").Value * .Cells(i, "W").Value
End If
End Select
Next i
End With

关于If Else 条件下的 Vba 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47881881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com