Instead of starting from
scratch, if you need an Excel macro, you can often find sample code at
reputable sites on the internet. To copy that code, and add it to one of your workbooks,
follow these steps:
- Copy the sample
code that you want to use
- Open the
workbook in which you want to add the code
- Hold the Alt
key, and press the F11 key, to open the Visual Basic Editor
- Choose Insert |
Module
- Where the cursor
is flashing, choose Edit | Paste
To run the code:
- On the Excel
Ribbon, click the View tab
- At the far
right, click Macros
- Select a macro
in the list, and click the Run button
Another type of Excel
code is Event code, which runs automatically when something specific occurs in
the workbook. For example, if you enter a number in a cell, or select an entry
in a cell's drop down list, the worksheet has been changed. This could trigger
the Worksheet_Change event.
Worksheet event code is
stored on a worksheet module. To add worksheet event code to your worksheet, do
the following:
- Copy the code
that you want to use
- Select the
worksheet in which you the code to run
- Right click on
the sheet tab and click View Code, to open the Visual Basic
Editor.
- Where the cursor
is flashing, choose Edit | Paste
Another type of code is
Workbook Event code, which should be added to the workbook code module:
- Copy the code
that you want to use
- Select the
workbook in which you want to store the code
- Hold the Alt
key, and press the F11 key, to open the Visual Basic Editor
- In the Project
Explorer, find your workbook, and open the list of Microsoft Excel Objects
- Right-click on
the ThisWorkbook object, and choose View Code
- Where the cursor
is flashing, choose Edit | Paste
You may find code in a
sample workbook online, and decide to add it to one of your workbooks. You can
copy all the code in a module by doing the following:
- Open both
workbooks
- Hold the Alt
key, and press the F11 key, to open the Visual Basic Editor
- In the Project
Explorer, find your workbook, and the workbook with the code that you want
to copy. The screenshot at the right, the code is in VBACodeCopy.xls and
will be copied to MyForm.xlsm
- In the workbook
with the code, click the + sign to view the list of Modules
- Click on the
module that you want to copy, and drag it over the project where you'd
like the copy placed.
- Release the
mouse button, and a copy of the module will appear in the workbook.
To run the code:
- On the Excel
Ribbon, click the View tab
- At the far
right, click Macros
- Select a macro
in the list, and click the Run button
To use macros in Excel,
you might need to enable them when the file opens. If you are using macros for
the first time on your current computer, you might also need to adjust the
macro security settings.
Follow the instructions
below, to make these changes.
Enable Macros When Opening the File
When you open a workbook
that contains macros, you might see a security warning, at the top of the
worksheet, above the Formula Bar.
- Click the
Options button.
- Click Enable
This Content, to allow the workbook's macros to run, and click OK.
Check Your Macro Security Settings
If you haven't run macros
before, you might need to change your macro security level. (You may have to
clear this with your IT department.)
- On the Ribbon,
click the Developer tab, and in the Code group, click Macro Security.
- In the Macro
Settings category, under Macro Settings, click Disable all macros with
notification
- Click OK.
- If you changed
the setting, close the workbook, and then reopen it
After you copy a macro to
a regular module, follow the steps below, to run the macro. If the macro does
not run, check your macro settings.
To run an Excel macro:
- Copy the macro
code to a regular code module in your file.
- Then, on the
Ribbon's View tab, click the top part of the Macro button, to open the
Macro window
- In the list of
macros, click on the macro that you want to run
- Click the Run button
No comments:
Post a Comment
Thank You.
https://linwintech.blogspot.com/