+ Reply to Thread
Results 1 to 9 of 9

VBA code to convert all data + formatting of entire sheet to VBA code

  1. #1
    Registered User
    Join Date
    12-27-2013
    Location
    Gilbert, AZ
    MS-Off Ver
    Excel 2007
    Posts
    15

    VBA code to convert all data + formatting of entire sheet to VBA code

    Does anybody know a VBA macro that you can run on a given sheet within a workbook and it will convert all the data and formatting of that sheet into VBA code so that I can then copy and paste that code into another macro to then generate a the identical spreadsheet on a new blank sheet?

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: VBA code to convert all data + formatting of entire sheet to VBA code

    Change "Sheet1" to whatever sheet you want the formatting copied from. Remove the ClearContents line if you want to preserve the data on the new sheet.

    Please Login or Register  to view this content.
    Last edited by walruseggman; 01-14-2016 at 03:14 PM.
    I'm interested in starting a career working with VBA, if anyone knows of any opportunities!

  3. #3
    Registered User
    Join Date
    12-27-2013
    Location
    Gilbert, AZ
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: VBA code to convert all data + formatting of entire sheet to VBA code

    Thank you walruseggman. That code would definitely work but what I would like to do is instead of copying the sheet, actually generate the code to write the entire sheet from scratch because I do not want to have to open another workbook to copy the sheet and the sheet I need is not in the workbook that will be open at the time. So in other words, I would like to be able to create a certain template sheet from vba code independent of anything else. For instance, if the only thing I had on the template was the word "HI" in cell A1 and "BYE" in cell A2, then what I want to be able to do is run a routine that would generate this:

    Please Login or Register  to view this content.
    I would then be able to take this code and copy and paste it into another macro so that whenever I want I could create a new sheet with those exact contents from scratch, not by having to copy another sheet. Of course my actual template is much more complex than that, or I would just write the code out myself. It's probably wishful thinking, but it would be cool if someone knew a way, or had a vba code to do this.

    Said another way, what I would like is to be able to translate all the contents, including formatting, data, formulas, links, everything exactly as it is in the existing sheet of an entire existing Excel sheet into VBA code that when executed would write to a new page all those contents and formatting, formulas, and data... without the need of copying, but just independently all on its own from scratch.
    Last edited by jocanon; 01-14-2016 at 03:34 PM.

  4. #4
    Registered User
    Join Date
    12-27-2013
    Location
    Gilbert, AZ
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: VBA code to convert all data + formatting of entire sheet to VBA code

    This isn't the reason for me wanting to do this, but I was just thinking, it would be an easy way to email very large files because then you could send it over as a text file and the recipient could execute the macro to recreate your workbook. Anyway, that is not what I want this for, but that is the idea.

  5. #5
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: VBA code to convert all data + formatting of entire sheet to VBA code

    what I would like is to be able to translate all the contents, including formatting, data, formulas, links, everything exactly as it is in the existing sheet
    What you describe there is literally .Copy. If you're insistent on not using .Copy, any other method would just mimic what .Copy does, so therefore it's not worth writing. The functionality already exists.

    Recreating .Copy would be incredibly long and enormously tedious to write. If you're not using copy, there are hundreds of formatting variables you'd have to account for, and do so for each range that has different formatting. That is, unless you had a very specific list of just the formatting variables you want to use. But even then, it's a lot of work compared to two lines of code.

    I wish you luck on getting the answer you want, but I don't think you're going to find it.

  6. #6
    Registered User
    Join Date
    12-27-2013
    Location
    Gilbert, AZ
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: VBA code to convert all data + formatting of entire sheet to VBA code

    Quote Originally Posted by walruseggman View Post
    What you describe there is literally .Copy.
    I would say it is getting inside what .Copy actually creates so that I can then preserve that code and use it stand alone without the need of going back and copying again. It's not that I refuse to use .Copy, it is just that, if possible, I would like a way to generate a template in a new Excel workbook without the need of opening and copying it from an existing workbook. Does this make sense? I mean, I am OK to accept that this is not practical to do, but do you at least understand what it is I am trying to do?

  7. #7
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: VBA code to convert all data + formatting of entire sheet to VBA code

    Yes, I understand. Yes, it is impractical. Like I mentioned, there are hundreds of formatting variables you would need to capture for every range that has a different format.

    Anyway, just a few things to note:
    • You can have the code open up a workbook and copy a sheet, this would work well on a network drive if multiple people need to access it. the user need not be involved until a new template sheet is automatically created via .Copy.
    • In your hypothetical email situation, you could just email the template sheet, there's no need to re-create it from scratch. I know you said that wasn't you exact application, but still. I think that would apply to any scenario.
    • I understand you want to "see the code" behind what .Copy does. But again, I have doubts that anyone would willingly try to recreate something so massive and unnecessary.


    Again, I wish you luck. I you do decide to use a .Copy approach, I would be happy to help.

  8. #8
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    419

    Re: VBA code to convert all data + formatting of entire sheet to VBA code

    if possible, I would like a way to generate a template
    You can save spreadsheets as templates (I'm not an expert on this) - that may do what you want.

    As far as doing what you have asked, I know where to begin, but not where to end! To literally replicate .copy would probably involve dozens (maybe hundreds!) of parameters that hardly anyone has heard of or even used. If the formatting is fairly simple then it shouldn't be too hard to come up with something, but if there is anything like a cell that has more than one font size or colour in it, then things start getting messy.

    Anyway - if you really want to try it, this might get you started. The code below writes the address, value, formula, and a few other things for every cell to a .txt file.
    Please Login or Register  to view this content.
    And this reads from the file and applies it to the spreadsheet.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    12-27-2013
    Location
    Gilbert, AZ
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: VBA code to convert all data + formatting of entire sheet to VBA code

    mgs73, awesome, that is exactly what I was looking for. Thank you!

+ 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] Copying entire sheet including code and name references
    By _bells in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-02-2015, 01:51 AM
  2. VBA Code to Copy entire sheet and paste in another sheet
    By molly13 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-28-2015, 11:45 AM
  3. AVB Code to copy entire row to another work sheet
    By sara101 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-02-2014, 11:52 AM
  4. [SOLVED] VBA code to copy and paste an entire row from one sheet to another
    By emach in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 09-24-2013, 12:12 PM
  5. [SOLVED] This VB code to hide a row is only working for one sheet, and not the entire workbook?
    By ibabs in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 08-07-2013, 04:28 PM
  6. VBA code to Copy entire row to new sheet multiple times
    By wazimu13 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 05-12-2013, 09:10 AM
  7. Useful code that deletes entire rows based on formatting
    By Ralfie in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-20-2007, 01:02 PM

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