You are on page 1of 5

9/24/2020 excel - Do nothing in vba - Stack Overflow

Do nothing in vba
Asked 2 years, 11 months ago Active 9 days ago Viewed 60k times

Is there an equivalent to python "pass" in VBA to simply do nothing in the code?

5 for example:

For Each ws In ThisWorkbook.Sheets


If ws.Name = "Navy Reqs" Then
1 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.

vba excel

edited Jul 9 '18 at 19:34 asked Oct 17 '17 at 17:11


Community ♦ Rik
1 1 1,451 2 16 28

Wow that Python pass is the single most ridiculously useless thing I've ever seen. – Mathieu Guindon Oct
17 '17 at 17:27

Or is it not more like some kind of continue statement (C#), i.e. it skips to the next iteration? If so, then
simply reword your conditions so that you don't need any no-op code. – Mathieu Guindon Oct 17 '17 at
17:33

1 It's useful in python if you have a function or loop that is not yet implemented but you will implement in the
future. Instructors use it in their code where they want their students to fill in code often – Rik Oct 17 '17
at 17:43

1 Oh wow. So yeah, the VBA equivalent to that is no code - actually, a 'not implemented yet comment
would be even better. – Mathieu Guindon Oct 17 '17 at 17:44

2 Python is compiled based on indents so the lack of indent throws an error – Rik Oct 17 '17 at 17:48

8 Answers Active Oldest Votes

https://stackoverflow.com/questions/46795591/do-nothing-in-vba 1/5
9/24/2020 excel - Do nothing in vba - Stack Overflow

just remove pass and re run the code. VBA will be happy to accept that I believe
10
answered Oct 17 '17 at 17:18
itChi
545 3 13

1 you can also add a comment 'like this if you want to indicate that you meant to leave it blank (useful
for future debugging) – SeanC Oct 17 '17 at 21:23

1 For this use case tbh, I would have omitted the sheet in the for statement, with <>. It really does depend if
this was a place holder, or a sheet he wanted specifically not to include in the statement. – itChi Oct 17 '17
at 21:35

Don't include any statements:

6 Sub qwerty()
If 1 = 3 Then
Else
MsgBox "1 does not equal 3"
End If
End Sub

answered Oct 17 '17 at 17:19


Gary's Student
87.7k 8 47 72

Write code that does what it says, and says what it does.

5 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.

https://stackoverflow.com/questions/46795591/do-nothing-in-vba 2/5
9/24/2020 excel - Do nothing in vba - Stack Overflow

You want comments that say why, not what - a comment that says 'do nothing is the exact
opposite of that. Don't write no-op code, it's pure noise.

Assuming Python's pass works like C#'s continue statement and skips to the next iteration, then
the VBA equivalent is the one and only legitimate use of a GoTo jump:

For ...
If ... Then GoTo Skip
...
Skip:
Next

edited Oct 17 '17 at 17:37 answered Oct 17 '17 at 17:30


Mathieu Guindon
62.2k 7 83 191

Just leave it blank. You can also use a Select statement, it's easier to read.

5 For Each ws In ThisWorkbook.Sheets


Select Case ws.Name
Case "Navy Reqs":
'...

Case "temp":
'do nothing

Case Else:
'...
End Select
Next

answered Oct 17 '17 at 17:18


Kostas K.
5,775 2 15 24

This code shows an IF test that keeps searching unless it gets a match.

2 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
https://stackoverflow.com/questions/46795591/do-nothing-in-vba 3/5
9/24/2020 excel - Do nothing in vba - Stack Overflow

' watch this space for composite word seach


EXCAT = "- - tba - -"

End Function

edited Oct 14 '18 at 0:31 answered Oct 14 '18 at 0:29


Hossein Golshani Kristian Kebby
1,745 5 13 25 21 1

I coded in COBOL for many years and the equivalent 'do nothing' statement is NEXT SENTENCE .

2 In VBA, I find myself creating a dummy variable (sometimes a global) dim dummy as integer and
then when I need that 'do nothing' action in an If..Then..Else I put in a line of code: dummy = 0 .

edited Jun 19 '19 at 14:08 answered Jun 19 '19 at 12:44


Christophe Le Besnerais jerry
1,524 2 19 37 21 1

@dippas Actually it is an answer, he suggests a different way to address the problem. Only the last
sentence was misleading ... thus I took the freedom to remove it, to make the answer more clear. –
GhostCat Jun 19 '19 at 13:00

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
1 construct is useful:

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.

Obviously anything here is OK e.g., x=x , but it would be nice to have something formal like pass .

But I've been using x=x .

https://stackoverflow.com/questions/46795591/do-nothing-in-vba 4/5
9/24/2020 excel - Do nothing in vba - Stack Overflow

answered Apr 2 at 17:34


q335r49
482 4 10

Most languages have a "null" or "empty" statement like Python's pass . These statements have
evolved because they are practically useful, and in some grammars, necessary. For example, as
0 others have suggested, null statements can serve as side-effect free anchors for a debugger.

I use:

Debug.Assert True

Note that if your use case is to set a conditional breakpoint, you may find Stop more useful, as in:

If targetShape.Type = msoAutoShape Then


Stop
End

answered Sep 14 at 7:04


Leo
1,828 17 24

https://stackoverflow.com/questions/46795591/do-nothing-in-vba 5/5

You might also like