This gisst was made in response to a reddit thread here. OP Claims that ChatGPT is good at writing basic VBA code if you give it clear instructions.
Explaining code in such a way that it codes the correct thing is pretty awful though, slow and tedious... Let's try to do that for stdTimer.
The prompt I used was as follows:
I want to write a predeclared VBA class. Next I want to add public event
Tick
with no parameters. Next I need a remote workbook property with events and a callback object. I want aCreate
method which acts as a constructor, which creates and initialises an object via theprotInit
method. TheCreate
method should have paramsmilliseconds
and an optionalcallback
of typestdICallable
. In the initialisation method I need to set access to the VB object model to true, then create a new instance ofApplication
and within this create a newworkbook
object. Within this workbook I want to inject some macro code from agetTimerCode()
method including themilliseconds
.getTimerCode
should contain a string which DeclaresSleep
function for Mac 64-bit, Mac 32-bit, Windows 64-bit and Windows 32-bit. There should be a public boolean determining running state. In theMainLoop
method we should loop while running, and modify a range A1 of the remote workbook. It's this event we will link to in our main file to emit theTick
event. After modifying the range we should sleep for the milliseconds and doevents. After the loop has finished we should close the workbook and quit the application. The injected code should also have a way of starting this loop (non-blocking) and a way of stopping the timer as 2 macrosStartTimer
andStopTimer
. After injecting the code we should run the StartTimer macro remotely. As discussed above we should connect to the remote workbook, listen for change events and emit theTick
event and if a callback was provided we should call that as well. Would you be able to code this for me?
Chat GPT generated 2 responses and this was the code it generated.
Was it worth it? Not sure, maybe it would have been worth it to get some boiler plate down... On the other hand the prompt took a lot of writing.