Solved

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

Posted on 2012-04-12
4
180 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
  • 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

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

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…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

820 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