+ Reply to Thread
Results 1 to 5 of 5

Modify VBA Code Dynamically when copying and exisiting worksheet to a new one

  1. #1
    Registered User
    Join Date
    05-07-2016
    Location
    New York City
    MS-Off Ver
    2013
    Posts
    24

    Modify VBA Code Dynamically when copying and exisiting worksheet to a new one

    I have an Excel 2016 workbook with one worksheet.
    Within are many lines of code which refer to "Sheet1" (i.e. Sheet1.Select)

    At the beginning of a new year, a new worksheet is created ("worksheet2") using VBA code.
    It's simply a backup to worksheet1 in a new tab.
    That seems to be working well.

    However, I just noticed that certain aspects of the code within the newly created "worksheet2" do not execute properly.
    Upon further inspection, I see that the code in "worksheet2" still refer to "worksheet1" (i.e. Sheet1.Select)

    Is there a way to dynamically change the VBA code in the new "worksheet2" to point to "worksheet2" during its creation,
    rather than still point to "workshheet1"

    I hope I've explained my problem well.
    Thanks for any help

  2. #2
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Modify VBA Code Dynamically when copying and exisiting worksheet to a new one

    Here is a shot in the dark. If you change the code that says sheet1 to activesheet, then the code will apply to whichever sheet is active. Hard to tell if this will work correctly for you without seeing your workbook, though.

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

    Re: Modify VBA Code Dynamically when copying and exisiting worksheet to a new one

    Where is the code located?

    Is there only one sheet in the workbook?
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    05-07-2016
    Location
    New York City
    MS-Off Ver
    2013
    Posts
    24

    Re: Modify VBA Code Dynamically when copying and exisiting worksheet to a new one

    Great shot it the dark....brilliant!
    Works great.
    Thank you!

  5. #5
    Registered User
    Join Date
    05-07-2016
    Location
    New York City
    MS-Off Ver
    2013
    Posts
    24

    Re: Modify VBA Code Dynamically when copying and exisiting worksheet to a new one

    maniacb solved my problem, but to answer your questions.

    1. The code is located in both Module1, Workbook1 and Workbook2

    2. No, there are now 2 workbooks, but each January another will be created.

    Thanks for your help.

+ 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. Dynamically place add a frame within an exisiting frame
    By ryan.wherry in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-09-2017, 08:50 PM
  2. Modify Worksheet VBA Code
    By ibuworks in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-27-2016, 09:41 AM
  3. Copying rows to an exisiting worksheet based on a defined criteria
    By dansmithmore in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-08-2013, 07:45 AM
  4. Copying cells dynamically to a worksheet based on values in another worksheet
    By freelance in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-10-2013, 01:45 PM
  5. Macro - Copying Exisiting sheets into a new workbook
    By danielhadfield in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-03-2012, 09:54 AM
  6. Dynamically add code to a worksheet
    By elliebee in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-14-2010, 12:29 PM
  7. Exisiting code to work regardless of worksheet name
    By ge0rge in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-08-2008, 10:55 AM

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