Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

939 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

6 Experts available now in Live!

Get 1:1 Help Now