Merge all files in a folder

F

franciz

Hi all

I am using the the codes in Merge a range from all workbooks in a folder
with AutoFilter provided by Ron de Bruin

Is it possible to have two auto filters enable in the codes? if tes,
How do I write the additional syntax to filter for NonBlanks rows in column
C given that the current codes provided wrote as :

FilterField = 2
SearchValue = "Y"

Thanks for your assistance

regards, francis
 
R

Ron de Bruin

Hi franciz

For others this is the code page (last example)
http://www.rondebruin.nl/copy3.htm

We can add one line

Replace this:
sourceRange.AutoFilter Field:=FilterField, _
Criteria1:=SearchValue


With:

'Filter the range on the FilterField column
sourceRange.AutoFilter Field:=FilterField, _
Criteria1:=SearchValue

sourceRange.AutoFilter Field:=3, Criteria1:="<>"
 
F

franciz

Hi Ron,

Thanks, this work great. I need two more modification in the excellent codes
of yours.

Is it also possible to have the header include, my source files headers in
on row 2. I have tried to modify the below to include a header but was
unsucessful.

' Set a range without the Header row
Set rng = .Resize(.Rows.Count - 1,
..Columns.Count). _
Offset(1,
0).SpecialCells(xlCellTypeVisible)

In addition, I would like the new workbook name as "Utility" rather than
"Sheet1".
Embarrass to say that I could not find the codes that mentioned naming the
workbook as "Sheet1"

Thank you for your assistance

regards, francis
 
R

Ron de Bruin

Use

Set rng = .SpecialCells(xlCellTypeVisible)


You must save the file before Sheet1 will be changed
If you create a one sheet workbook like I do in the code the name is automatic Sheet1

After this line
BaseWks.Columns.AutoFit

Add
BaseWks.SaveAs "C:\Utility.xls"

If you want to close it add this one also
BaseWks.Close False

What do you want to do if there is alrewady a file with that name?
Replace ?

You can add the date/time to the file name for example
 
F

franciz

Hi Ron,

Thank for assisting in this, I appreciate your effort and patience.

1) Using this line : Set rng = .SpecialCells(xlCellTypeVisible)
will include header row which is on the row 1 of the source files, but
my header row from the source files start at row 2. How do I change this?

2) Quote " You can add the date/time to the file name for example "

This is a good idea as it allow me to save different file names and keep it
for
a period just in case I need to refer back to what have been done.How do I
add this into the line. Appreciate your help in this.


Thanks in advance

regards, francis
 
R

Ron de Bruin

Hi francis


Change

RangeAddress = Range("A1:G" & Rows.Count).Address

To

RangeAddress = Range("A2:G" & Rows.Count).Address
Change the Column to yours


Use this to save

BaseWks.SaveAs "C:\Utility " & Format(Now, "yyyy-mm-dd h-mm-ss") & ".xls"
 
F

franciz

Hi Ron,

Thank you very much for your help in this. It does do what I want except
that the
Headers of all the sourcefile does appear in the Sheet1's result on multiple
rows.

Is it possible to have the Header appear only once as the Header in the
result, ie Sheet1 of the new workbook on row 1?

Further, the current result show that the files' name start in A1, how can I
move it to start at A2 and name the Header column as Securities since the
first row is a Header row

How do I change this line of code to add the number of worksheet instead 1

'Add a new workbook with one sheet
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum = 1

Your help and guidance is very much appreciate

regards, francis
 
R

Ron de Bruin

I have a typo in the save line and the close line
Must be
' BaseWks.parent.SaveAs "C:\Utility " & Format(Now, "yyyy-mm-dd h-mm-ss") & ".xls"
' BaseWks.parent.Close False

Change the header range here

If FNum = 1 Then
mybook.Worksheets(ShName).Range("A2:N2").Copy _
BaseWks.Range("A1")
End If

Full macro looks like this

Sub Basic_Example_4()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
Dim rng As Range, SearchValue As String
Dim FilterField As Integer, RangeAddress As String
Dim ShName As Variant, RwCount As Long

'**********************************************************
'***Change this five code lines before you run the macro***
'**********************************************************

'Fill in the path\folder where the files are
MyPath = "C:\Users\Ron\test"

'Fill in the sheet name where the data is in each workbook
'Use ShName = "Sheet1" if you want to use a sheet name instead if the index
'We use the first sheet in every workbook in this example(I use the index)
ShName = 1

'Fill in the filter range: A1 is the header of the first column and G is
'the last column in the range and it will filter on all rows on the sheet
'You can also use a fixed range like A1:G2500 if you want
RangeAddress = Range("A2:G" & Rows.Count).Address

'Field that you want to filter in the range ( 1 = column A in this
'example because the filter range start in column A
FilterField = 2

'Fill in the filter value ("<>ron" if you want the opposite)
'Or use wildcards like "*ron" for cells that start with ron or use
'"*ron*" if you look for cells where ron is a part of the cell value
SearchValue = "y"

'**********************************************************
'**********************************************************


'Add a slash after MyPath if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If

'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If

'Fill the array(myFiles)with the list of Excel files in the folder
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop

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

'Add a new workbook with one sheet
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum = 1

'Loop through all files in the array(myFiles)
If FNum > 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
On Error GoTo 0

If Not mybook Is Nothing Then

On Error Resume Next
'set filter range
With mybook.Worksheets(ShName)
Set sourceRange = .Range(RangeAddress)
End With

If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
End If
On Error GoTo 0

If Not sourceRange Is Nothing Then

If FNum = 1 Then
mybook.Worksheets(ShName).Range("A2:N2").Copy _
BaseWks.Range("A1")
End If

'Find the last row in BaseWks
rnum = RDB_Last(1, BaseWks.Cells) + 1

