#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default merge

Hello,

I have a number of excel workbooks created from an export from our
accounting programme.

These contine all our invoice data for upload to our finace company
who then advance the invoice amounts direclty to us. This gives us
imeditate payment which is good for our cash flow.

What I would like to do is to merge all these workbooks (average 1 to
2 workbooks a week for the last 12 months) and then x-ref them back
against out accounts programe.

The 1st stage I need help with is to merge the last 12 months of work
books back into one workbook.

Does this make sense ?

Thanks

Rob
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default merge

copy this to new module and try

Option Explicit

'The example macro below you can use to merge a fixed range or
'all cells from one or all worksheets from each workbook in a folder

'First we call the Function "Get_File_Names" to fill a array with all
file names
'There are three arguments in this Function that we can change

'1) MyPath = the folder where the files are
'2) Subfolders = True if you want to include subfolders
'3) ExtStr = file extension of the files you want to merge
' ExtStr examples a "*.xls" , "*.csv" , "*.xlsx"
' "*.xlsm" ,"*.xlsb" , for all Excel file formats use "*.xl*"
' Do not change myReturnedFiles:=myFiles


'Then if there are files in the folder we call the macro "Get_Data"
'There are six arguments in this macro that we can change


'1) FileNameInA = True to add the path/file name in the A column
'2) PasteAsValues = True to paste as values (recommend)
'3) SourceShName = sheet name, if "" it will use the SourceShIndex and
if "all" it copy from all worksheets
'4) SourceShIndex = to avoid problems with different sheet names use the
index (1 is the first worksheet)
'5) SourceRng = Range you want to copy. Tip: "A:F" will copy all cells
with data in this six columns
'6) StartCell = Enter the first cell and the macro will copy from that
cell till the last cell on the worksheet
' If StartCell = "" then it use the SourceRng
' Do not change myReturnedFiles:=myFiles


'The example below will merge A1:G1 from the first worksheet of each
file
'It will use a fixed range on the first worksheet because SourceShName
and StartCell are ""

Sub RDB_Merge_Data()
Dim myFiles As Variant
Dim myCountOfFiles As Long

myCountOfFiles = Get_File_Names( _
MyPath:="C:\Users\Ron\test", _
Subfolders:=False, _
ExtStr:="*.xl*", _
myReturnedFiles:=myFiles)

If myCountOfFiles = 0 Then
MsgBox "No files that match the ExtStr in this folder"
Exit Sub
End If

Get_Data _
FileNameInA:=True, _
PasteAsValues:=True, _
SourceShName:="", _
SourceShIndex:=1, _
SourceRng:="A1:G1", _
StartCell:="", _
myReturnedFiles:=myFiles

End Sub


' Note: You not have to change the macro below, you only
' edit and run the RDB_Merge_Data above.

Sub Get_Data(FileNameInA As Boolean, PasteAsValues As Boolean,
SourceShName As String, _
SourceShIndex As Integer, SourceRng As String, StartCell As
String, myReturnedFiles As Variant)
Dim SourceRcount As Long
Dim SourceRange As Range, destrange As Range
Dim mybook As Workbook, BaseWks As Worksheet
Dim rnum As Long, CalcMode As Long
Dim SourceSh As Variant
Dim sh As Worksheet
Dim I As Long

'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With

'Add a new workbook with one sheet named "Combine Sheet"
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
BaseWks.Name = "Combine Sheet"

'Set start row for the Data
rnum = 1

'Check if we use a named sheet or the index
If SourceShName = "" Then
SourceSh = SourceShIndex
Else
SourceSh = SourceShName
End If

