+ Reply to Thread
Results 1 to 4 of 4

Add Code To Worksheet with VBA

Hybrid View

realniceguy5000 Add Code To Worksheet with VBA 07-14-2010, 09:41 AM
romperstomper Re: Add Code To Worksheet... 07-14-2010, 10:12 AM
realniceguy5000 Re: Add Code To Worksheet... 07-14-2010, 10:47 AM
realniceguy5000 Re: Add Code To Worksheet... 07-14-2010, 12:32 PM
  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.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: Add Code To Worksheet with VBA

    If you know how to add code into the ThisWorkbook module, there shouldn't really be much difference adding it to a worksheet module.
    The fact that you are having to do this repeatedly suggests to me you might want to rethink whatever it is that you are doing...
    Everyone who confuses correlation and causation ends up dead.

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

    Re: Add Code To Worksheet with VBA

    Quote Originally Posted by romperstomper View Post
    If you know how to add code into the ThisWorkbook module, there shouldn't really be much difference adding it to a worksheet module.
    The fact that you are having to do this repeatedly suggests to me you might want to rethink whatever it is that you are doing...
    Understood, I'll work on some type of code and do some experimenting. This normally happens once a year since I create these workbooks.worksheets from last years templates from the previous year. I'm now working on 2011 workbooks for the year so that is why the change occurs. I just don't want to recreate the workbooks again is all.

    I'll keep looking. Thank You,

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

    Re: Add Code To Worksheet with VBA

    Quote Originally Posted by romperstomper View Post
    If you know how to add code into the ThisWorkbook module, there shouldn't really be much difference adding it to a worksheet module.
    The fact that you are having to do this repeatedly suggests to me you might want to rethink whatever it is that you are doing...
    This wasn't so Hard After All.

    Here is a solution should anyone find the need...

    This will loop though all workbooks in a folder and delete the current procedure name and add the same procedure back with new code.

    Sub OpenALL()
    
    Dim lCount As Long
    Dim wbResults As Workbook
    Dim wbCodeBook As Workbook
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    
    On Error Resume Next
    
    Set wbCodeBook = ThisWorkbook
    
    With Application.FileSearch
            .NewSearch
             
            .LookIn = "G:\Hazleton Production\2011 Production\Production Links\2011 Large Area"
            .FileType = msoFileTypeExcelWorkbooks
            '.Filename = "Book*.xls"
    
        If .Execute > 0 Then
    
              For lCount = 1 To .FoundFiles.Count
                                  
              Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
              
                    Call DeleteProcedureFromModule
               
              wbResults.Close SaveChanges:=True
              
              Next lCount
         End If
    End With
    
        On Error GoTo 0
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
        Application.EnableEvents = True
    End Sub
    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
            
            With CodeMod
                On Error Resume Next
                 
                LineNum = .CountOfLines + 1
                .InsertLines LineNum, "Private Sub CommandButton3_Click()"
                LineNum = LineNum + 1
                .InsertLines LineNum, "Workbooks.Open ""G:\Hazleton Production\2011 Production\Production Links\Sheet Lists\2011 Large Area Sheet List.xls"""
                LineNum = LineNum + 1
                .InsertLines LineNum, "End Sub"
            End With
    
        Next
    End Sub

+ Reply to Thread

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