STDIN and STDOUT
Inputting to Sub
routines and Function
s 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 Sub
routines 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 Sub
routines and Function
s 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 Sub
routines 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)
Converted to Community Wiki as per policy. – dmckee --- ex-moderator kitten – 2012-03-16T15:24:21.727
Sorry that wasn't automatic on my part! – Gaffi – 2012-03-16T15:26:22.097
No trouble. Actually, they've taken the power to make questions CW away from users (you can still do answers, I think). You could flag for moderator attention, but as little activity as CodeGolf gets that is hardly necessary. – dmckee --- ex-moderator kitten – 2012-03-16T15:28:13.157
2VBA is a relatively verbose language with few syntax shortcuts. If you're going for best score, VBA may not be a good choice. If you're looking to hone your skills, more power to ya. – Mr. Llama – 2012-03-20T17:55:08.037
2@GigaWatt Honing my skills it is. Actually since playing around with different challenges, I've already picked up a few new tricks for working with VBA! I don't expect to win any real [tag:code-golf] challenges with VBA, but it's good practice. :-) – Gaffi – 2012-03-20T17:59:34.127