Solved

Adding names to worksheets during merging

Posted on 2013-10-31
4
296 Views
Last Modified: 2013-10-31
Hi guys

I have an output in the form of multiple worksheets.
I have found a solution to merge them all toghether into one workbook. But I would like the sheets to be named after the original file name.

Lets say my file is called 9252400.xlsx, i would then like the corresponding sheet in the workbook to be called 9252400

The code I am using for merging is:

Sub GetSheets()
Path = "C:\Documents and Settings\DAJO\My Documents\MD-CPH\PI_Database\0908315-3\PlantInfo_Excel\"
Filename = Dir(Path & "*.xls")
  Do While Filename <> ""
  Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
     For Each Sheet In ActiveWorkbook.Sheets
     Sheet.Copy after:=ThisWorkbook.Sheets(1)
  Next Sheet
     Workbooks(Filename).Close
     Filename = Dir()
  Loop
End Sub

Open in new window


Do one of you have a good idea on how to do that?

Br/Danny
0
Comment
Question by:dannyjoer
  • 2
  • 2
4 Comments
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 39613955
Hi,

pls try the nwe sheet is always the 2nd

Sub GetSheets()
Path = "C:\Documents and Settings\DAJO\My Documents\MD-CPH\PI_Database\0908315-3\PlantInfo_Excel\"
Filename = Dir(Path & "*.xls")
  Do While Filename <> ""
  Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
     For Each Sheet In ActiveWorkbook.Sheets
        Sheet.Copy after:=ThisWorkbook.Sheets(1)
        ThisWorkbook.Sheets(2).Name = Left(Filename, Len(Filename) - 4)
     Next Sheet
     Workbooks(Filename).Close
     Filename = Dir()
  Loop
End Sub

Open in new window

Regards
0
 

Author Comment

by:dannyjoer
ID: 39614076
It actually works alright, I just think I haven't expressed my self good enough :) All the data is contained in the first sheet in all the different workbooks I want to merge.

So I wanna copy the first sheet from each workbook in my directory, name it the filename, and the finally merge it into one workbook.

Can you help with that? :)

Br/Danny
0
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39614144
If you only want the first sheet of each file and create a new file

pls try

Sub GetSheets()
strPath = "C:\Documents and Settings\DAJO\My Documents\MD-CPH\PI_Database\0908315-3\PlantInfo_Excel\"
Filename = Dir(strPath & "*.xls")
Set wbkDest = Workbooks.Add
  Do While Filename <> ""
     Set wbkOrig = Workbooks.Open(Filename:=strPath & Filename, ReadOnly:=True)
     wbkOrig.Sheets(1).Copy before:=wbkDest.Sheets(1)
     wbkDest.Sheets(1).Name = Left(Filename, Len(Filename) - 4)
     Workbooks(Filename).Close
     Filename = Dir()
  Loop
  
  wbkDest.SaveAs strPath & "Result.xls"
End Sub

Open in new window

Regards
0
 

Author Closing Comment

by:dannyjoer
ID: 39614180
Perfect Solution.. Simple and effective..
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
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.

743 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

11 Experts available now in Live!

Get 1:1 Help Now