'Loop through all files in the array(myFiles)
For I = LBound(myReturnedFiles) To UBound(myReturnedFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(myReturnedFiles(I))
On Error GoTo 0

If Not mybook Is Nothing Then

If LCase(SourceShName) < "all" Then

'Set SourceRange and check if it is a valid range
On Error Resume Next

If StartCell < "" Then
With mybook.Sheets(SourceSh)
Set SourceRange = .Range(StartCell & ":" &
RDB_Last(3, .Cells))
End With
Else
With mybook.Sheets(SourceSh)
Set SourceRange =
Application.Intersect(.UsedRange, .Range(SourceRng))
End With
End If

If Err.Number 0 Then
Err.Clear
Set SourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If SourceRange.Columns.Count =
BaseWks.Columns.Count Then
Set SourceRange = Nothing
End If
End If
On Error GoTo 0

If Not SourceRange Is Nothing Then

'Check if there enough rows to paste the data
SourceRcount = SourceRange.Rows.Count
If rnum + SourceRcount = BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the
sheet to paste"
mybook.Close savechanges:=False
BaseWks.Parent.Close savechanges:=False
GoTo ExitTheSub
End If

'Set the destination cell
If FileNameInA = True Then
Set destrange = BaseWks.Range("B" & rnum)
With SourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value =
myReturnedFiles(I)
End With
Else
Set destrange = BaseWks.Range("A" & rnum)
End If

'Copy/paste the data
If PasteAsValues = True Then
With SourceRange
Set destrange = destrange. _
Resize(.Rows.Count,
.Columns.Count)
End With
destrange.Value = SourceRange.Value
Else
SourceRange.Copy destrange
End If

rnum = rnum + SourceRcount
End If

'Close the workbook without saving
mybook.Close savechanges:=False

Else

'Loop through all sheets in mybook
For Each sh In mybook.Worksheets

'Set SourceRange and check if it is a valid range
On Error Resume Next

If StartCell < "" Then
With sh
Set SourceRange = .Range(StartCell & ":" &
RDB_Last(3, .Cells))
End With
Else
With sh
Set SourceRange =
Application.Intersect(.UsedRange, .Range(SourceRng))
End With
End If

If Err.Number 0 Then
Err.Clear
Set SourceRange = Nothing
Else
'if SourceRange use almost all columns then skip
this file
If SourceRange.Columns.Count
BaseWks.Columns.Count - 2 Then
Set SourceRange = Nothing
End If
End If
On Error GoTo 0

If Not SourceRange Is Nothing Then

'Check if there enough rows to paste the data
SourceRcount = SourceRange.Rows.Count
If rnum + SourceRcount = BaseWks.Rows.Count
Then
MsgBox "Sorry there are not enough rows in
the sheet to paste"
mybook.Close savechanges:=False
BaseWks.Parent.Close savechanges:=False
GoTo ExitTheSub
End If

'Set the destination cell
If FileNameInA = True Then
Set destrange = BaseWks.Range("C" & rnum)
With SourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value =
myReturnedFiles(I)
BaseWks.Cells(rnum, "B"). _
Resize(.Rows.Count).Value =
sh.Name
End With
Else
Set destrange = BaseWks.Range("A" & rnum)
End If

'Copy/paste the data
If PasteAsValues = True Then
With SourceRange
Set destrange = destrange. _
Resize(.Rows.Count,
.Columns.Count)
End With
destrange.Value = SourceRange.Value
Else
SourceRange.Copy destrange
End If

rnum = rnum + SourceRcount
End If

Next sh

'Close the workbook without saving
mybook.Close savechanges:=False
End If
End If
Next I

'Set the column width in the new workbook
BaseWks.Columns.AutoFit

ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
for more information Try the below link

http://www.rondebruin.nl/merge.htm

Vishwanath N

*** Sent via Developersdex http://www.developersdex.com ***
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default merge

copy this to new module and try

Option Explicit

'The example macro below you can use to merge a fixed range or
'all cells from one or all worksheets from each workbook in a folder

'First we call the Function "Get_File_Names" to fill a array with all
file names
'There are three arguments in this Function that we can change

'1) MyPath = the folder where the files are
'2) Subfolders = True if you want to include subfolders
'3) ExtStr = file extension of the files you want to merge
' ExtStr examples a "*.xls" , "*.csv" , "*.xlsx"
' "*.xlsm" ,"*.xlsb" , for all Excel file formats use "*.xl*"
' Do not change myReturnedFiles:=myFiles


'Then if there are files in the folder we call the macro "Get_Data"
'There are six arguments in this macro that we can change


'1) FileNameInA = True to add the path/file name in the A column
'2) PasteAsValues = True to paste as values (recommend)
'3) SourceShName = sheet name, if "" it will use the SourceShIndex and
if "all" it copy from all worksheets
'4) SourceShIndex = to avoid problems with different sheet names use the
index (1 is the first worksheet)
'5) SourceRng = Range you want to copy. Tip: "A:F" will copy all cells
with data in this six columns
'6) StartCell = Enter the first cell and the macro will copy from that
cell till the last cell on the worksheet
' If StartCell = "" then it use the SourceRng
' Do not change myReturnedFiles:=myFiles


'The example below will merge A1:G1 from the first worksheet of each
file
'It will use a fixed range on the first worksheet because SourceShName
and StartCell are ""

Sub RDB_Merge_Data()
Dim myFiles As Variant
Dim myCountOfFiles As Long

myCountOfFiles = Get_File_Names( _
MyPath:="C:\Users\Ron\test", _
Subfolders:=False, _
ExtStr:="*.xl*", _
myReturnedFiles:=myFiles)

If myCountOfFiles = 0 Then
MsgBox "No files that match the ExtStr in this folder"
Exit Sub
End If

Get_Data _
FileNameInA:=True, _
PasteAsValues:=True, _
SourceShName:="", _
SourceShIndex:=1, _
SourceRng:="A1:G1", _
StartCell:="", _
myReturnedFiles:=myFiles

End Sub


' Note: You not have to change the macro below, you only
' edit and run the RDB_Merge_Data above.

Sub Get_Data(FileNameInA As Boolean, PasteAsValues As Boolean,
SourceShName As String, _
SourceShIndex As Integer, SourceRng As String, StartCell As
String, myReturnedFiles As Variant)
Dim SourceRcount As Long
Dim SourceRange As Range, destrange As Range
Dim mybook As Workbook, BaseWks As Worksheet
Dim rnum As Long, CalcMode As Long
Dim SourceSh As Variant
Dim sh As Worksheet
Dim I As Long

'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With

'Add a new workbook with one sheet named "Combine Sheet"
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
BaseWks.Name = "Combine Sheet"

'Set start row for the Data
rnum = 1

'Check if we use a named sheet or the index
If SourceShName = "" Then
SourceSh = SourceShIndex
Else
SourceSh = SourceShName
End If

'Loop through all files in the array(myFiles)
For I = LBound(myReturnedFiles) To UBound(myReturnedFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(myReturnedFiles(I))
On Error GoTo 0

If Not mybook Is Nothing Then

If LCase(SourceShName) < "all" Then

'Set SourceRange and check if it is a valid range
On Error Resume Next

If StartCell < "" Then
With mybook.Sheets(SourceSh)
Set SourceRange = .Range(StartCell & ":" &
RDB_Last(3, .Cells))
End With
Else
With mybook.Sheets(SourceSh)
Set SourceRange =
Application.Intersect(.UsedRange, .Range(SourceRng))
End With
End If

If Err.Number 0 Then
Err.Clear
Set SourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If SourceRange.Columns.Count =
BaseWks.Columns.Count Then
Set SourceRange = Nothing
End If
End If
On Error GoTo 0

If Not SourceRange Is Nothing Then

'Check if there enough rows to paste the data
SourceRcount = SourceRange.Rows.Count
If rnum + SourceRcount = BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the
sheet to paste"
mybook.Close savechanges:=False
BaseWks.Parent.Close savechanges:=False
GoTo ExitTheSub
End If

'Set the destination cell
If FileNameInA = True Then
Set destrange = BaseWks.Range("B" & rnum)
With SourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value =
myReturnedFiles(I)
End With
Else
Set destrange = BaseWks.Range("A" & rnum)
End If

'Copy/paste the data
If PasteAsValues = True Then
With SourceRange
Set destrange = destrange. _
Resize(.Rows.Count,
.Columns.Count)
End With
destrange.Value = SourceRange.Value
Else
SourceRange.Copy destrange
End If

rnum = rnum + SourceRcount
End If

'Close the workbook without saving
mybook.Close savechanges:=False

Else

'Loop through all sheets in mybook
For Each sh In mybook.Worksheets

'Set SourceRange and check if it is a valid range
On Error Resume Next

If StartCell < "" Then
With sh
Set SourceRange = .Range(StartCell & ":" &
RDB_Last(3, .Cells))
End With
Else
With sh
Set SourceRange =
Application.Intersect(.UsedRange, .Range(SourceRng))
End With
End If

If Err.Number 0 Then
Err.Clear
Set SourceRange = Nothing
Else
'if SourceRange use almost all columns then skip
this file
If SourceRange.Columns.Count
BaseWks.Columns.Count - 2 Then
Set SourceRange = Nothing
End If
End If
On Error GoTo 0

If Not SourceRange Is Nothing Then

'Check if there enough rows to paste the data
SourceRcount = SourceRange.Rows.Count
If rnum + SourceRcount = BaseWks.Rows.Count
Then
MsgBox "Sorry there are not enough rows in
the sheet to paste"
mybook.Close savechanges:=False
BaseWks.Parent.Close savechanges:=False
GoTo ExitTheSub
End If

'Set the destination cell
If FileNameInA = True Then
Set destrange = BaseWks.Range("C" & rnum)
With SourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value =
myReturnedFiles(I)
BaseWks.Cells(rnum, "B"). _
Resize(.Rows.Count).Value =
sh.Name
End With
Else
Set destrange = BaseWks.Range("A" & rnum)
End If

'Copy/paste the data
If PasteAsValues = True Then
With SourceRange
Set destrange = destrange. _
Resize(.Rows.Count,
.Columns.Count)
End With
destrange.Value = SourceRange.Value
Else
SourceRange.Copy destrange
End If

rnum = rnum + SourceRcount
End If

Next sh

'Close the workbook without saving
mybook.Close savechanges:=False
End If
End If
Next I

'Set the column width in the new workbook
BaseWks.Columns.AutoFit

ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
for more information Try the below link

http://www.rondebruin.nl/merge.htm

Vishwanath N

*** Sent via Developersdex http://www.developersdex.com ***
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default merge

On 25 Apr, 12:51, Vishwanath N wrote:
copy this to new module and try

Option Explicit

'The example macro below you can use to merge a fixed range or
'all cells from one or all worksheets from each workbook in a folder

'First we call the Function "Get_File_Names" to fill a array with all
file names
'There are three arguments in this Function that we can change

'1) MyPath = the folder where the files are
'2) Subfolders = True if you want to include subfolders
'3) ExtStr = file extension of the files you want to merge
' * ExtStr examples a "*.xls" , "*.csv" , "*.xlsx"
' * "*.xlsm" ,"*.xlsb" , for all Excel file formats use "*.xl*"
' * Do not change myReturnedFiles:=myFiles

'Then if there are files in the folder we call the macro "Get_Data"
'There are six arguments in this macro that we can change

'1) FileNameInA = True to add the path/file name in the A column
'2) PasteAsValues = True to paste as values (recommend)
'3) SourceShName = sheet name, if "" it will use the SourceShIndex and
if "all" it copy from all worksheets
'4) SourceShIndex = to avoid problems with different sheet names use the
index (1 is the first worksheet)
'5) SourceRng = Range you want to copy. Tip: "A:F" will copy all cells
with data in this six columns
'6) StartCell = Enter the first cell and the macro will copy from that
cell till the last cell on the worksheet
' * If StartCell = "" then it use the SourceRng
' * Do not change myReturnedFiles:=myFiles

'The example below will merge A1:G1 from the first worksheet of each
file
'It will use a fixed range on the first worksheet because SourceShName
and StartCell are ""

Sub RDB_Merge_Data()
* * Dim myFiles As Variant
* * Dim myCountOfFiles As Long

* * myCountOfFiles = Get_File_Names( _
* * * * * * * * * * *MyPath:="C:\Users\Ron\test", _
* * * * * * * * * * *Subfolders:=False, _
* * * * * * * * * * *ExtStr:="*.xl*", _
* * * * * * * * * * *myReturnedFiles:=myFiles)

* * If myCountOfFiles = 0 Then
* * * * MsgBox "No files that match the ExtStr in this folder"
* * * * Exit Sub
* * End If

* * Get_Data _
* * * * * * FileNameInA:=True, _
* * * * * * PasteAsValues:=True, _
* * * * * * SourceShName:="", _
* * * * * * SourceShIndex:=1, _
* * * * * * SourceRng:="A1:G1", _
* * * * * * StartCell:="", _
* * * * * * myReturnedFiles:=myFiles

End Sub

' Note: You not have to change the macro below, you only
' edit and run the RDB_Merge_Data above.

Sub Get_Data(FileNameInA As Boolean, PasteAsValues As Boolean,
SourceShName As String, _
* * * * * * *SourceShIndex As Integer, SourceRng As String, StartCell As
String, myReturnedFiles As Variant)
* * Dim SourceRcount As Long
* * Dim SourceRange As Range, destrange As Range
* * Dim mybook As Workbook, BaseWks As Worksheet
* * Dim rnum As Long, CalcMode As Long
* * Dim SourceSh As Variant
* * Dim sh As Worksheet
* * Dim I As Long

* * 'Change ScreenUpdating, Calculation and EnableEvents
* * With Application
* * * * CalcMode = .Calculation
* * * * .Calculation = xlCalculationManual
* * * * .ScreenUpdating = False
* * * * .EnableEvents = False
* * End With

* * 'Add a new workbook with one sheet named "Combine Sheet"
* * Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
* * BaseWks.Name = "Combine Sheet"

* * 'Set start row for the Data
* * rnum = 1

* * 'Check if we use a named sheet or the index
* * If SourceShName = "" Then
* * * * SourceSh = SourceShIndex
* * Else
* * * * SourceSh = SourceShName
* * End If

* * 'Loop through all files in the array(myFiles)
* * For I = LBound(myReturnedFiles) To UBound(myReturnedFiles)
* * * * Set mybook = Nothing
* * * * On Error Resume Next
* * * * Set mybook = Workbooks.Open(myReturnedFiles(I))
* * * * On Error GoTo 0

* * * * If Not mybook Is Nothing Then

* * * * * * If LCase(SourceShName) < "all" Then

* * * * * * * * 'Set SourceRange and check if it is a valid range
* * * * * * * * On Error Resume Next

* * * * * * * * If StartCell < "" Then
* * * * * * * * * * With mybook.Sheets(SourceSh)
* * * * * * * * * * * * Set SourceRange = .Range(StartCell & ":" &
RDB_Last(3, .Cells))
* * * * * * * * * * End With
* * * * * * * * Else
* * * * * * * * * * With mybook.Sheets(SourceSh)
* * * * * * * * * * * * Set SourceRange =
Application.Intersect(.UsedRange, .Range(SourceRng))
* * * * * * * * * * End With
* * * * * * * * End If

* * * * * * * * If Err.Number 0 Then
* * * * * * * * * * Err.Clear
* * * * * * * * * * Set SourceRange = Nothing
* * * * * * * * Else
* * * * * * * * * * 'if SourceRange use all columns then skip this file
* * * * * * * * * * If SourceRange.Columns.Count =
BaseWks.Columns.Count Then
* * * * * * * * * * * * Set SourceRange = Nothing
* * * * * * * * * * End If
* * * * * * * * End If
* * * * * * * * On Error GoTo 0

* * * * * * * * If Not SourceRange Is Nothing Then

* * * * * * * * * * 'Check if there enough rows to paste the data
* * * * * * * * * * SourceRcount = SourceRange.Rows.Count
* * * * * * * * * * If rnum + SourceRcount = BaseWks.Rows.Count Then
* * * * * * * * * * * * MsgBox "Sorry there are not enough rows in the
sheet to paste"
* * * * * * * * * * * * mybook.Close savechanges:=False
* * * * * * * * * * * * BaseWks.Parent.Close savechanges:=False
* * * * * * * * * * * * GoTo ExitTheSub
* * * * * * * * * * End If

* * * * * * * * * * 'Set the destination cell
* * * * * * * * * * If FileNameInA = True Then
* * * * * * * * * * * * Set destrange = BaseWks.Range("B" & rnum)
* * * * * * * * * * * * With SourceRange
* * * * * * * * * * * * * * BaseWks.Cells(rnum, "A"). _
* * * * * * * * * * * * * * * * * * Resize(.Rows.Count).Value =
myReturnedFiles(I)
* * * * * * * * * * * * End With
* * * * * * * * * * Else
* * * * * * * * * * * * Set destrange = BaseWks.Range("A" & rnum)
* * * * * * * * * * End If

* * * * * * * * * * 'Copy/paste the data
* * * * * * * * * * If PasteAsValues = True Then
* * * * * * * * * * * * With SourceRange
* * * * * * * * * * * * * * Set destrange = destrange. _
* * * * * * * * * * * * * * * * * * * * * * Resize(.Rows.Count,
.Columns.Count)
* * * * * * * * * * * * End With
* * * * * * * * * * * * destrange.Value = SourceRange.Value
* * * * * * * * * * Else
* * * * * * * * * * * * SourceRange.Copy destrange
* * * * * * * * * * End If

* * * * * * * * * * rnum = rnum + SourceRcount
* * * * * * * * End If

* * * * * * * * 'Close the workbook without saving
* * * * * * * * mybook.Close savechanges:=False

* * * * * * Else

* * * * * * * * 'Loop through all sheets in mybook
* * * * * * * * For Each sh In mybook.Worksheets

* * * * * * * * * * 'Set SourceRange and check if it is a valid range
* * * * * * * * * * On Error Resume Next

* * * * * * * * * * If StartCell < "" Then
* * * * * * * * * * * * With sh
* * * * * * * * * * * * * * Set SourceRange = .Range(StartCell & ":" &
RDB_Last(3, .Cells))
* * * * * * * * * * * * End With
* * * * * * * * * * Else
* * * * * * * * * * * * With sh
* * * * * * * * * * * * * * Set SourceRange =
Application.Intersect(.UsedRange, .Range(SourceRng))
* * * * * * * * * * * * End With
* * * * * * * * * * End If

* * * * * * * * * * If Err.Number 0 Then
* * * * * * * * * * * * Err.Clear
* * * * * * * * * * * * Set SourceRange = Nothing
* * * * * * * * * * Else
* * * * * * * * * * * * 'if SourceRange use almost all columns then skip
this file
* * * * * * * * * * * * If SourceRange.Columns.Count
BaseWks.Columns.Count - 2 Then
* * * * * * * * * * * * * * Set SourceRange = Nothing
* * * * * * * * * * * * End If
* * * * * * * * * * End If
* * * * * * * * * * On Error GoTo 0

* * * * * * * * * * If Not SourceRange Is Nothing Then

* * * * * * * * * * * * 'Check if there enough rows to paste the data
* * * * * * * * * * * * SourceRcount = SourceRange.Rows.Count
* * * * * * * * * * * * If rnum + SourceRcount = BaseWks.Rows.Count
Then
* * * * * * * * * * * * * * MsgBox "Sorry there are not enough rows in
the sheet to paste"
* * * * * * * * * * * * * * mybook.Close savechanges:=False
* * * * * * * * * * * * * * BaseWks.Parent.Close savechanges:=False
* * * * * * * * * * * * * * GoTo ExitTheSub
* * * * * * * * * * * * End If

* * * * * * * * * * * * 'Set the destination cell
* * * * * * * * * * * * If FileNameInA = True Then
* * * * * * * * * * * * * * Set destrange = BaseWks.Range("C" & rnum)
* * * * * * * * * * * * * * With SourceRange
* * * * * * * * * * * * * * * * BaseWks.Cells(rnum, "A"). _
* * * * * * * * * * * * * * * * * * * * Resize(.Rows.Count).Value =
myReturnedFiles(I)
* * * * * * * * * * * * * * * * BaseWks.Cells(rnum, "B"). _
* * * * * * * * * * * * * * * * * * * * Resize(.Rows.Count).Value =
sh.Name
* * * * * * * * * * * * * * End With
* * * * * * * * * * * * Else
* * * * * * * * * * * * * * Set destrange = BaseWks.Range("A" & rnum)
* * * * * * * * * * * * End If

* * * * * * * * * * * * 'Copy/paste the data
* * * * * * * * * * * * If PasteAsValues = True Then
* * * * * * * * * * * * * * With SourceRange
* * * * * * * * * * * * * * * * Set destrange = destrange. _
* * * * * * * * * * * * * * * * * * * * * * * * Resize(.Rows.Count,
.Columns.Count)
* * * * * * * * * * * * * * End With
* * * * * * * * * * * * * * destrange.Value = SourceRange.Value
* * * * * * * * * * * * Else
* * * * * * * * * * * * * * SourceRange.Copy destrange
* * * * * * * * * * * * End If

* * * * * * * * * * * * rnum = rnum + SourceRcount
* * * * * * * * * * End If

* * * * * * * * Next sh

* * * * * * * * 'Close the workbook without saving
* * * * * * * * mybook.Close savechanges:=False
* * * * * * End If
* * * * End If
* * Next I

* * 'Set the column width in the new workbook
* * BaseWks.Columns.AutoFit

ExitTheSub:
* * 'Restore ScreenUpdating, Calculation and EnableEvents
* * With Application
* * * * .ScreenUpdating = True
* * * * .EnableEvents = True
* * * * .Calculation = CalcMode
* * End With
End Sub
for more information Try the below link

http://www.rondebruin.nl/merge.htm

Vishwanath N

*** Sent via Developersdexhttp://www.developersdex.com***


Thanks for this ...

A small problem when running the macro .. I get the following error

Compile Error
Sub or Function not defined.

I think this refering to
Get_File_Names function as this is high lighted...

Regards

Rob
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default merge

See
http://www.rondebruin.nl/fso.htm



--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"roblowein" wrote in message ...
On 25 Apr, 12:51, Vishwanath N wrote:
copy this to new module and try

Option Explicit

'The example macro below you can use to merge a fixed range or
'all cells from one or all worksheets from each workbook in a folder

'First we call the Function "Get_File_Names" to fill a array with all
file names
'There are three arguments in this Function that we can change

'1) MyPath = the folder where the files are
'2) Subfolders = True if you want to include subfolders
'3) ExtStr = file extension of the files you want to merge
' ExtStr examples a "*.xls" , "*.csv" , "*.xlsx"
' "*.xlsm" ,"*.xlsb" , for all Excel file formats use "*.xl*"
' Do not change myReturnedFiles:=myFiles

'Then if there are files in the folder we call the macro "Get_Data"
'There are six arguments in this macro that we can change

'1) FileNameInA = True to add the path/file name in the A column
'2) PasteAsValues = True to paste as values (recommend)
'3) SourceShName = sheet name, if "" it will use the SourceShIndex and
if "all" it copy from all worksheets
'4) SourceShIndex = to avoid problems with different sheet names use the
index (1 is the first worksheet)
'5) SourceRng = Range you want to copy. Tip: "A:F" will copy all cells
with data in this six columns
'6) StartCell = Enter the first cell and the macro will copy from that
cell till the last cell on the worksheet
' If StartCell = "" then it use the SourceRng
' Do not change myReturnedFiles:=myFiles

'The example below will merge A1:G1 from the first worksheet of each
file
'It will use a fixed range on the first worksheet because SourceShName
and StartCell are ""

Sub RDB_Merge_Data()
Dim myFiles As Variant
Dim myCountOfFiles As Long

myCountOfFiles = Get_File_Names( _
MyPath:="C:\Users\Ron\test", _
Subfolders:=False, _
ExtStr:="*.xl*", _
myReturnedFiles:=myFiles)

If myCountOfFiles = 0 Then
MsgBox "No files that match the ExtStr in this folder"
Exit Sub
End If

Get_Data _
FileNameInA:=True, _
PasteAsValues:=True, _
SourceShName:="", _
SourceShIndex:=1, _
SourceRng:="A1:G1", _
StartCell:="", _
myReturnedFiles:=myFiles

End Sub

' Note: You not have to change the macro below, you only
' edit and run the RDB_Merge_Data above.

Sub Get_Data(FileNameInA As Boolean, PasteAsValues As Boolean,
SourceShName As String, _
SourceShIndex As Integer, SourceRng As String, StartCell As
String, myReturnedFiles As Variant)
Dim SourceRcount As Long
Dim SourceRange As Range, destrange As Range
Dim mybook As Workbook, BaseWks As Worksheet
Dim rnum As Long, CalcMode As Long
Dim SourceSh As Variant
Dim sh As Worksheet
Dim I As Long

'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With

'Add a new workbook with one sheet named "Combine Sheet"
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
BaseWks.Name = "Combine Sheet"

'Set start row for the Data
rnum = 1

'Check if we use a named sheet or the index
If SourceShName = "" Then
SourceSh = SourceShIndex
Else
SourceSh = SourceShName
End If

'Loop through all files in the array(myFiles)
For I = LBound(myReturnedFiles) To UBound(myReturnedFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(myReturnedFiles(I))
On Error GoTo 0

If Not mybook Is Nothing Then

If LCase(SourceShName) < "all" Then

'Set SourceRange and check if it is a valid range
On Error Resume Next

If StartCell < "" Then
With mybook.Sheets(SourceSh)
Set SourceRange = .Range(StartCell & ":" &
RDB_Last(3, .Cells))
End With
Else
With mybook.Sheets(SourceSh)
Set SourceRange =
Application.Intersect(.UsedRange, .Range(SourceRng))
End With
End If

If Err.Number 0 Then
Err.Clear
Set SourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If SourceRange.Columns.Count =
BaseWks.Columns.Count Then
Set SourceRange = Nothing
End If
End If
On Error GoTo 0

If Not SourceRange Is Nothing Then

'Check if there enough rows to paste the data
SourceRcount = SourceRange.Rows.Count
If rnum + SourceRcount = BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the
sheet to paste"
mybook.Close savechanges:=False
BaseWks.Parent.Close savechanges:=False
GoTo ExitTheSub
End If

'Set the destination cell
If FileNameInA = True Then
Set destrange = BaseWks.Range("B" & rnum)
With SourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value =
myReturnedFiles(I)
End With
Else
Set destrange = BaseWks.Range("A" & rnum)
End If

'Copy/paste the data
If PasteAsValues = True Then
With SourceRange
Set destrange = destrange. _
Resize(.Rows.Count,
.Columns.Count)
End With
destrange.Value = SourceRange.Value
Else
SourceRange.Copy destrange
End If

rnum = rnum + SourceRcount
End If

'Close the workbook without saving
mybook.Close savechanges:=False

Else

'Loop through all sheets in mybook
For Each sh In mybook.Worksheets

'Set SourceRange and check if it is a valid range
On Error Resume Next

If StartCell < "" Then
With sh
Set SourceRange = .Range(StartCell & ":" &
RDB_Last(3, .Cells))
End With
Else
With sh
Set SourceRange =
Application.Intersect(.UsedRange, .Range(SourceRng))
End With
End If

If Err.Number 0 Then
Err.Clear
Set SourceRange = Nothing
Else
'if SourceRange use almost all columns then skip
this file
If SourceRange.Columns.Count
BaseWks.Columns.Count - 2 Then
Set SourceRange = Nothing
End If
End If
On Error GoTo 0

If Not SourceRange Is Nothing Then

'Check if there enough rows to paste the data
SourceRcount = SourceRange.Rows.Count
If rnum + SourceRcount = BaseWks.Rows.Count
Then
MsgBox "Sorry there are not enough rows in
the sheet to paste"
mybook.Close savechanges:=False
BaseWks.Parent.Close savechanges:=False
GoTo ExitTheSub
End If

'Set the destination cell
If FileNameInA = True Then
Set destrange = BaseWks.Range("C" & rnum)
With SourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value =
myReturnedFiles(I)
BaseWks.Cells(rnum, "B"). _
Resize(.Rows.Count).Value =
sh.Name
End With
Else
Set destrange = BaseWks.Range("A" & rnum)
End If

'Copy/paste the data
If PasteAsValues = True Then
With SourceRange
Set destrange = destrange. _
Resize(.Rows.Count,
.Columns.Count)
End With
destrange.Value = SourceRange.Value
Else
SourceRange.Copy destrange
End If

rnum = rnum + SourceRcount
End If

Next sh

'Close the workbook without saving
mybook.Close savechanges:=False
End If
End If
Next I

'Set the column width in the new workbook
BaseWks.Columns.AutoFit

ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
for more information Try the below link

http://www.rondebruin.nl/merge.htm

Vishwanath N

*** Sent via Developersdexhttp://www.developersdex.com***


Thanks for this ...

A small problem when running the macro .. I get the following error

Compile Error
Sub or Function not defined.

I think this refering to
Get_File_Names function as this is high lighted...

Regards

Rob
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
merge two excel files like in word mail merge azmerritt Excel Discussion (Misc queries) 1 December 11th 16 09:23 PM
Zip codes in mail merge - first digit doesn't display in merge Cheryl Excel Discussion (Misc queries) 1 December 22nd 09 08:13 PM
mail merge excludes my headers and critical data in Word merge Nix Excel Discussion (Misc queries) 0 April 21st 06 08:35 PM
Merge =( formula should retain fraction type numbers after merge. Aubrey Excel Worksheet Functions 0 February 9th 06 07:37 PM
how do i get my mail merge to update the data source at each merge Steel_Monkey Excel Discussion (Misc queries) 0 November 30th 05 08:41 AM


All times are GMT +1. The time now is 08:25 PM.

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"