Automate Testing for Immediate Window Functions
This response seeks to make the testing of Anonymous VBE Immediate Window Functions that take input from the range [A1]
and output to the immediate window.
Testing immediate window functions can take hours if done by hand, so rather than doing that, take advantage of the Microsoft Visual Basic for Applications Extensibility 5.3 reference and and helper function to automate testing these functions. (you will need to give Excel access to the VBE project model - remember to revoke this access when you are done!)
Example
As sample of code that can do this is shown below - must be pasted into the top of Module1
to function.
Private Sub helper()
'' At runtime, the program will be pasted below, with `?` replaced as `Debug.Print`
'' At runtime, the program will be pasted above
End Sub
Sub cgTester(ByVal program As String, ParamArray testCases())
'' Using `MicroSoft Visual Basic for Applications Extensibility 5.3` and
'' having access to the VBA Project Model
Dim testCase As Variant, _
vbProj As VBIDE.VBProject, _
vbComp As VBIDE.VBComponent
Debug.Print vbCrLf; vbCrLf; program; vbCrLf; vbCrLf
Set vbProj = Application.ThisWorkbook.VBProject
Set vbComp = vbProj.VBComponents.Item("Module1")
Debug.Assert (vbComp.CodeModule.Lines(2, 1) = _
" '' At runtime, the program will be pasted below, with `?` replaced as `Debug.Print`")
Debug.Assert (vbComp.CodeModule.Lines(4, 1) = _
" '' At runtime, the program will be pasted above")
'' "paste" in the program above, where indicated
Call vbComp.CodeModule.InsertLines(3, Replace(program, "?", "Debug.?"))
Call vbComp.CodeModule.DeleteLines(4, 1)
For Each testCase In testCases
Let [A1].Value = testCase
Debug.Print "<- `"; testCase; "`"; vbCrLf; "->";
Call helper '' Call the program
Debug.Print '' Print for spacing
Next testCase
Call vbComp.CodeModule.InsertLines(3, "") '' Clear out the program to be ready for next use
Call vbComp.CodeModule.DeleteLines(4, 1)
End Sub
Usage
Shown as used on my solution to the Facey McFaceface question.
program = "b=[Left(Upper(A1))]+Mid([Lower(A1)],2):l=len(b):y=[Right(A1)=""y""]:?b;IIf(y,"""",""y"")"" Mc""IIf(y,Left(b,l-1+(InStrRev(b,""e"")=l-1)),b)""face"
cgTester program, "boat", "Face", "Dog", "Family", "Lady", "Donkey", "Player", "yyy", "DJ Grand Master Flash"
b=[Left(Upper(A1))]+Mid([Lower(A1)],2):l=len(b):y=[Right(A1)="y"]:?b;IIf(y,"","y")" Mc"IIf(y,Left(b,l-1+(InStrRev(b,"e")=l-1)),b)"face
<- `boat`
->Boaty McBoatface
<- `Face`
->Facey McFaceface
<- `Dog`
->Dogy McDogface
<- `Family`
->Family McFamilface
<- `Lady`
->Lady McLadface
<- `Donkey`
->Donkey McDonkface
<- `Player`
->Playery McPlayerface
<- `yyy`
->Yyy McYyface
<- `DJ Grand Master Flash`
->Dj grand master flashy McDj grand master flashface