Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-04-12
4
Medium Priority
?
185 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 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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 demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

972 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