+ Reply to Thread
Results 1 to 6 of 6

VBA - Create a new sheet with shifting references

  1. #1
    Registered User
    Join Date
    08-30-2021
    Location
    Toronto, Canada
    MS-Off Ver
    365
    Posts
    3

    Question VBA - Create a new sheet with shifting references

    Hi All,

    This is the first time posting here.

    I am looking to create a VBA code that would be able to create a new sheet based on a modular template/reference sheet.

    Essentially what I want it to do is to have a button I press that would create a new column on the input sheet and a new sheet that's based on the new input column and an existing template. This template sheet will have several formulas that will reference the first column on the input sheet and also serves as the first sheet. Each new sheet I want to generate will also need to reference the input sheet but will be shifted one cell to the right. So essentially, a button will be at the location of the last input column and when pressed, will create a new sheet, reference the new column created and the button will then shift over as well.

    The tricky part is this. I would like this file to be used in the future for different purposes. I would like to be able to change the template sheet (ie add new rows and formulas) and have the VBA be able to pick that up and shift references.

    I haven't dive too deeply into it yet but I am stuck on how to copy a formula I typed on the sheet into VBA and modifying it (shift cell reference to the right) and retype it back. I'm planning to just read and store all the formulas and shift everything that references the input sheet to the right. Just not how to do this. Another issue was I am not sure what the most efficient way to create a new sheet from a template would be.

    Here is an example I was thinking of.

    Input sheet:


    A B
    1 Cool Sheet 1 Bad Sheet 2
    2 1 34
    3 8 3422
    4 34 3411
    Click for New Sheet


    Template sheet:

    Sheet Title: Cool Sheet 1

    Cell 1: A2+A3

    Cell 2: A2*A4

    Cell 3: A3*A3



    Example new Sheet 1

    Sheet Title: Bad Sheet 2

    Cell 1: B2+B3

    Cell 2: B2*B4

    Cell 3: B3*B3


    When the New sheet button is pressed, a new column is generated and requires the user to input a new set of information. A new sheet is also created that will reference the new set of numbers

    A B C
    1 Cool Sheet 1 Bad Sheet 2 Meh Sheet
    2 1 34
    3 8 3422
    4 34 3411
    Click for New Sheet

    Example new Sheet 2

    Sheet Title: Meh Sheet

    Cell 1: C2+C3

    Cell 2: C2*C4

    Cell 3: C3*C3




    Hope someone can help me out.

    Thanks!

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA - Create a new sheet with shifting references

    Please upload a workbook or a representative cut down copy, anonymised if necessary. It is always easier to advise if we can see your request in its context.

    Show a before and after situation with manually calculated results, explaining which information is data and which is results, and if it's not blindingly obvious how you have arrived at your results some explanatory notes as well.

    To upload a file click the Go Advanced button at the foot of your post, look underneath the post area for the Manage Attachments section and take it from there.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: VBA - Create a new sheet with shifting references

    I'm unable to clearly understand what you want.

    Anyway, I guess something like this :
    There are sheet "Input" and sheet "Template" in the workbook originally.

    In Sheet "Input" column-A there is already a data where :
    cell A1 value is a sheet named "Template"
    cell A2 to cell A5 value is a number

    In sheet "Template" column-A there is already a formula, where :
    cell A1 formula is =Input!A2+Input!A3
    cell A2 formula is =Input!A2*Input!A4
    cell A3 formula is =Input!A3*Input!A3
    So here the formula in sheet "Template" is referencing to a cell in sheet "Input".

    What you want is a macro which ask the user to type/input:
    1. the name of the sheet which will be created (say the user type "blablabla")
    2. the 1st number
    3. the 2nd number
    4. the 3rd number
    5. the 4th number (addition)
    All of these five inputs will be placed by the macro in the last blank column (say it's column B) of sheet "Input".

    Then the macro do this :
    - create a new sheet with a name based on point-1 above ("blablabla")
    - copy whatever formula exist in sheet "Template" column A
    - paste the formula on cell A1 of sheet "blablabla"
    - replace the column reference in the formula of sheet "blablabla",
    from column A (as seen in sheet "Template") to whatever last non-empty column row-1 in sheet "Input" (column B in this example)

    So the formula in the newly created sheet "blablabla" will be something like this :
    cell A1 formula is =Input!B2+Input!B3
    cell A2 formula is =Input!B2*Input!B4
    cell A3 formula is =Input!B3*Input!B3

    Suppose that column B in sheet "Input" already has the data (and also there is already sheet "blablabla" in the workbook)
    and suppose that later on you change the formula in sheet "Template" into like this :
    cell A1 formula is =Input!A2-Input!A3
    cell A2 formula is =Input!A2+Input!A4
    cell A3 formula is =Input!A5/Input!A3
    cell A4 formula is =Input!A2*Input!A3
    cell A5 formula is =Input!A4*Input!A5

    Then you run the macro again with input :
    1. blublublu
    2. 100
    3. 200
    4. 300
    5. 400

    Then the result in sheet "blublublu"
    Cell A1 : -100
    Cell A2 : 400
    Cell A3 : 2
    Cell A4 : 20000
    Cell A5 : 120000

    where if you look at the formula in sheet "blublublu" column-A is like this :
    cell A1 formula is =Input!C2-Input!C3
    cell A2 formula is =Input!C2+Input!C4
    cell A3 formula is =Input!C5/Input!C3
    cell A4 formula is =Input!C2*Input!C3
    cell A5 formula is =Input!C4*Input!C5

    Please Login or Register  to view this content.
    Still not sure thought if this is what you mean.
    Last edited by karmapala; 08-31-2021 at 03:17 AM.

  4. #4
    Registered User
    Join Date
    08-30-2021
    Location
    Toronto, Canada
    MS-Off Ver
    365
    Posts
    3

    Re: VBA - Create a new sheet with shifting references

    Thanks Karmapala,

    This is pretty much what I wanted to do. The formulas will probably be quite a bit more complicated but I think that would still work. Reading the sample code, it looks like it takes the template file and allow me to type values with input box. then it generates a new sheet after the old sheet, and replaces all the references that say "!A" with the new reference column.

    I'm not too familiar with .Address but I'm assuming that it returns the column letter. If I change up my template and it only references column B, could I just replace the !A with !B and still have it working?

    I didn't expect the code to be so short. I guess in my head it seems much more complicated than it actually is.

    I'll give it a try tonight and see if it does what I think it does.

  5. #5
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: VBA - Create a new sheet with shifting references

    Quote Originally Posted by kytung View Post
    Thanks Karmapala,
    Reading the sample code, it looks like it takes the template file and allow me to type values with input box.
    then it generates a new sheet after the old sheet,
    and replaces all the references that say "!A" with the new reference column.
    YEs you are correct.
    In other words, sheet "Input" column A is just for the sheet "Template" reference.

    I'm not too familiar with .Address but I'm assuming that it returns the column letter.
    Correct.

    If I change up my template and it only references column B, could I just replace the !A with !B and still have it working?
    I haven't tried it yet. But logically, yes it should still run if the formula in sheet "Template" column A is referencing
    to column B in sheet "Input".

    So assumed that sheet "Template" formula reference is to column B (after row 1) of sheet "Input",
    the next new column will be column C of sheet "Input", where row 1 is the name of the new sheet,
    and the rest of the rows is the value the user input.

    Just remember that you need to change this line :
    Please Login or Register  to view this content.
    into
    Please Login or Register  to view this content.
    So, when you run the code, it will replace the !B to !C.
    If you don't change that line (it's still !A), the code won't work
    because it won't find "!A" in the newly_created_sheet_with_the_pasted_formula_from_sheet_template
    to be replaced with "!C".

    I didn't expect the code to be so short.
    I guess in my head it seems much more complicated than it actually is.
    I think as long as the formula in sheet "Template" is always referencing to whatever_column in sheet "Input",
    and the line
    Please Login or Register  to view this content.
    is referencing to What:="!whatever_column",
    then I think the code should still work no matter how complicated is the formula in sheet "Template".

    I'll give it a try tonight and see if it does what I think it does.
    Please do.
    I hope the code still can give you the result as you expected.

  6. #6
    Registered User
    Join Date
    08-30-2021
    Location
    Toronto, Canada
    MS-Off Ver
    365
    Posts
    3

    Re: VBA - Create a new sheet with shifting references

    I tried this out and tinkered with some of the codes and I think I got it working as I wanted it to. Thank you so much!

    I was also able to change the column references from !A to !B and it worked just as well.

    I do have one more additional follow-up question. Is there a way to also copy over the format while copying the formulas over?

+ 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. Insert new cells in a row without shifting formula references
    By 82penzop82 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-14-2018, 10:43 AM
  2. [SOLVED] Is there a way to guide the VBA code so that it may just keep shifting from main sheet ...
    By omega0010 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-30-2015, 11:38 AM
  3. Replies: 1
    Last Post: 07-16-2015, 10:07 AM
  4. Way to copy cells so that copying formula results in references as shifting horizontally
    By PivotTablePSHomage in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-04-2014, 08:03 PM
  5. how to create formulas with references to a invoice sheet
    By teange in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 10-04-2013, 10:28 PM
  6. How to create Read/Write VBA Userform that references variable cells/rows in a sheet?
    By Sivart9876 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-16-2012, 10:58 AM
  7. Shifting data to another sheet without blank cells
    By Mak2145 in forum Excel General
    Replies: 3
    Last Post: 06-18-2011, 02:54 AM

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