score:13
i think the confusion comes from 3
. we'd think that 3
and 4
should behave the same. in fact, 3
is equivalent to this:
if 3 = 3 then: (do nothing) 'an empty statement
debug.print 3 ' <-- this will be executed regardless of the previous if condition
to see it, change 3
into this:
if 3 = 0 then:
debug.print 3 '<-- 3 will be printed! ;)
in conclusion, yes, the :
is indeed to merge many statements on a single line
good job @vityata !!! :)
score:3
if then blocks require a matching end if when they are multi-line.
the first case is fine because the command after then is on the same line.
the second case is fine for the same reason, the : makes no difference in this situation.
the third case works because when the if statement evaluates to true, there is no command following the ':'. the : signals to compiler that the next command should be seen as being on the same line. there is nothing following, so processing moves to the next line which is seen as outside the if then block.
the fourth case has no notation to tell the compiler that the if then command is single line, and therefore it is looking for the end if to the block.
option explicit
public sub testme()
if 1 = 1 then: debug.print 1 '<-- all on a single line - ok
if 2 = 2 then debug.print 2 '<-- all on a single line - ok
if 3 = 3 then: '<-- no command following :
debug.print 3 '<-- seen as outside the if then block
' gives error:
' if 4 = 4 then
' debug.print 4
' end if '<-- required to show the end of the if then block
end sub
Source: stackoverflow.com
Related Query
- VBA - How the colon `:` works in VBA code with condition
- How to simplify; dozens of Excel Tabs with the same underlying VBA Code
- How do I let my VBA web scraping code keeps up with the new window?
- How do I select the entire worksheet with vba code
- How can I click the submit button in this HTML code (has no name or ID) with MS Access VBA
- VBA how do i have the same code on a command button but with changed variables
- How do I add buttons to an MS Access form at run time with VBA and add code to the _Click() event
- How to code vba with reference to selenium for clicking the close button of the pop up in edge browser that is modal based
- My access VBA code for creating excel files with data connections works on the first file then always fails at random points
- How do I save VB6 code, ie. the extension, which will open as macro in Excel? Not code created with VBA in Excel
- How to make a VBA input box that only disappears if the user enters a 7 character code with 3 numbers and an "M" and an "!"
- How to click on the second instance of drop down box with similar html code - Selenium VBA Excel
- How to use the Resize function with the offset function in VBA code for a User Defined Function in Excel
- When two Excel workbooks are open, each with VBA code, how do I switch the code view between workbooks?
- How can I have the sum of rows with the same code (condition) using excel vba
- Vba code for how to group together numbers which are the 31st character in a string with a row break between consecutive numbers
- how to take a different random numbers in one row in the range 1..n with vba code
- How to change VBA code to work with cells under the first one?
- How to comment and uncomment blocks of code in the Office VBA Editor
- How to continue the code on the next line in VBA
- How should I make my VBA code compatible with 64-bit Windows?
- What is the VBA code to emulate selecting a block with the CTRL+A shortcut?
- How do I make vba code compatible with libre office
- How to check from .net code whether "Trust access to the VBA project object model" is enabled or not for an Excel application?
- How to find the exact text string in a column of sheet using excel vba code
- How to Reuse Code with VBA
- How do i determine if an error is in any cell in the entire workbook with Excel VBA
- VBA: How to get the last used cell by VBA code when the last error occured in a Workbook/Worksheet?
- how to write value into cell with vba code without auto type conversion?
- How to write VBA code to hide all the column and row headings in Excel?
More Query from same tag
- Looking Through Multiple .csv Files with VBA
- Make the Text box disappear for the current selected record
- Custom Function Disable in Excel
- Is there another way to store Folder Path in EXCEL VBA than using Sheets?
- How to fix the Excel goes to Not-responding state when the Macro is running?
- Autorun Excel vba code when cell value changes
- Excel VBA Graph Stretch Macro
- Creating a VBA script to automate data upload and transformation
- Unfiltering all row data in Pivot Table then filter for specific values
- Close a modal opened by vba macro with c#
- run time error 1004 in for-loop
- Trying to only type check boxes in specific cells
- Why does OnTime not execute when a userform is open?
- Argument Not Optional Error for AutoFilter code
- Call Sub within a Sub in Outlook - Autofilter Method of Range Class failed
- Copy/Paste multiple rows in VBA
- Paste text into Excel comment VBA
- To plot chart based on dynamic user ranges
- Listbox not showing the values that were populated in it using Listbox.List method
- Cannot copy sheet to another workbook
- How can I optimize my flow control when looping through an array to find a matching value in another array?
- Delete 2 lines from header in txt files in a folder using vba macro
- Split rows that have multiline text and single line text
- Prime number in EXCEL VBA
- excel vba blank subtotal cells don't accept a formula
- Close userform with escape button
- Outlook custom menu buttons
- Extract Outlook Email Information to Excel from Specific Dates vba
- VBA Pivot Table dynamic range
- Store the row number in a VBS script as variable and refer to it