Adding VBA Code's to an Excel Workbook

Copy Excel VBA Code to a Regular Module:
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: 
  1. Copy the sample code that you want to use
  2. Open the workbook in which you want to add the code
  3. Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
  4. Choose Insert | Module
  5. Where the cursor is flashing, choose Edit | Paste
To run the code:
  1. On the Excel Ribbon, click the View tab
  2. At the far right, click Macros
  3. Select a macro in the list, and click the Run button
Copy Excel VBA Code to a Worksheet Module
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:
  1. Copy the code that you want to use
  2. Select the worksheet in which you the code to run
  3. Right click on the sheet tab and click View Code, to open the Visual Basic Editor.

  1. Where the cursor is flashing, choose Edit | Paste
Copy Excel VBA Code to a Workbook Module
Another type of code is Workbook Event code, which should be added to the workbook code module:
  1. Copy the code that you want to use
  2. Select the workbook in which you want to store the code
  3. Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
  4. In the Project Explorer, find your workbook, and open the list of Microsoft Excel Objects
  5. Right-click on the ThisWorkbook object, and choose View Code
  6. Where the cursor is flashing, choose Edit | Paste

Copy Excel VBA Code From a Different Workbook
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:
  1. Open both workbooks
  2. Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
  3. 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
  4. In the workbook with the code, click the + sign to view the list of Modules
  5. Click on the module that you want to copy, and drag it over the project where you'd like the copy placed.
  6. Release the mouse button, and a copy of the module will appear in the workbook.


To run the code:
  1. On the Excel Ribbon, click the View tab
  2. At the far right, click Macros
  3. Select a macro in the list, and click the Run button
Allow Macros to Run in Your Workbook
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.
  1. Click the Options button.
  2. 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.)
  1. On the Ribbon, click the Developer tab, and in the Code group, click Macro Security.
  2. In the Macro Settings category, under Macro Settings, click Disable all macros with notification
  3. Click OK.
  4. If you changed the setting, close the workbook, and then reopen it
Run an Excel Macro
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:
  1. Copy the macro code to a regular code module in your file.
  2. Then, on the Ribbon's View tab, click the top part of the Macro button, to open the Macro window
  3. In the list of macros, click on the macro that you want to run
  4. Click the Run button



No comments:

Post a Comment

Thank You.

https://linwintech.blogspot.com/