Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel vba add code to sheet module programmatically

Tags:

How to put the programmatically generated workbook an event code similar to below:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim nextTarget As Range

    Set nextTarget = Range(Selection.Address) 'store the next range the user selects

    Target.Columns.Select 'autofit requires columns to be selected
    Target.Columns.AutoFit

    nextTarget.Select
End Sub
like image 936
vims liu Avatar asked Jan 17 '16 09:01

vims liu


People also ask

How do I add VBA code to Excel?

Insert VBA code to Excel Workbook 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. Copy the VBA code (from a web-page etc.) and paste it to the right pane of the VBA editor ("Module1" window).

How to add a VBA module to thisworkbook?

Actually, I need a macro (Personal.XLSB) that add a VBA module & write code in ThisWorkbook for any new worksheet. By any of the following way: 1. The module is saved and just need to import to the new sheet. Code need to be wrttin automatically to ThisWorkbook. 2. Code for the module need to be writin in into new module.

How to add new sheets in a workbook using VBA code?

Different Ways to Add New Sheets in a Workbook using a VBA Code. 1. Add a Single Sheet. To add a single sheet, you can use the below code, where you didn’t specify any argument. This code tells Excel to add a sheet ... 2. Add Multiple Sheets. 3. Add a Sheet with a Name. 4. Add a Sheet with a Name ...

How to import a module to a new worksheet?

The module is saved and just need to import to the new sheet. Code need to be wrttin automatically to ThisWorkbook. 2. Code for the module need to be writin in into new module. Code need to be wrttin automatically to ThisWorkbook. This is to make the new worksheet run backup automatically. Here is the model and vba for you guys to use.

Where are the modules located in a workbook?

The modules are located in the Modules folder within the workbook. Sheet Modules – Each sheet in the workbook has a sheet object in the Microsoft Excel Objects folder. Double-clicking the sheet object opens its code module where we can add event procedures (macros).


1 Answers

Use this to add a workbook and place a worksheet change event into the Sheet1 module.

Sub AddSht_AddCode()
    Dim wb As Workbook
    Dim xPro As VBIDE.VBProject
    Dim xCom As VBIDE.VBComponent
    Dim xMod As VBIDE.CodeModule
    Dim xLine As Long

    Set wb = Workbooks.Add

    With wb
        Set xPro = .VBProject
        Set xCom = xPro.VBComponents("Sheet1")
        Set xMod = xCom.CodeModule

        With xMod
            xLine = .CreateEventProc("Change", "Worksheet")
            xLine = xLine + 1
            .InsertLines xLine, "  Cells.Columns.AutoFit"
        End With
    End With

End Sub

When you 1st run the code you may get an error.

enter image description here

Hit the Stop Icon and select the tools menu and "References"

enter image description here

enter image description here

Then find "Microsoft Visual Basic for Applications Extensibility 5.3 library" and check it.

enter image description here

Run the code again and it should work.

like image 127
Davesexcel Avatar answered Sep 23 '22 16:09

Davesexcel