Thread: Combining files
View Single Post

  #2 (permalink)  
Old 11-19-2004, 09:18 PM
sanereddy sanereddy is offline
Senior Member
 
Join Date: Nov 2004
Posts: 177
sanereddy is an unknown quantity at this point
Re:Combining files

Here it is, might need bit tweaking, I haven\'t tested the code on my pc. it\'s simple vbscript code.

put all the files that has to be merged in c:\\temp directory.
open a new workbook hit alt+f11 this opens vbe window select modules form insert menu, paste the code below, close the vbe window. in xl hit alt+f8 select scopy click run

Sub shcopy()
Dim xlBook As Excel.Workbook
Dim tBook As Excel.Workbook
Dim xlSht As Excel.Worksheet
Set tBook = ThisWorkbook
fName = Dir(\"c:\\temp\\*.xls\&quot
aa = 1
Do While (fName <> \"\&quot
Set xlBook = Workbooks.Open(\"c:\\temp\\\" & fName)
Set xlSht = xlBook.Sheets(1)
xlSht.UsedRange.Copy
tBook.Activate
Cells(aa, 1).Select
ActiveSheet.Paste
ActiveSheet.UsedRange.Select
aa = Selection.Rows.Count + 2
xlBook.Save
xlBook.Close
fName = Dir
Loop
Set xlBook = Nothing
Set xlSht = Nothing
Set tBook = Nothing
End Sub

if you would like to copy many tabs from into one spreadhseet within one excel file...
Sub CombineFilesInFolder()
Dim pathname As String
Dim filename As String
Dim rng As Range
dim tmprng as Range

Dim thiswrk As Workbook
Dim thissht As Worksheet

Dim wrk As Workbook
Dim sht As Worksheet

Set thiswrk = ThisWorkbook
Set thissht = thiswrk.Sheets(1)

Application.ScreenUpdating = False
pathname = \"\\\\Globasrvr\\globafone\\Billing\\OrangeBillingTools\\Orange_Excel_Sheets_From_Dean\\\"
filename = Dir(pathname & Application.PathSeparator & \"*.xls\&quot
Do Until filename = \"\"
Set wrk = Workbooks.Open(pathname & Application.PathSeparator & filename)
Set sht = wrk.Sheets(1)
Set tmprng = sht.UsedRange
Set rng = sht.UsedRange
rng.Copy thissht.Cells(thissht.UsedRange.Rows.Count + 1, 1)
wrk.Close False
filename = Dir()
Loop
Set sht = Nothing
Set wrk = Nothing

Application.ScreenUpdating = True
End Sub


Hope this helps.
Reply With Quote