Skip to main content
changed final version of example code to show full potential of this trick
Source Link
Taylor Raine
  • 8.7k
  • 2
  • 27
  • 51

Simplify built-in functions

When using certain functions frequently, reassign them to a user-defined function.

The following code (127 chars) can be reduced from:

Sub q()
w = 0
x = 1
y = 2
z = 3
a = Format(w, "0.0%")
b = Format(x, "0.0%")
c = Format(y, "0.0%")
d = Format(z, "0.0%")
End Sub

to (124 chars):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Function f(g)
f = Format(g, "0.0%")
End Function

Combining this with the ByRef trick and some autoformatting tricks, you can save even more characters (down to 11481):

Sub q()
w = 0w=0
x = 1x=1
y = 2y=2
z = 3z=3
a = f( w)
b = f( x)
c = f( y)
d = f( z)
End Sub
Sub f(g)
g = Formatg=Format(g, "0.0%")
End Sub

Do this judiciously, as VBA takes up a lot of characters to define a Function. The second code block would actually be larger than the first with any number fewer Format() calls.

Simplify built-in functions

When using certain functions frequently, reassign them to a user-defined function.

The following code (127 chars) can be reduced from:

Sub q()
w = 0
x = 1
y = 2
z = 3
a = Format(w, "0.0%")
b = Format(x, "0.0%")
c = Format(y, "0.0%")
d = Format(z, "0.0%")
End Sub

to (124 chars):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Function f(g)
f = Format(g, "0.0%")
End Function

Combining this with the ByRef trick, you can save even more characters (down to 114):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Sub f(g)
g = Format(g, "0.0%")
End Sub

Do this judiciously, as VBA takes up a lot of characters to define a Function. The second code block would actually be larger than the first with any number fewer Format() calls.

Simplify built-in functions

When using certain functions frequently, reassign them to a user-defined function.

The following code (127 chars) can be reduced from:

Sub q()
w = 0
x = 1
y = 2
z = 3
a = Format(w, "0.0%")
b = Format(x, "0.0%")
c = Format(y, "0.0%")
d = Format(z, "0.0%")
End Sub

to (124 chars):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Function f(g)
f = Format(g, "0.0%")
End Function

Combining this with the ByRef trick and some autoformatting tricks, you can save even more characters (down to 81):

Sub q
w=0
x=1
y=2
z=3
f w
f x
f y
f z
End Sub
Sub f(g)
g=Format(g,"0.0%")
End Sub

Do this judiciously, as VBA takes up a lot of characters to define a Function. The second code block would actually be larger than the first with any number fewer Format() calls.

deleted 1 character in body
Source Link
Taylor Raine
  • 8.7k
  • 2
  • 27
  • 51

Simplify built-in functions

When using certain functions frequently, reassign them to a user-defined function.

The following code (127 chars) can be reduced from:

Sub q()
w = 0
x = 1
y = 2
z = 3
a = Format(w, "0.0%")
b = Format(x, "0.0%")
c = Format(y, "0.0%")
d = Format(z, "0.0%")
End Sub
Sub q()
w = 0
x = 1
y = 2
z = 3
a = Format(w, "0.0%")
b = Format(x, "0.0%")
c = Format(y, "0.0%")
d = Format(z, "0.0%")
End Sub

to (124 chars):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Function f(g)
f = Format(g, "0.0%")
End Function
Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Function f(g)
f = Format(g, "0.0%")
End Function

Combining this with the ByRef trick, you can save even more characters (down to 114):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Sub f(g)
g = Format(g, "0.0%")
End Sub
Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Sub f(g)
g = Format(g, "0.0%")
End Sub

Do this judiciously, as VBA takes up a lot of characters to define a Function. The second code block would actually be larger than the first with any number fewer Format() calls.

Simplify built-in functions

When using certain functions frequently, reassign them to a user-defined function.

The following code (127 chars) can be reduced from:

Sub q()
w = 0
x = 1
y = 2
z = 3
a = Format(w, "0.0%")
b = Format(x, "0.0%")
c = Format(y, "0.0%")
d = Format(z, "0.0%")
End Sub

