?
Solved

Insert first row in multiple excel files in given folder

Posted on 2013-01-25
2
Medium Priority
?
365 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 2000 total points
ID: 38817988
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
ID: 38818695
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

777 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