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:
- add two new column headers in column C (mileage on time card) & D (overage)
- automatically adjust the column width to fit the content
- 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
Bookmarks