to (124 chars):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Function f(g)
f = Format(g, "0.0%")
End Function

Combining this with the ByRef trick, you can save even more characters (down to 114):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Sub f(g)
g = Format(g, "0.0%")
End Sub

Do this judiciously, as VBA takes up a lot of characters to define a Function. The second code block would actually be larger than the first with any number fewer Format() calls.

Simplify built-in functions

When using certain functions frequently, reassign them to a user-defined function.

The following code (127 chars) can be reduced from:

Sub q()
w = 0
x = 1
y = 2
z = 3
a = Format(w, "0.0%")
b = Format(x, "0.0%")
c = Format(y, "0.0%")
d = Format(z, "0.0%")
End Sub

to (124 chars):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Function f(g)
f = Format(g, "0.0%")
End Function

Combining this with the ByRef trick, you can save even more characters (down to 114):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Sub f(g)
g = Format(g, "0.0%")
End Sub

Do this judiciously, as VBA takes up a lot of characters to define a Function. The second code block would actually be larger than the first with any number fewer Format() calls.

replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

Simplify built-in functions

When using certain functions frequently, reassign them to a user-defined function.

The following code (127 chars) can be reduced from:

Sub q()
w = 0
x = 1
y = 2
z = 3
a = Format(w, "0.0%")
b = Format(x, "0.0%")
c = Format(y, "0.0%")
d = Format(z, "0.0%")
End Sub

to (124 chars):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Function f(g)
f = Format(g, "0.0%")
End Function

Combining this with the ByRef trickByRef trick, you can save even more characters (down to 114):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Sub f(g)
g = Format(g, "0.0%")
End Sub

Do this judiciously, as VBA takes up a lot of characters to define a Function. The second code block would actually be larger than the first with any number fewer Format() calls.

Simplify built-in functions

When using certain functions frequently, reassign them to a user-defined function.

The following code (127 chars) can be reduced from:

Sub q()
w = 0
x = 1
y = 2
z = 3
a = Format(w, "0.0%")
b = Format(x, "0.0%")
c = Format(y, "0.0%")
d = Format(z, "0.0%")
End Sub

to (124 chars):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Function f(g)
f = Format(g, "0.0%")
End Function

Combining this with the ByRef trick, you can save even more characters (down to 114):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Sub f(g)
g = Format(g, "0.0%")
End Sub

Do this judiciously, as VBA takes up a lot of characters to define a Function. The second code block would actually be larger than the first with any number fewer Format() calls.

Simplify built-in functions

When using certain functions frequently, reassign them to a user-defined function.

The following code (127 chars) can be reduced from:

Sub q()
w = 0
x = 1
y = 2
z = 3
a = Format(w, "0.0%")
b = Format(x, "0.0%")
c = Format(y, "0.0%")
d = Format(z, "0.0%")
End Sub

to (124 chars):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Function f(g)
f = Format(g, "0.0%")
End Function

Combining this with the ByRef trick, you can save even more characters (down to 114):

Sub q()
w = 0
x = 1
y = 2
z = 3
a = f(w)
b = f(x)
c = f(y)
d = f(z)
End Sub
Sub f(g)
g = Format(g, "0.0%")
End Sub

Do this judiciously, as VBA takes up a lot of characters to define a Function. The second code block would actually be larger than the first with any number fewer Format() calls.

corrected lang from vb to vba
Source Link
Taylor Raine
  • 8.7k
  • 2
  • 27
  • 51
Loading
Mod Removes Wiki by Doorknob
added 367 characters in body
Source Link
Gaffi
  • 3.7k
  • 2
  • 23
  • 54
Loading
edited body
Source Link
Gaffi
  • 3.7k
  • 2
  • 23
  • 54
Loading
added 34 characters in body
Source Link
Gaffi
  • 3.7k
  • 2
  • 23
  • 54
Loading
Source Link
Gaffi
  • 3.7k
  • 2
  • 23
  • 54
Loading