Search code examples
vbarubberduck

With New MyTestableMacro .Run - What does it do?


I am following this tutorial by RubberduckVBA and have come across this piece of code, but have no idea what it really does:

With New MyTestableMacro
    .Run dataServiceStub, wsServiceStub
End With

I have tried to search for "New keyword in With statement" as well as a few other things, but do not find anything about it.

It is not code that I have seen in a year of doing VBA...

I have also tried making a class named MyTestableMacro with a method Run which also did not work.

Just to be clear, I want to know what MyTestableMacro is. Is it a class, module, temporary Macro? And is the Run A method/function inside of it?


Solution

  • Conceptually, this is no different from the following code:

    Dim something As MyTestableMacro
    Set something = New MyTestableMacro
    With something
        ...
    End With
    

    Note that we had to Dim a variable, then initialize it with the New keyword. But we can cut out the 2 extra lines by compressing them into this single statement:

    With New MyTestableMacro
        ...
    End With
    

    In regards to your another question -- the MyTestableMacro ought to be a class module. Otherwise you wouldn't be able to New it at all. And the Run typically would be a Public method (could be a Sub or a Function, given the syntax used in your post). A Private method would not work. Friend would work only for within the same VBA project but it's very rare to see a method using Friend instead of Public or Private.