Skip to main content
6 of 6
Added LongLong to the literal list
Taylor Raine
  • 8.8k
  • 2
  • 28
  • 51

STDIN and STDOUT

Inputting to Subroutines and Functions via input variables

Public Sub A(ByRef B as String)

May be reduced down to

Sub a(b$) 

The Public and ByRef calls are the default for VBA and thus implicit, and may (almost) always be dropped.

The type literal $ forces b to be of the type String.

Other type literals

  • ! Single
  • @ Currency
  • # Double
  • % Integer
  • $ String
  • & Long
  • ^ LongLong (64 Bit Only)

Furthermore, it is generally accepted that you may leave the input variable as the default type, Variant and leave any type-based errors unhandled. Eg. Sub E(F) in which F is expected to be of type Boolean[] (which would be passed to the routine like E Array(True, False, False))

Inputting to Subroutines and Immediate Window Functions via Cells

VBA does not have a fully functional console and thus does not have any official STDIN, and thus allows for some play with passing input.

In excel, it is generally accepted to take input from a cell or range of cells, which may be done like

s=[A1]

which implicitly puts the .value from the cell [A1] (which may also be referenced as cells(1,1) or range("A1")

Example Problem: Display the input in a messagebox

Via Subroutine Sub A:msgbox[A1]:End Sub

Via Immediates Window Function msgbox[A1]

Inputting Via Conditional Compilation Arguments

VBA Projects support taking arguments from the command line or via the VBAProject Properties (view via the project explorer -> [Your VBA Project] -(Right Click)-> VBAProject Properties -> Conditional Compilation Arguments)

This is largely useful for Error Code Challenges

Given the Conditional Compilation Argument n=[some_value] this allows for executing code that will produce an error code, based off of the value of n. note, this calls for an addition of 2 bytes to your code for the n= in the conditional compilation arguments section of the VBAProject Properties Pane.

Example Code

...
#If n=3 then
return ''  Produces error code '3', Return without GoSub
#ElseIf n=20 then
resume ''  Produces error code '20', Resume without Error
#EndIf
...

Outputting Via Function Value

Not Much to say here, the general form of quoted below is about as compact as it can be made.

Public Function A(b)
    ...
    A=C
End Function

NOTE: in the vast majority of cases it is more byte convert the method to a subroutine and output to the VBE immediates window (see Below)

Outputting From Subroutines and Functions via the VBE Immediates Window

Outputting to the VBE immediates window (AKA the VBE Debug Window) is a common output method for VBA for text based challenges, however, it is important to remember that the Debug.Print "Text" call may be substantially golfed.

Debug.Print "Text"

is functionally identical to

Debug.?"Text"

as ? autoformats to Print.

Outputting from Subroutines and VBE Immediates Window functions via Other Methods

On rare occasion, when the situation is just right, you may take input from some of the more trivial inputs available to VBA such as the font size adjuster, font selector, and zoom. (Eg. Emulating the Word Font Size Selector)

Taylor Raine
  • 8.8k
  • 2
  • 28
  • 51