Results 1 to 4 of 4

Add Code To Worksheet with VBA

Threaded View

  1. #1
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Add Code To Worksheet with VBA

    Hi,

    Once again I find myself having to edit some code in around 52 workbooks, each workbook has one sheet. I was able to use this code below to delete the problem worksheet code. However what I want now is to add some code back into the worksheet. I have seen several examples on how to add Mods and placing code into mods and even how to add code into Thisworkbook. but havent found anything about adding in sheet code.

    Anyway does anyone know a way to add a private sub routine into a worksheet?

    Here is what I used to remove the code. Can someone help modify this to add code back in?

    
    Sub DeleteProcedureFromModule()
    Const vbext_pk_Proc As Integer = 0
    Const vbext_ct_Document As Integer = 100
        
    Dim VBProj As Object
    Dim VBComp As Object
    Dim CodeMod As Object
    Dim StartLine As Long
    Dim NumLines As Long
    Dim ProcName As String
    Dim ws As Object
        
        Set VBProj = ActiveWorkbook.VBProject
        For Each ws In ActiveWorkbook.Worksheets
            Set VBComp = VBProj.vbcomponents(ws.CodeName)
            Set CodeMod = VBComp.CodeModule
        
            ProcName = "CommandButton3_Click"
            With CodeMod
                On Error Resume Next
                StartLine = .ProcStartLine(ProcName, vbext_pk_Proc)
                If Err.Number = 0 Then
                    NumLines = .ProcCountLines(ProcName, vbext_pk_Proc)
                    .DeleteLines StartLine:=StartLine, Count:=NumLines
                ElseIf Err.Number = 35 Then
                    Err.Clear
                End If
            End With
        Next
    End Sub
    Thanks as always, Mike
    Last edited by realniceguy5000; 07-14-2010 at 12:32 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1