+ Reply to Thread
Results 1 to 11 of 11

Add column headers to newly created worksheet and add formatting to all columns

  1. #1
    Forum Contributor
    Join Date
    02-22-2014
    Location
    Winthrop, ME
    MS-Off Ver
    Excel 2010
    Posts
    420

    Add column headers to newly created worksheet and add formatting to all columns

    Good morning. I have a master workbook that contains two columns with headers; client names (column A) and the number of approved miles (column B). I need to copy those two columns and paste them automatically in a new excel workbook. The code I currently have in my master workbook is as follows:

    Please Login or Register  to view this content.
    The code works great for copying from my master and pasting into a newly created workbook. Now I can't figure out how to do the following in the newly created workbook:
    1. add two new column headers in column C (mileage on time card) & D (overage)
    2. automatically adjust the column width to fit the content
    3. add the following function to column D =SUM(B2-C2) and only show the value if it's a negative number

    Here is a sample of the workbook I'm creating. Thanks in advance for feedback.

    Mileage Approval Dummy Worksheet.xlsm

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Add column headers to newly created worksheet and add formatting to all columns

    1. add two new column headers in column C (mileage on time card) & D (overage)
    Please Login or Register  to view this content.

    2. automatically adjust the column width to fit the content
    Please Login or Register  to view this content.

    3. add the following function to column D =SUM(B2-C2) and only show the value if it's a negative number
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    02-22-2014
    Location
    Winthrop, ME
    MS-Off Ver
    Excel 2010
    Posts
    420

    Re: Add column headers to newly created worksheet and add formatting to all columns

    JBeaucaire ,

    Brilliant. Here's how I put the additional code into the existing code.

    Please Login or Register  to view this content.
    It does everything I need with the exception of the formula. When I enter a value in column C no value populates in column D. Here's the workbook with the above code. Thanks.

    Mileage Approval Dummy Worksheet.xlsm

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Add column headers to newly created worksheet and add formatting to all columns

    So maybe that code should be putting that formula in column D instead of column C.

  5. #5
    Forum Contributor
    Join Date
    02-22-2014
    Location
    Winthrop, ME
    MS-Off Ver
    Excel 2010
    Posts
    420

    Re: Add column headers to newly created worksheet and add formatting to all columns

    JBeaucaire,

    Yes, of course. Changed code to reflect the desired column and it worked like a charm. The following is the code as it stands currently.

    Please Login or Register  to view this content.
    I'm still coming up short on how to adjust my code to adjust what is copied and pasted from the original workbook to the newly created workbook. This is what I'm trying to accomplish:

    From the Original Workbook to the New Workbook


    Column A-Copy and Paste ALL
    Column B-Copy and Paste ALL
    Column C-Copy and Paste ALL
    Column D-Copy borders & cell background color only and Paste borders & cell background color only
    Column E-Copy borders & cell background color only and Paste borders & cell background color only
    Column F-Copy borders & cell background color only and Paste borders & cell background color only
    Column G-Copy borders & cell background color only and Paste borders & cell background color only

    Thanks again for any feedback you may be able/willing to offer.

    Here's my Dummy workbook with the above code at work. Thanks.

    Mileage Approval Spreadsheet DUMMY.xlsm


    Matthew
    Last edited by moosetales; 01-17-2016 at 01:32 PM.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Add column headers to newly created worksheet and add formatting to all columns

    This is how I would do that with your code:
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    02-22-2014
    Location
    Winthrop, ME
    MS-Off Ver
    Excel 2010
    Posts
    420

    Re: Add column headers to newly created worksheet and add formatting to all columns

    Thank you kindly. With your suggested code the first run worked perfectly. I'm going to run it through its paces and report back in a bit. Thanks again.

    Matthew

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Add column headers to newly created worksheet and add formatting to all columns

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  9. #9
    Forum Contributor
    Join Date
    02-22-2014
    Location
    Winthrop, ME
    MS-Off Ver
    Excel 2010
    Posts
    420

    Re: Add column headers to newly created worksheet and add formatting to all columns

    JBeaucaire,

    After trialing the workbook with new code I decided to make columns A:G AutoFit (see code below). Everything copies over to the new workbook perfectly but when I type in the columns of the new workbook the columns do not re-size to fit the newly entered text. Did I adjust the code incorrectly? Thanks.

    Please Login or Register  to view this content.

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Add column headers to newly created worksheet and add formatting to all columns

    Typing in the columns after the macro has run does not retrigger the autofit, it only autofits based on the content of the columns at the time the macro runs.

  11. #11
    Forum Contributor
    Join Date
    02-22-2014
    Location
    Winthrop, ME
    MS-Off Ver
    Excel 2010
    Posts
    420
    That makes sense. Thanks for all your help.

    Matthew

+ 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. Copy columns with specified names to a newly created Sheet.
    By qwers in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-07-2015, 02:10 PM
  2. macro to add conditional formatting to a newly created sheet
    By Shellybelly in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-27-2015, 09:09 AM
  3. macro code to activate the newly created worksheet in excel.
    By Shellybelly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-15-2015, 05:55 PM
  4. Linking newly created worksheet names to cells?
    By qstillso in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-23-2015, 07:09 AM
  5. [SOLVED] Use Current Worksheet Name in Formula to Rename Newly Created Worksheet
    By MICowboy13 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-12-2015, 04:26 PM
  6. Updating data on newly created worksheet
    By wolverine82 in forum Excel General
    Replies: 6
    Last Post: 04-14-2010, 11:17 AM
  7. Referencing a newly created worksheet
    By Charyn in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-02-2005, 12:07 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