+ Reply to Thread
Results 1 to 10 of 10

How to copy code module from one worksheet to another.

  1. #1
    Forum Contributor
    Join Date
    01-08-2017
    Location
    Salt Lake City, Ut.
    MS-Off Ver
    2019 and 365
    Posts
    135

    How to copy code module from one worksheet to another.

    Basically I would think this should be pretty simple, but everything that I have tried is not working for me. First, I am using Excel 2016 and working with a workbook where when needed it copies one worksheet called template to another. The template worksheet has a simple Print button on it. The copy procedure copies the print button OK, but the simple CommandButton1_Click code which simple contains "PrintSelection" does not get copied over. I want to automate this so that other users can add new sheets and the Print button will be ready for their use.

    I have researched and tried a number of things. I have added the reference to MS VB for Applications Extensibility 5.3 to the VB project window and I have tried a couple of different subs but they fail with the message "subscript out or range" errors.

    One example is"
    "Sub copySheetCode()

    Dim src As CodeModule
    Dim dest As CodeModule

    Set src = ThisWorkbook.VBProject.VBComponents("Template").CodeModule

    Set dest = ThisWorkbook("test").VBProject.VBComponents _
    .CodeModule

    dest.DeleteLines 1, dest.CountOfLines
    dest.AddFromString src.Lines(1, src.CountOfLines)

    End Sub
    "

    The error occurs on the first like starting with "Set src"

    Does anyone have any idea what I might need to do to resolve this, or any other way of achieving what I want to do?

    Thanks in advance, all help is greatly appreciated.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: How to copy code module from one worksheet to another.

    Where have you put this code?

    Also, why are you referring to ThisWorkbook for both the source and destination workbook?

    ThisWorkbook is a reference to the workbook the code is in.
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    01-08-2017
    Location
    Salt Lake City, Ut.
    MS-Off Ver
    2019 and 365
    Posts
    135

    Re: How to copy code module from one worksheet to another.

    The code is in the worksheet code area. The "Thisworkbook is what I copied from my search. I have also tried:

    [code]<Sub copyCode()
    Dim CodeCopy As VBIDE.CodeModule
    Dim CodePaste As VBIDE.CodeModule
    Dim numLines As Integer

    Set CodeCopy = ActiveWorkbook.VBProject.VBComponents("Template").CodeModule
    Set CodePaste = ActiveWorkbook.VBProject.VBComponents("test").CodeModule

    numLines = CodeCopy.CountOfLines
    'Use this line to erase all code that might already be in sheet3:
    'If CodePaste.CountOfLines > 1 Then CodePaste.DeleteLines 1, CodePaste.CountOfLines

    CodePaste.AddFromString CodeCopy.Lines(1, numLines)

    End Sub>[code]

    Both give me the same error: run time error 9, "subscript out or range"

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to copy code module from one worksheet to another.

    Change the name of the destination workbook to suit and include the file extension

    Change the name of the destination code module (Module1) to suit.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    Forum Contributor
    Join Date
    01-08-2017
    Location
    Salt Lake City, Ut.
    MS-Off Ver
    2019 and 365
    Posts
    135

    Re: How to copy code module from one worksheet to another.

    It still results in the same error 9 subscript out of range. Also I am not trying to copy code into a module but into the same area where if you placed code when the worksheet was activated or deactivated would appear. When you add the commandbutton1 control and then right click on it to view code, it places the code in this same section for the worksheet control codes.
    Please Login or Register  to view this content.
    I am simply trying to find a way to automate placing this code there since the copy of the template sheet is created in a new worksheet with all the formulas, etc and the print commandbutton1 control, but the above code is not copied over into that worksheet.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: How to copy code module from one worksheet to another.

    Are you copying within the same workbook or from one workbook to another?

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: How to copy code module from one worksheet to another.

    This works for me.
    Change newsheetname to the newly created sheetname.
    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to copy code module from one worksheet to another.

    Quote Originally Posted by dentler View Post

    I am simply trying to find a way to automate placing this code there since the copy of the template sheet is created in a new worksheet with all the formulas, etc and the print commandbutton1 control, but the above code is not copied over into that worksheet.
    Show your code where you copy the template. If you do it correctly, it will copy the command buttons and their code as well.

    This simple macro would copy a worksheet named "Template" as a new workbook and include the ActiveX command buttons and their code.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    01-19-2018
    Location
    Michigan, USA
    MS-Off Ver
    2016
    Posts
    9

    Re: How to copy code module from one worksheet to another.

    Try this code in the button code of the template sheet, do this by right clicking the button and viewing code.

    The different Var names are not really necessary they just indicate to me what I intended them to be used for. One caveat on copying a sheet with activex components on it. What I have found is that the component on the new copy will, when triggered run the matching code on the template first then run its own version of the code. I have been unable to find a way to stop this.

    Please Login or Register  to view this content.
    Last edited by TravelByFireworks; 01-19-2018 at 10:57 AM.

  10. #10
    Registered User
    Join Date
    01-19-2018
    Location
    Michigan, USA
    MS-Off Ver
    2016
    Posts
    9

    Re: How to copy code module from one worksheet to another.

    Never mind the caveat. I had to use the Me. construct to get around what was happening to my template. Thanks to Leith Ross for that long sought after solution.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Calling Code in Another Worksheet or Module
    By snapfade in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-19-2016, 03:46 PM
  2. Sending VBA code from a Module to Code a worksheet
    By jacksocket in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-22-2013, 08:06 PM
  3. new to vba, module or worksheet code?
    By c2q in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-09-2012, 07:33 PM
  4. how to access Sheet module, normal module, Worbook module to type code
    By alibaba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2009, 07:51 AM
  5. Code in userform/worksheet vs. in Module
    By davegb in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-19-2006, 11:20 AM
  6. Run worksheet module code from workbook module?
    By keithb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-14-2005, 12:05 AM
  7. Create a newworksheet with VBA code and put VBA code in the new worksheet module
    By ceshelman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-15-2005, 12:05 PM

Tags for this Thread

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