Hello world! of VBA

Source: Internet
Author: User

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, * ~_~ *

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.