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
Solved

Insert first row in multiple excel files in given folder

Posted on 2013-01-25
2
362 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
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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

829 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