Navigation
Recent Posts
Archive
The following macro will insert a footer giving the page numbers, file loaction and name, and the date and time on the active sheet.
Sub myfoot()
'Dim fullpath As String
'Dim sLeft As String
'Dim sRight As String
fullpath = ActiveWorkbook.FullName
Const sPAGE As String = "&P"
Const sPAGES As String = "&N"
Const sDATE As String = "&D"
Const sTIME As String = "&T"
sLeft = "Page " & sPAGE & " of " & sPAGES
sRight = sDATE & " " & sTIME
ActiveSheet.PageSetup.CenterFooter = fullpath
ActiveSheet.PageSetup.LeftFooter = sLeft
ActiveSheet.PageSetup.RightFooter = sRight
End Sub
The following macro copies an Excel sheet to a custom template. All you need do is set the template path to your own or a network drive. This is really only a snippet to demonstrate the concept but is a complete macro nonetheless. I've used it within a more complex process to generate invoices from a data file, then copy each invoice to a separate file to email out as an attachment. This was a temporary solution during the lead up to a billing system change for a Telecoms client.
I've used this to get around not being able to copy headers and footers in Excel and to avoid copying common drawing objects such as a logo and text boxes. By having these set in the template I didn't need to copy them each time the process looped.
Sub copy_ActiveSheet()
Dim wb As Workbook
Dim source As Range
Dim dest As Workbook
'Enter the path to your template file
Const TemplatePath As String = "C:mark.xlt"
Set source = Nothing
On Error Resume Next
Set source = Cells.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
Application.ScreenUpdating = False
Set dest = Workbooks.Add(TemplatePath)
source.Copy
With dest.Sheets(1)
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
End With
Tags:Excel |Macro |Drawing objects |headers |footers
I was looking for a quick way to merge the data from multiple workbooks into one worksheet in a new excel workbook.
The process of manually going through 40+ files and manually extracting the data wasn't going to be an effective use of time.
Although I used code before to do it I couldn't lay my hands on it, which hard drive, which file etc. so I had to start again but I needed a fast solution so fired up google.
One of the first links I clicked was for DigDB, an excel add-in which actually could do the job and a lot more besides but I didn't want to pay the licence. I bookmarked the site to return to later.
The solution I found was on Ron de Bruin's Excel Tips page which contains a lot of VBA code examples which you can use for free. The code there solved the problem exactly. It copies all the ranges you specify in the source workbooks and copies them into one master worksheet.
The precise code is reproduced below with some minor additional comments. To use it open an Excel workbook. Click on tools, Macro and Visual Basic Editor. In the VB editor select View and Code. Copy the code below into that page and click save. Save the file as basebook.xls on your C drive.
In the VB editor change the ranges you want to copy as required.
To use it the source files need to saved into a folder named source_data on the C drive, i.e. C:/source_data
From within basebook.xls select tools and macro. Select the macro to run.
Tags:Excel |Macro |VBA |Spreadsheeets
© Eriginal Ltd 2011, all rights reserved