Consulting

Results 1 to 6 of 6

Thread: Add worksheet change event to all sheets during runtime

  1. #1
    VBAX Regular
    Joined
    Oct 2021
    Posts
    16
    Location

    Exclamation Add worksheet change event to all sheets during runtime

    Hello, so what I am trying to achieve is, on button click the file will create a new workbook and add n number of sheets and n number of tables in each sheet, which is already done thanks to p45cal and arnelgp's help. And then on each of those sheets, the code provided by p45cal or arnelgp (again) here http://www.vbaexpress.com/forum/show...rksheet_change

    I found this on a different site:

    Sub AddCode()    
        Dim ws As Worksheet: Set ws = Worksheets("Sheet1")
        Dim wb As Workbook:  Set wb = ActiveWorkbook
        Dim code As String:  code = "Sub Duh()" & vbCrLf & " debug.print(""I'm Here!"")" & vbCrLf & "End Sub"
    
    
        Dim lineCount As Integer
    
    
        With wb.VBProject.VBComponents(ws.Name).CodeModule
            lineCount = .CountOfLines
            If lineCount > 0 Then
                .DeleteLines 1, lineCount
            End If
            .AddFromString code
        End With
    End Sub
    Which works however if I change the sheet name to my sheet name (example 15 minutes Inter - 1st Week Nov), it's giving an error saying Subscript out of range. But that's impossible because my sheet is named exactly like that and I am running the code above on that same workbook, unless I am misunderstanding what that error means.

    Also there will be 4 or 5 sheets and the code above will only add the worksheet change event to just the indicated sheet and I need the code be added to all the sheets in that workbook

    I am using Excel 365. Thanks to anyone who will help enlighten me

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    is wb the new workbook?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Hang on.. if you're using arnelgp's code which I think is copying a range over and over from a template sheet, then instead of copying the range to a new newly added blank sheet in a (new) workbook, you make a new sheet by copying the whole template sheet (then further added tables on that sheet can be created by only copying a range (disable events first)), any code behind the sheet being copied from the template workbook will go with it automatically.
    I haven't time just now to look at the code - perhaps ask arnelgp?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I think you're confusing the worksheet .Name (15 minutes Inter - 1st Week Nov) and it's .CodeName (Sheet1), which you can change BTW

    Capture.JPG

    Option Explicit
    
    
    Sub AddCode()
        Dim ws As Worksheet: Set ws = Sheet1    '   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        Dim wb As Workbook:  Set wb = ActiveWorkbook
        Dim code As String:  code = "Sub Duh()" & vbCrLf & " debug.print(""I'm Here!"")" & vbCrLf & "End Sub"
    
        With wb.VBProject.VBComponents(ws.CodeName).CodeModule  '   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            If .CountOfLines > 0 Then .DeleteLines 1, .CountOfLines
            .AddFromString code
        End With
    End Sub
    Attached Files Attached Files
    Last edited by Paul_Hossler; 11-05-2021 at 06:56 PM. Reason: Typo
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Regular
    Joined
    Oct 2021
    Posts
    16
    Location
    Hey guys, I haven't been able to check your replies as I had an accident last Friday. I am still recovering. I will try this out as soon as I go back to work. I really appreciate the time you all are giving me.

  6. #6
    get well soon
    in ph or elsewhere, which region in ph?

Tags for this Thread

Posting Permissions

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