Write your first VBA program

Implements an automatic new worksheet in the active workbook and renames it "Salary Summary".

(1) Open the VBE window, click the menu "Insert" - "Module", and create a new module in the module (if you delete a module, you can click the delete module object, right-click and select Remove Module, then a box "Remove Module" will pop up. "Export before" according to your own situation.), and the code window will pop up automatically, if not, you can double-click the module object to open the code window; place the cursor in the code window of the module, and click the menu "Insert" - " Process", the "Add Process" dialog box pops up, enter the name AddSheet, click OK, and the custom process frame will be automatically generated in the code window corresponding to the module; enter the code in the AddSheet process frame to automatically create a new name called "Wage Summary", the code as follows:

Public Sub AddSheet()

'Create a new worksheet and name it "Salary Summary"

Dim ws As Worksheet

Set ws = Worksheets.Add

ws.Name="salary summary"

End Sub




Click the triangle button in the toolbar to run the AddSheet process in the module. After executing the code, a new worksheet named "Salary Summary" is added to the current workbook.



(2) If you want to automatically generate another worksheet and name it another name, you can change the second "salary summary" in the above code to another name, and then click the triangle to run to generate a new worksheet in the current workbook. Zhang new worksheets, such as:



(3) To save the code, save the workbook where it is located as "Excel Macro-Enabled Workbook".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325768049&siteId=291194637