[英]Why is VBA reading my ElseIf statement but not executing the code under it?
我有一些嵌套的 If 和 ElseIf 语句。
代码通过 st 值运行 if 和 elseif 语句。
代码应该做的是循环使用第一个 genr 值的 elseif 语句,匹配 st,存储上限值(在本例中为 62),计算该上限值的最大值,然后循环到下一个 genr 值。
我已经逐步调试它,它实际上是 go elseif 行,用于 genr = Q3。 但是,它会跳过新的上限值并使用旧的上限值完成代码的 rest。 帽是 integer
有什么我想念的吗?
st = "H" 'take st=H as an example, genr for it is Q1 and Q3
Gen = o.Cells(cell.Row, "G")
G = Split(Gen, ",")
For k = LBound(G) To UBound(G)
If Left(Trim(G(k)), 1) = "Q" Then
Genr = CStr(G(k))
If St = "H" Then
If Genr = "Q1" Or Genr = "Q2" Then
cap = 62
ElseIf Genr = "Q3" Then
cap = 63
End If
ElseIf St = "Wh" Then
If Genr = "Q1" Or Genr = "Q2" Or Genr = "Q3" Then
cap = 65
End If
' and so on for the rest of the st = "" loops
.
.
.
End If
max = c.Cells(cap, "G").Value + max
End If 'endif of trim
Next k
尝试这个
'* st = "H" instead of st = H
st = "H" 'take st=H as an example, genr for it is Q1 and Q3
Gen = o.Cells(cell.Row, "G")
G = Split(Gen, ",")
For k = LBound(G) To UBound(G)
'* Changed = "G" to = "Q"
If Left(Trim(G(k)), 1) = "Q" Then
'* Added the Trim here
Genr = Trim(CStr(G(k)))
If St = "H" Then
If Genr = "Q1" Or Genr = "Q2" Then
cap = 62
ElseIf Genr = "Q3" Then
cap = 63
End If
ElseIf St = "Wh" Then
If Genr = "Q1" Or Genr = "Q2" Or Genr = "Q3" Then
cap = 65
End If
' and so on for the rest of the st = "" loops
.
.
.
End If
max = c.Cells(cap, "G").Value + max
End If 'endif of trim
Next k
请阅读以'*
开头的评论
您使用Trim()
检查第一个字母是否为“G”,但在将值分配给Genr
时不使用Trim()
,因此请尝试修复它。 如果有空格,您的If
将失败。
G = Split(Gen, ",")
For k = LBound(G) To UBound(G)
Genr = Trim( CStr(G(k)) )
If Left(Genr, 1) = "G" Then
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.