Solved

Insert a new worksheet named 'listings' in all xlsx-files of a given folder

Posted on 2012-04-12
4
181 Views
Last Modified: 2012-04-13
Dear Experts:

I would like to run a macro that accomplishes the following task:

Insert a worksheet named 'listings' in all files of a folder

The macro is to run from a xlsm-file that always resides in the folder where all the xlsx-files have to be worked on.

That is, the macro should work on any folder, i.e. - I guess - the code snippet 'ActiveWorkbook.path' should be part of the code.

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
  • 2
4 Comments
 
LVL 7

Expert Comment

by:hippohood
ID: 37841404
Hi,
the code below should do the job. Please, let me know if you have any issues with it

Sub InsertSheetInEveryFileInFolder()
Dim folderspec$

folderspec = Left(ThisWorkbook.FullName, Len(ThisWorkbook.FullName) - Len(ThisWorkbook.Name))

Dim fs, f, fc, f1, wb As Workbook, ws
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
For Each f1 In fc
    If UCase(Right(f1, 4)) = "XLSX" Then
        Set wb = Workbooks.Open(f1.FullName)
        Set ws = wb.Worksheets.Add
        ws.Name = "listings"
        wb.Close True
    End If
Next

End Sub

Open in new window

0
 

Author Comment

by:AndreasHermle
ID: 37841594
Hi hippohood,

thank you very much for your swift support.

I am afraid to tell you that your code throws an error message on Line 12. Error Message is 438: Object does not support this property or method

Line 6 says 'ws' at the end. Shouldn't 'ws' be declared as well?

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

Regards, Andreas
0
 
LVL 7

Accepted Solution

by:
hippohood earned 500 total points
ID: 37841794
Sorry, Andreas. This would depend on your library settings. To simplify things you can just remove ".FullName" on line 12.

BTW, this code requires that all the XLSX files are not open, not read-only and don't have a sheet named "listings" already. You can add on error resume next statement to avoid hlating the macro if either of those conditions are not true.

See below a corrected code
Sub InsertSheetInEveryFileInFolder()
Dim folderspec$

folderspec = Left(ThisWorkbook.FullName, Len(ThisWorkbook.FullName) - Len(ThisWorkbook.Name))

Dim fs, f, fc, f1, wb As Workbook, ws
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files

On Error Resume Next
For Each f1 In fc
    If UCase(Right(f1, 4)) = "XLSX" Then
        Set wb = Workbooks.Open(f1)
        Set ws = wb.Worksheets.Add
        ws.Name = "listings"
        wb.Close True
    End If
Next

End Sub
 

Open in new window

0
 

Author Closing Comment

by:AndreasHermle
ID: 37843200
Great, this did the trick.

Thank you very much for your professional and swift help.

Regards, Andreas
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

732 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