+ Reply to Thread
Results 1 to 11 of 11

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

Hybrid View

moosetales Add column headers to newly... 01-16-2016, 10:44 AM
JBeaucaire Re: Add column headers to... 01-16-2016, 12:00 PM
moosetales Re: Add column headers to... 01-16-2016, 02:01 PM
JBeaucaire Re: Add column headers to... 01-17-2016, 04:24 AM
moosetales Re: Add column headers to... 01-17-2016, 11:15 AM
JBeaucaire Re: Add column headers to... 01-17-2016, 02:13 PM
moosetales Re: Add column headers to... 01-17-2016, 02:46 PM
moosetales Re: Add column headers to... 01-17-2016, 02:23 PM
JBeaucaire Re: Add column headers to... 01-17-2016, 02:30 PM
JBeaucaire Re: Add column headers to... 01-18-2016, 03:18 AM
moosetales That makes sense. Thanks for... 01-18-2016, 07:21 AM
  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:

    Option Explicit
    
    Sub Sample()
    
    'http://stackoverflow.com/questions/17343275/setting-column-headers-when-adding-columns-to-a-table-via-macro-in-excel
    
        Dim wbI As Workbook, wbO As Workbook
        Dim wsI As Worksheet, wsO As Worksheet
    
        '~~> Source/Input Workbook
        Set wbI = ThisWorkbook
        '~~> Set the relevant sheet from where you want to copy
        Set wsI = wbI.Sheets("Mileage Approvals")
    
        '~~> Destination/Output Workbook
        Set wbO = Workbooks.Add
    
        With wbO
            '~~> Set the relevant sheet to where you want to paste
            Set wsO = wbO.Sheets("Sheet1")
    
            
            '~~> Copy the range
            wsI.Range("A1:B400").Copy
    
            '~~> Paste it in say Cell A1. Change as applicable
            wsO.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        End With
    End Sub
    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)
    wsO.Range("C1:D1").Value = [{"mileage on time card","overage"}]

    2. automatically adjust the column width to fit the content
    wsO.Columns.Autofit

    3. add the following function to column D =SUM(B2-C2) and only show the value if it's a negative number
    Dim LR as Long
    
    LR = wsO.Range("A" & Rows.Count).End(xlUp).Row
    wsO.Range("C2:C" & LR).Formula = "=IF(B2-C2<0, B2-C2, """")"
    _________________
    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.

    Sub Sample()
        Dim wbI As Workbook, wbO As Workbook
        Dim wsI As Worksheet, wsO As Worksheet
    
        '~~> Source/Input Workbook
        Set wbI = ThisWorkbook
        '~~> Set the relevant sheet from where you want to copy
        Set wsI = wbI.Sheets("Mileage Approvals")
    
        '~~> Destination/Output Workbook
        Set wbO = Workbooks.Add
    
        With wbO
            '~~> Set the relevant sheet to where you want to paste
            Set wsO = wbO.Sheets("Sheet1")
    
            '~~> Copy the range
            wsI.Range("A1:B400").Copy
    
            '~~> Paste it in say Cell A1. Change as applicable
                   
            wsO.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
            
            wsO.Range("C1:D1").Value = [{"Mileage on Time Card","Overage"}]
            
            wsO.Columns.AutoFit
            
            Dim LR As Long
    
            LR = wsO.Range("A" & Rows.Count).End(xlUp).Row
            wsO.Range("C2:C" & LR).Formula = "=IF(B2-C2<0, B2-C2,"")"
                  
            
        End With
    End Sub
    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.

    Option Explicit
    
    Sub Sample()
        Dim wbI As Workbook, wbO As Workbook
        Dim wsI As Worksheet, wsO As Worksheet
    
        '~~> Source/Input Workbook
        Set wbI = ThisWorkbook
        '~~> Set the relevant sheet from where you want to copy
        Set wsI = wbI.Sheets("Mileage Approvals")
    
        '~~> Destination/Output Workbook
        Set wbO = Workbooks.Add
    
        With wbO
            '~~> Set the relevant sheet to where you want to paste
            Set wsO = wbO.Sheets("Sheet1")
    
            '~~> Copy the range
            wsI.Range("A1:G400").Copy
            
    
            '~~> Paste it in say Cell A1. Change as applicable
                   
            wsO.Range("A1:G300").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
            
            wsO.Range("D1:G1").Value = [{"Mileage on Time Card","Overage", "Employee", "Week Ending"}]
               
            '~~> Bold top row
               
            Rows("1:1").Select
            Selection.Font.Bold = True
            
            '~~> Fit content to column and center text in columns B, C, D
            
            wsO.Columns("B:B").HorizontalAlignment = xlCenter
                wsO.Columns("C:C").HorizontalAlignment = xlCenter
                    wsO.Columns("D:D").HorizontalAlignment = xlCenter
                        wsO.Columns("E:E").HorizontalAlignment = xlCenter
                            wsO.Columns("F:F").HorizontalAlignment = xlCenter
            
            '~~> Sets different column widths for columns A, B, C, D
            
             Columns("A").AutoFit
                Columns("B").ColumnWidth = 10
                    Columns("C").ColumnWidth = 10
                        Columns("D").ColumnWidth = 10
                            Columns("E").ColumnWidth = 10
                                Columns("F").ColumnWidth = 25
            
            Dim LR As Long
    
    
            LR = wsO.Range("A" & Rows.Count).End(xlUp).Row
            wsO.Range("D2:E400" & LR).NumberFormat = "0"
            
            LR = wsO.Range("A" & Rows.Count).End(xlUp).Row
            wsO.Range("E2:E" & LR).Formula = "=IF(C2-D2<0, C2-D2,"""")"
                  
            LR = wsO.Range("A" & Rows.Count).End(xlUp).Row
            wsO.Range("G2:G400" & LR).NumberFormat = "mm/dd/yy"
                  
            LR = wsO.Range("A" & Rows.Count).End(xlUp).Row
            wsO.Range("G2:G" & LR).Formula = "=TODAY()-WEEKDAY(TODAY(),3)+IF(WEEKDAY(TODAY(),3)>4,11,4"
                
            
            
            
                   
        End With
    End Sub
    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:
    Option Explicit
    
    Sub Sample()
    Dim wbI As Workbook, wbO As Workbook
    Dim wsI As Worksheet, wsO As Worksheet
    Dim LR As Long
    
    Set wbI = ThisWorkbook                          'Source/Input Workbook
    Set wsI = wbI.Sheets("Mileage Approvals")       'Set the relevant sheet from where you want to copy
    Set wbO = Workbooks.Add                         'Destination/Output Workbook
    Set wsO = wbO.Sheets("Sheet1")                  'Set the relevant sheet to where you want to paste
    
    With wsO
        wsI.Range("A1").CurrentRegion.Copy .Range("A1")      'Paste it in say Cell A1. Change as applicable
        .Range("D:G").ClearContents
        
        .Range("D1:G1").Value = [{"Mileage on Time Card","Overage", "Employee", "Week Ending"}]
        .Rows("1:1").Font.Bold = True                   'Bold top row
        .Range("B:G").HorizontalAlignment = xlCenter    'formatting
        .Columns("A").AutoFit
        .Columns("B:E").ColumnWidth = 10
        .Columns("F").ColumnWidth = 25
        .Columns("G").ColumnWidth = 15
        
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        .Range("D2:E" & LR).NumberFormat = "0"
        .Range("E2:E" & LR).Formula = "=IF(C2-D2<0, C2-D2,"""")"
        .Range("G2:G" & LR).NumberFormat = "mm/dd/yy"
        .Range("G2:G" & LR).Formula = "=TODAY()-WEEKDAY(TODAY(),3)+IF(WEEKDAY(TODAY(),3)>4,11,4)"
    End With
    
    End Sub

  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

    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.

    .Columns("A:G").AutoFit
        '.Columns("B:E").ColumnWidth = 15
        '.Columns("F").ColumnWidth = 20
        '.Columns("G").ColumnWidth = 15

  8. #8
    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

  9. #9
    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.

  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