Many of the books that teach programming like to start with a Hello World applet, and our VBA tour begins here.
Into the VBA editor, insert a module in the Insert menu, and then insert our first program from the Insert menu with the name ExcelbaSub1, and then the VBA editor automatically generates a child procedure that types our code in the middle of the child procedure:
Public Sub ExcelbaSub1()
Dim TStr As String
TStr = "Hello World!"
Application.ActiveSheet.Range("A1").Value = TStr
Debug.Print TStr
MsgBox TStr, , "http://bianceng.cn"
End Sub
Our Hello World applet is finished, following the run Sub process/user form in the Run menu, or clicking on the toolbar's Run button, or pressing F5 to run our program, in addition to the tools/macros/macros in the Excel main interface. Select ExcelbaSub1 to run.
Let's explain the purpose of each sentence of this program:
The first sentence: The Public keyword declares that the subroutine is common, the sub is the keyword that declares the subroutine, and ExcelbaSub1 is our subroutine name.
The second sentence: Dim is the keyword that declares the variable, which we use to declare a string variable named Tstr.
The third sentence: Assign tstr this variable to Hello world!
Set the value of the A1 on the current worksheet to Hello world!
Use the Debug.Print Immediate window to display the value of the TSTR.
Sixth: Use the message dialog box to display the value of the TSTR.
Last sentence: Mark End subroutine.
Although the program is small, but the most basic elements are all, perhaps, this is the charm of Hello World, * ~_~ *