Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Merging files

I have a lot of .csv format files in a folder. Columns in each file are same
but not the rows.

I have another .xls file in the same folder with similar columns. I am
looking for a VBA which can copy all records from cm1FEB2005bhav.csv file
into my master.xls file in sheet1, then copy all records from
cm2FEB2005bhav.csv file into my master.xls file in sheet1 just below the
existing data and continue this till cm4MAR2005bhav.csv

If it is a saturday or sunday, the .csv files will not be present.

For example:
Files
cm5FEB2005bhav.csv
cm6FEB2005bhav.csv
will not be present as 5Feb and 6 Feb was saturday and sunday.

Maxi
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Merging files

How about something like:

Option Explicit
Sub testme()

Dim myFolder As String
Dim dCtr As Long
Dim testStr As String
Dim myFileName As String
Dim DestCell As Range
Dim TmpWkbk As Workbook
Dim MstrWks As Worksheet

myFolder = "C:\my documents\excel\test\"
If Right(myFolder, 1) < "\" Then
myFolder = myFolder & "\"
End If

Set MstrWks = workbooks("master.xls").Worksheets("sheet1")

With MstrWks
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With

For dCtr = DateSerial(2005, 2, 2) To DateSerial(2005, 3, 4)
myFileName = myFolder & "cm" & Format(dCtr, "dmmmyyyy") & "bhav.csv"
testStr = ""
On Error Resume Next
testStr = Dir(myFileName)
On Error GoTo 0
If testStr = "" Then
'not found
'comment this line when you're happy
Debug.Print myFileName & " wasn't found"
Else
Set TmpWkbk = Workbooks.Open(Filename:=myFileName)
TmpWkbk.Worksheets(1).UsedRange.Copy _
Destination:=DestCell
TmpWkbk.Close savechanges:=False
With DestCell.Parent
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With
End If
Next dCtr

End Sub

This assumes I can use column A to find the next available row.

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

mac_see wrote:

I have a lot of .csv format files in a folder. Columns in each file are same
but not the rows.

I have another .xls file in the same folder with similar columns. I am
looking for a VBA which can copy all records from cm1FEB2005bhav.csv file
into my master.xls file in sheet1, then copy all records from
cm2FEB2005bhav.csv file into my master.xls file in sheet1 just below the
existing data and continue this till cm4MAR2005bhav.csv

If it is a saturday or sunday, the .csv files will not be present.

For example:
Files
cm5FEB2005bhav.csv
cm6FEB2005bhav.csv
will not be present as 5Feb and 6 Feb was saturday and sunday.

Maxi


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Merging files

Terrific. That worked !!!!

Thanx a million

"Dave Peterson" wrote:

How about something like:

Option Explicit
Sub testme()

Dim myFolder As String
Dim dCtr As Long
Dim testStr As String
Dim myFileName As String
Dim DestCell As Range
Dim TmpWkbk As Workbook
Dim MstrWks As Worksheet

myFolder = "C:\my documents\excel\test\"
If Right(myFolder, 1) < "\" Then
myFolder = myFolder & "\"
End If

Set MstrWks = workbooks("master.xls").Worksheets("sheet1")

With MstrWks
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With

For dCtr = DateSerial(2005, 2, 2) To DateSerial(2005, 3, 4)
myFileName = myFolder & "cm" & Format(dCtr, "dmmmyyyy") & "bhav.csv"
testStr = ""
On Error Resume Next
testStr = Dir(myFileName)
On Error GoTo 0
If testStr = "" Then
'not found
'comment this line when you're happy
Debug.Print myFileName & " wasn't found"
Else
Set TmpWkbk = Workbooks.Open(Filename:=myFileName)
TmpWkbk.Worksheets(1).UsedRange.Copy _
Destination:=DestCell
TmpWkbk.Close savechanges:=False
With DestCell.Parent
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With
End If
Next dCtr

End Sub

This assumes I can use column A to find the next available row.

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

mac_see wrote:

I have a lot of .csv format files in a folder. Columns in each file are same
but not the rows.

I have another .xls file in the same folder with similar columns. I am
looking for a VBA which can copy all records from cm1FEB2005bhav.csv file
into my master.xls file in sheet1, then copy all records from
cm2FEB2005bhav.csv file into my master.xls file in sheet1 just below the
existing data and continue this till cm4MAR2005bhav.csv

If it is a saturday or sunday, the .csv files will not be present.

For example:
Files
cm5FEB2005bhav.csv
cm6FEB2005bhav.csv
will not be present as 5Feb and 6 Feb was saturday and sunday.

Maxi


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Merging 2 files Distribution list - Contacts Module Excel Discussion (Misc queries) 1 October 25th 08 02:48 AM
merging files Maaz New Users to Excel 3 March 1st 07 04:24 PM
merging zip files! via135 Excel Discussion (Misc queries) 2 November 19th 06 05:32 PM
merging two files mcap Excel Discussion (Misc queries) 3 April 9th 06 07:28 PM
Merging two different files justinfisher Excel Discussion (Misc queries) 0 January 21st 05 08:53 PM


All times are GMT +1. The time now is 09:32 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"