Excel VBA“如果没有块如果结束”-VBA错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11460065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA "End If without block If" - VBA error
提问by Josh McKearin
I can not figure out why VBA is yelling at me on this one. Very simple code, only one if statement is involved, and it clearly has a "Starting" If and a matching End If. I am a VB.NET developer, so it may be a difference in the syntax that I am missing.
我不明白为什么 VBA 在这个问题上对我大喊大叫。很简单的代码,只涉及一个if语句,而且很明显有一个“Starting”If和一个匹配的End If。我是一名 VB.NET 开发人员,所以我缺少的语法可能有所不同。
Sub splitter()
Dim line() As String
Dim rng As Range
Dim row As Range
Dim cell As Range
For Each row In rng.Rows
If row.Value <> "" Then
line = Split(Range(row, 1), ",")
Select Case line(0)
Case "Desktop"
Range(row, 8).Value = "Desktop"
Case "Laptop"
Range(row, 8).Value = "Laptop"
Case "Server"
Range(row, 8).Value = "Server"
Case Else
Range(row, 8).Value = "N/A"
End If
Next
End Sub
Specifically, the end result is that it will populate a "Child" drop down list (Range(row, 8)) based on the selection made from of the "Parent" drop down list (Range(row, 1)). Because the question will arise, the reason I am doing this using VBA is because I can utilize the Split() function with the way the items in the Parent drop down list are made eg. "Desktop, Dell, 745". Also, I am a better programmer than an excel developer.
具体来说,最终结果是它将根据从“父”下拉列表 (Range(row, 1)) 中所做的选择来填充“子”下拉列表 (Range(row, 8))。因为会出现问题,所以我使用 VBA 执行此操作的原因是因为我可以使用 Split() 函数以及 Parent 下拉列表中的项目的制作方式,例如。“台式机,戴尔,745”。此外,我是一个比 excel 开发人员更好的程序员。
回答by Jon Crowell
Your select statement needs an end:
您的 select 语句需要结束:
Select Case line(0)
Case "Desktop"
Range(row, 8).Value = "Desktop"
Case "Laptop"
Range(row, 8).Value = "Laptop"
Case "Server"
Range(row, 8).Value = "Server"
Case Else
Range(row, 8).Value = "N/A"
End Select