Create a Macro with the VBA Editor (2024)

Quickstart / Create a Macro with the VBA Editor

In This Topic

Create a Macro with the VBA Editor

In This Topic

This example shows how to create a macrothat gets text from the screen and displays it in a message box.

This article contains tabbed content that is specificto eachterminal type. Be sure the tab for yourWhich Terminal Type are you Using? is selected.

Create a macro

  1. On the Tools tab,selectVisual Basic to open the Visual Basic Editor.
  2. In the Visual Basic Project Explorer,right click onthe project folder, and choose Insert > Module.
  3. In the code window, add a subroutine by entering Sub followed by the name for the macro. For this example, enter SubCopyTextMacro().The editor creates the End Sub line.

    The name you choose for the subroutine must follow the Visual Basic naming conventions for macros. For more information, seeNaming Macros.

  4. Enter the code for the macro in your new subroutine as shown below and then save your new macro.
    • IBM
    • Open Systems
    Get text from the screen

    Copy Code

    Sub CopyTextMacro() 'Create a variable to hold the text Dim screenText As String 'Get text from the screen starting at screen row 1, column 1, and 30 characters long screenText = ThisIbmScreen.GetText(1, 1, 30) 'Display the text in a message box MsgBox (screenText )End Sub
    Get text from the screen

    Copy Code

    Sub CopyTextMacro() 'Create a variable to hold the text Dim screenText As String 'Get text from the screen starting at screeen column 1, row 1 ' and ending at row 30, column 30 screenText = ThisScreen.GetText2(1, 1, 30, 30) 'Display the text in a message box MsgBox (screenText )End Sub
  5. Place the cursor anywhere in the procedure (Sub) you just created and then press F5.
    The message box should display the text on the first line. (If your screen doesn't have any text in the upper left part of the display area, you may need to change the row and column arguments and run the macro again to display some text.)

Concepts

This macro uses an InfoConnect property (ThisScreen for Open Systems or ThisIbmScreen for IBM) togetthe screen object for the session. You can use this property to get the screen object for any macro in a session project.InfoConnect provides several other properties that you can use toget key objects when you are creating macros in session projects.For more about these properties, seeUsing the InfoConnect Object Model.

The screenobject represents the host application screen andit provides methods and properties used to access host screen data.The GetText methodused in the IBM sampleis used to get the text from a screen location.The Open Systems library also has a GetText method that you can use in the same way.

The GetText2 method used in the Open Systems sample is used to get the text in a region of the screen. The IBM libraryGetTextEx method can be usedfor the same purpose.

See Also

Create a Macro with the VBA Editor (2024)

FAQs

Create a Macro with the VBA Editor? ›

Create a macro using Visual Basic

How to create a macro with VBA? ›

Here are the steps to create such a macro:
  1. Step 1: Open Excel and Enable Developer Tab. ...
  2. Step 2: Open the Visual Basic for Applications (VBA) Editor. ...
  3. Step 3: Create a New Macro. ...
  4. Step 4: Write the Macro Code. ...
  5. Step 5: Customize the Macro Code. ...
  6. Step 6: Run the Macro.

How can a macro be created using macro editor? ›

How?
  1. In the Code group on the Developer tab, click Record Macro.
  2. Optionally, enter a name for the macro in the Macro name box, enter a shortcut key in the Shortcut key box, and a description in the Description box, and then click OK to start recording.

How do I use VBA editor in Excel? ›

To access the VBA editor in Excel, first, ensure the Developer tab is visible. If it's not, click File > Options and select Customize Ribbon. Then, check the box next to Developer in the right pane. Now, click the Dev tab and select Visual Basic or use the shortcut Alt + F11 to open the VBA editor.

How do I create a macro in VBA Access? ›

Create a standalone macro
  1. On the Create tab, in the Macros & Code group, click Macro. Access opens the Macro Builder.
  2. On the Quick Access Toolbar, click Save.
  3. In the Save As dialog box, type a name for the macro, and then click OK.
  4. Continue with the section Add actions to a macro.

Is VBA used to create macros? ›

Visual Basic for Applications is a computer programming language developed and owned by Microsoft. You can create macros to automate repetitive word- and data-processing functions with VBA and generate custom forms, graphs, and reports. VBA functions within MS Office applications.

How do I create a personal VBA macro in Excel? ›

Create and update the Personal Macro workbook
  1. On the Developer tab, in the Code group, click Record Macro.
  2. In the Record Macro dialog box, type a meaningful name for the macro in the Macro name box. ...
  3. In the Store macro in box, select Personal Macro Workbook.
  4. Click OK.
  5. Perform the actions that you want to record.

What is the easiest way to create a macro? ›

Record a macro with a button
  1. Click View > Macros > Record Macro.
  2. Type a name for the macro.
  3. To use this macro in any new documents you make, be sure the Store macro in box says All Documents (Normal. ...
  4. To run your macro when you click a button, click Button.
  5. Click the new macro (it's named something like Normal.

What is the macro code in VBA? ›

Macro code refers to the VBA (Visual Basic for Applications) code for the macro. You can create macros in Excel by either recording the steps you want it to perform (the VBA is written for you), or by writing the VBA yourself. Writing this VBA or macro code yourself gives you far greater control over your macro.

How do I use macro editor? ›

Edit a Macro
  1. Click the Developer tab.
  2. Click the Macros button. The Macro dialog box appears. ...
  3. Select a macro to edit.
  4. Click the Edit button. The Microsoft Visual Basic for Applications program appears. ...
  5. Edit the macro's code as desired.
  6. Click the Save button.
  7. Close the Visual Basic for Applications program window.

What is the difference between VBA and macros? ›

1. Macro Is the peace of code or recorded steps of user activity whereas VBA(Visual Basic For Application) is a programming language. 2. Excel Macros are recorded in VBA so we can say that VBA is a wider term which includes macros.

How do I access VBA code editor? ›

Step-by-Step Guide on How to Open Microsoft VBA
  1. Launch Office app.
  2. Go to “File” > “Options” or “Excel Options”.
  3. Click on “Customize Ribbon” or “Customize”.
  4. Look for “Main Tabs” and checkmark the box next to “Developer”.
  5. On the Developer tab, click on “Visual Basic” or the code editor icon.

What is the shortcut for VBA editor? ›

You can press the ALT key + F11 keys on keyboard to open the VBA quickly.

How to create a macro in VBA? ›

On the Tools tab, select Visual Basic to open the Visual Basic Editor. In the Visual Basic Project Explorer, right click on the project folder, and choose Insert > Module. In the code window, add a subroutine by entering Sub followed by the name for the macro.

How do I create a macro module in VBA? ›

To create a new module:
  1. On the Tools menu, click Macro > Visual Basic Editor.
  2. In the Visual Basic Editor, on the Insert menu, click Module.
  3. In the Module editing window, paste the VBA code that you want to use. ...
  4. On the File menu, click Save Global.
Oct 8, 2023

How do I add macro code to VBA? ›

Insert VBA code to Excel Workbook

Open your workbook in Excel. Press Alt + F11 to open Visual Basic Editor (VBE). Right-click on your workbook name in the "Project-VBAProject" pane (at the top left corner of the editor window) and select Insert -> Module from the context menu.

How to create a macro button in VBA? ›

Add a button (Form control)
  1. On the Developer tab, in the Controls group, click Insert, and then under Form Controls, click Button .
  2. Click the worksheet location where you want the upper-left corner of the button to appear. ...
  3. Assign a macro to the button, and then click OK.

Can macros be coded using VBA in Excel? ›

Excel VBA is Microsoft's programming language for Office applications such as MS-Excel, MS-Word, and MS-Access. Macros are what most people who write VBA code use.

Top Articles
Latest Posts
Article information

Author: Catherine Tremblay

Last Updated:

Views: 6379

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Catherine Tremblay

Birthday: 1999-09-23

Address: Suite 461 73643 Sherril Loaf, Dickinsonland, AZ 47941-2379

Phone: +2678139151039

Job: International Administration Supervisor

Hobby: Dowsing, Snowboarding, Rowing, Beekeeping, Calligraphy, Shooting, Air sports

Introduction: My name is Catherine Tremblay, I am a precious, perfect, tasty, enthusiastic, inexpensive, vast, kind person who loves writing and wants to share my knowledge and understanding with you.