With sourceRange.Parent
Set rng = Nothing

'Firstly, remove the AutoFilter
.AutoFilterMode = False

'Filter the range on the FilterField column
sourceRange.AutoFilter Field:=FilterField, _
Criteria1:=SearchValue

sourceRange.AutoFilter Field:=3, Criteria1:="<>"

With .AutoFilter.Range

'Check if there are results after you use AutoFilter
RwCount = .Columns(1).Cells. _
SpecialCells(xlCellTypeVisible).Cells.Count - 1

If RwCount = 0 Then
'There is no data, only the header
Else
' Set a range without the Header row
Set rng = .Resize(.Rows.Count - 1, .Columns.Count). _
Offset(1, 0).SpecialCells(xlCellTypeVisible)


'Copy the range and the file name in column A
If rnum + RwCount < BaseWks.Rows.Count Then
BaseWks.Cells(rnum, "A").Resize(RwCount).Value _
= mybook.Name
rng.Copy BaseWks.Cells(rnum, "B")
End If
End If

End With

'Remove the AutoFilter
.AutoFilterMode = False

End With
End If

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

'Open the next workbook
Next FNum

'Set the column width in the new workbook
BaseWks.Columns.AutoFit
BaseWks.parent.SaveAs "C:\Utility " & Format(Now, "yyyy-mm-dd h-mm-ss") & ".xls"
' BaseWks.parent.Close False
MsgBox "Look at the merge results in the new workbook after you click on OK"
End If

'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
 
R

Ron de Bruin

I was to fast
Further, the current result show that the files' name start in A1, how can I
move it to start at A2 and name the Header column as Securities since the
first row is a Header row

Also

If FNum = 1 Then
mybook.Worksheets(ShName).Range("A2:N2").Copy _
BaseWks.Range("B1")
BaseWks.Range("A1").Value = "WhatYouWant"
End If

How do I change this line of code to add the number of worksheet instead 1

Do you want to use your default amount of sheets when you open a new workbook or a different number
Why do you want to have empty sheets ?
 
F

franciz

Hi Ron,

I was having problem to respond as I was not able to access the reply page
after I click on reply, not sure why.

Thank for your assistance, your codes works excellently.
The reason for additional sheets is because I will copy a table in sheet1 of
another workbook, named "OLT" to a new sheet here. This is for a lookup on
customer names in Column C and its related rates in Column A of the "OLT"
workbook, so that VLookup can look at the table and search the customer names
in Column B and return the related rates in Column M of the active workbook,
named "Utility yyyy-mm-dd h-mm-ss"

Another option is to look at workbook "OLT" without copying the table to the
active
workbook, but not sure is this possible and should the other workbook be open?

What do you think? Your suggestion and input is very appreciate and valuable.

Another request is the the current macro will filter as :

sourceRange.AutoFilter Field:=FilterField, _
Criteria1:=SearchValue
FilterField = 12
SearchValue = "m"

I have add the following filter :
sourceRange.AutoFilter Field:=3, Criteria1:="Y"
sourceRange.AutoFilter Field:=2, Criteria1:="<>"

I need to also filter for Criteria1:="c" in Column L by adding this line

sourceRange.AutoFilter Field:=12, Criteria1:="c"

and copy this result to Sheet2 for next day processing in which the "c" will
be change to "m" so that these data can be include in the filter list when
the Merge
macro is run.

Your codes is superior to my current level of understanding in this subject
as I have just started to study and learn VBA, I was looking for
Worksheet.Add but couldn't
find it. Your effort and guidance is very much appreciate.

Many Thanks.

regards, francis
 
R

Ron de Bruin

Hi franciz

You can use this to add two empty worksheets

Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
BaseWks.Parent.Worksheets.Add.Name = "Yournewsheet1"
BaseWks.Parent.Worksheets.Add.Name = "Yournewsheet2"

Lookup formulas will work with closed workbooks
If you create the formulas with both workbooks and save the file then it
will work without opening the OLT workbook.

Autofilter can have two options for each column

sourceRange.AutoFilter Field:=1, Criteria1:="=m", Operator:=xlOr, Criteria2:="=c"
 
F

franciz

Hi Ron,

Thanks for your assistance and valuable input.

I have not test the additional codes but will do it tomorrow and revert to
you on the outcome.

You mentioned that " Lookup formulas will work with closed workbooks
If you create the formulas with both workbooks and save the file then it
will work without opening the OLT workbook "

I am not too sure I understand this if for example, I use Vlookup in the
"Utility" workbook which is the workbook that shows the result of the filter
range, what other
formula do I need to use in the "OLT" workbook?

Pls advise, thanks

Regards, francis
 
R

Ron de Bruin

If your data table is in the OLT workbook

Your formula looks like this
=VLOOKUP(A1,'C:\Users\Ron\Desktop\[OLT.xls]Sheet1'!$A$1:$B$10,2,FALSE)

With both workbooks open enter the = sign and build the formula and use your mouse
to select the cell or data table.
After you close the OLT workbook it will automatic add the path before the workbook name in the formula.

Good luck
 
F

franciz

Hi Ron

I have insert this line :

sourceRange.AutoFilter Field:=12, Criteria1:="m", Operator:=xlOr,
Criteria2:="c"
With .AutoFilter.Range

under
sourceRange.AutoFilter Field:=FilterField, _
Criteria1:=SearchValue
sourceRange.AutoFilter Field:=2, Criteria1:="<>"


with the top having :

FilterField = 3
SearchValue = "Y"

the result doesn't show as expected, ie all the "m" with the other criteria
on the "Sheet1" and all the "c" on another sheet.

The result show all the "m" and "c" appear on sheet1.

Appreciate your guidance on where have I went wrong on this.

Thanks

regards, francis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top