Programaticaly Adding Worksheet_Change to a Worksheet

ka3bhy

New Member
Joined
Dec 20, 2007
Messages
4
I am trying to write a simple Excel data entry application for our school system, but I am stuck.

I have a vendor application that uses VB Script to create an Excel workbook. I am able to add worksheets, functions, and even a code module and a module and macro into the workbook.

I need to add a worksheet_change macro programatically to the worksheet after I have created it.

Does anyone happen to have a VB Script sample of how to do this?

Thanks,
Rod
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi, ka3bhy,
WELCOME to the Board!!!!!

This works for me. I hope it is clear for you how to make edits.
Code:
Option Explicit
Sub AddSheetWithCode()
'This code requires "trusted access to the VBE"
Dim MyVBE As Object
Dim shtCode As String
Dim sht As Worksheet
'I think this part is written by Tom Urtis
    If Val(Application.Version) >= 10 Then
    On Error Resume Next
    Set MyVBE = ActiveWorkbook.VBProject
 
        If Err.Number <> 0 Then
        MsgBox _
        "The security settings for Excel on your computer are not set to " & _
        "allow you to access this macro. This security measure " & _
        "deals with trusted access to the Visual Basic Editor, and was " & _
        "added to versions beginning with Excel 2002." & vbNewLine & vbNewLine & _
        "To establish access to this macro, follow these steps:" & vbNewLine & vbNewLine & _
        "(1) Click the OK button at the bottom of this message." & vbNewLine & _
        "(2) From the worksheet menu, click Tools > Macro > Security." & vbNewLine & _
        "(3) Select the tab named ''Trusted Publishers''." & vbNewLine & _
        "(4) Select by putting a checkmark in the box next to" & vbNewLine & _
        " ''Trust access to Visual Basic Project''." & vbNewLine & _
        "(5) Click the OK button to exit the Security dialog." & vbNewLine & vbNewLine & _
        "After that, come back here and click button again.", _
        16, "Cannot continue due to security settings - - see explanation below:"
        Exit Sub
        End If
    End If
 
    On Error GoTo 0
 
 
'add sheet and code
Application.ScreenUpdating = False
 
    Set sht = ThisWorkbook.Sheets.Add
    shtCode = _
    "Private Sub Worksheet_SelectionChange(ByVal Target As Range)" & vbNewLine & _
    "If Target.Cells.Count > 1 Then Exit Sub" & vbNewLine & _
    "MsgBox ""You selected cell "" & target.address(0,0)" & vbNewLine & _
    "End Sub"
    ThisWorkbook.VBProject.VBComponents(sht.CodeName).CodeModule.AddFromString shtCode
Application.ScreenUpdating = True
End Sub
kind regards,
Erik
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top