在 vba 中什么都不做
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46795591/
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
Do nothing in vba
提问by Rik
Is there an equivalent to python "pass" in VBA to simply do nothing in the code?
在 VBA 中是否有等效于 python“pass”的代码,可以在代码中什么都不做?
for example:
例如:
For Each ws In ThisWorkbook.Sheets
If ws.Name = "Navy Reqs" Then
ws.Select
nReqs = get_num_rows
Cells(1, 1).Select
If ActiveSheet.AutoFilterMode Then Cells.AutoFilter
Selection.AutoFilter
ElseIf ws.Name = "temp" Then
pass
Else
ws.Select
nShips = get_num_rows
End If
Next
I get an error here that pass is not defined. Thanks.
我在这里收到一个错误,未定义 pass。谢谢。
回答by itChi
just remove passand re run the code. VBA will be happy to accept that I believe
只需删除pass并重新运行代码。VBA 会很乐意接受我相信
回答by Gary's Student
Don't include any statements:
不要包含任何语句:
Sub qwerty()
If 1 = 3 Then
Else
MsgBox "1 does not equal 3"
End If
End Sub
回答by Mathieu Guindon
Write code that does what it says, and says what it does.
编写代码,说到做到,说到做到。
For Each ws In ThisWorkbook.Sheets
If ws.Name = "Navy Reqs" Then
ws.Select
nReqs = get_num_rows
Cells(1, 1).Select
If ActiveSheet.AutoFilterMode Then Cells.AutoFilter
Selection.AutoFilter
Else If ws.Name <> "temp" Then
ws.Select
nShips = get_num_rows
End If
Next
That's all you need. An instruction that means "here's some useless code" does not exist in VBA.
这就是你所需要的。VBA 中不存在表示“这里有一些无用的代码”的指令。
You want comments that say why, not what- a comment that says 'do nothingis the exact opposite of that. Don't write no-op code, it's pure noise.
您需要说明原因的评论,而不是说明内容的评论 - 评论与'do nothing此完全相反。不要编写无操作代码,这是纯粹的噪音。
Assuming Python's passworks like C#'s continuestatement and skips to the next iteration, then the VBA equivalent is the one and only legitimate use of a GoTojump:
假设 Python 的pass工作方式类似于 C# 的continue语句并跳到下一次迭代,那么 VBA 等效项是唯一合法使用的GoTo跳转:
For ...
If ... Then GoTo Skip
...
Skip:
Next
回答by Kostas K.
Just leave it blank. You can also use a Select statement, it's easier to read.
只需将其留空。您还可以使用 Select 语句,它更易于阅读。
For Each ws In ThisWorkbook.Sheets
Select Case ws.Name
Case "Navy Reqs":
'...
Case "temp":
'do nothing
Case Else:
'...
End Select
Next
回答by Kristian Kebby
This code shows an IF test that keeps searching unless it gets a match.
此代码显示了一个 IF 测试,该测试会一直搜索,除非获得匹配项。
Function EXCAT(Desc)
Dim txt() As String
' Split the string at the space characters.
txt() = Split(Desc)
For i = 0 To UBound(txt)
EXCAT = Application.VLookup(txt(i), Worksheets("Sheet1").Range("Dept"), 2, False)
If IsError(EXCAT) Then Else Exit Function
Next
' watch this space for composite word seach
EXCAT = "- - tba - -"
End Function
回答by jerry
I coded in COBOL for many years and the equivalent 'do nothing' statement is NEXT SENTENCE.
我用 COBOL 编码多年,等效的“什么都不做”语句是NEXT SENTENCE.
In VBA, I find myself creating a dummy variable (sometimes a global) dim dummy as integerand then when I need that 'do nothing' action in an If..Then..ElseI put in a line of code: dummy = 0.
在VBA中,我发现自己创建一个虚拟变量(有时是一个全球性的)dim dummy as integer,然后当我需要的是“什么也不做”一个动作If..Then..Else我把一行代码: dummy = 0。
回答by q335r49
This is actually a legit question. I want to run a debug routine that stops when a certain critical value is, say, 8, i.e., set break point at x = 8 and then step through line by line. So the following construct is useful:
这实际上是一个合法的问题。我想运行一个调试例程,当某个临界值是 8 时停止,即在 x = 8 处设置断点,然后逐行逐步执行。因此,以下构造很有用:
Select Case x
Case 21
'do nothing
Case 8
'do nothing
Case 14
'do nothing
Case 9
'do nothing
End Select
- Since you can't put a break point on a comment, an actual statement is needed.
- You also can't put a break point on the Case statements because that gets executed every time.
- 由于您不能在注释上放置断点,因此需要一个实际的语句。
- 您也不能在 Case 语句上设置断点,因为它每次都会执行。
Obviously anything here is OK e.g., x=x, but it would be nice to have something formal like pass.
显然,这里的任何东西都可以,例如,x=x但是如果有像pass.
But I've been using x=x.
不过我一直在用x=x。

