Solved

Insert first row in multiple excel files in given folder

Posted on 2013-01-25
2
359 Views
Last Modified: 2013-01-25
Dear Experts:

I would like to run a macro that performs the following action:

I got hundreds of xlsm-files in C:\temp\

- I would like to insert a row at the very top of each workbook (each workbook contains just one worksheet named 'temp').

- This newly created first row has to have the following entries for A1, B1, C1, D1:
Item Number, Customer, Street, Items.

- The first row should be formatted to repeat with each page break (rows to repeat at top)

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
0
Comment
Question by:AndreasHermle
2 Comments
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
Comment Utility
Try

Sub insertheaders()
    Dim pth As String, fnam As String, wb As Workbook
    pth = "c:\temp\"
    fnam = Dir(pth & "*.xls?")
    Do While fnam <> ""
        Set wb = Workbooks.Open(pth & fnam)
        wb.ActiveSheet.Range("A1").EntireRow.Insert
        wb.ActiveSheet.Range("A1:D1") = Split("Item Number, Customer, Street, Items", ",")
        wb.Close True
        fnam = Dir
    Loop
End Sub
0
 

Author Comment

by:AndreasHermle
Comment Utility
Dear ssagibh:

thank you very much for your great help. Works great although I needed to make a slight change on the variable declaration:

Dim wb As Variant

And I added
wb.ActiveSheet.PageSetup.PrintTitleRows = "$1:$1" for the header to be repeated on page breaks.

Anyhow, thank you very much for your great and swift support. I really appreciate it.

Regards, Andreas
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now