Hi All,</SPAN>
I would like to receive some help or suggestions on the following code based on what I am trying to do. It is copied from this website -</SPAN>
http://msdn.microsoft.com/en-us/library/cc837974(v=office.12).aspx </SPAN>
The code works perfectly but works a slightly different way that I want to. What it does right now is to merge cells (A2:C15) of every worksheet named "xxx" from all workbooks in a folder to a summary sheet (Sheet2) of an active workbook. The loops work perfectly by the way.</SPAN> </SPAN>
I don't want it to go through every file in the directory and merge the range from every file that meets the criteria above. Here is what I need the code to do. The code only allows to merge files with file names I type into cells A:1:A6 of my active workbook(Sheet1). Let's say I have in cell1 of my worksheet typed "myfile", the code will go out and searches the folder for myfile.xls and copies all cells from A:C15 from worksheet named "xxx". Now, I need the same when I input others file names in my sheet column. The botoom line is that the code will only loop through files based on file names in column A of my current workbook. </SPAN>
Thanks a lot everyone!!!</SPAN>
Private Sub CommandButton1_Click()</SPAN>
Dim MyPath As String, FilesInPath As String</SPAN>
Dim MyFiles() As String</SPAN>
Dim SourceRcount As Long, FNum As Long</SPAN>
Dim mybook As Workbook, BaseWks As Worksheet</SPAN>
Dim sourceRange As Range, destrange As Range</SPAN>
Dim rnum As Long, CalcMode As Long</SPAN>
' Change this to the path\folder location of your files.</SPAN>
MyPath = "</SPAN>C:\Users\Ron\test</SPAN>"</SPAN>
' Add a slash at the end of the path if needed.</SPAN>
If Right(MyPath, 1) <> "\" Then</SPAN>
MyPath = MyPath & "\"</SPAN>
End If</SPAN>
' If there are no Excel files in the folder, exit.</SPAN>
FilesInPath = Dir(MyPath & "*.xls*")</SPAN>
If FilesInPath = "" Then</SPAN>
MsgBox "No files found"</SPAN>
Exit Sub</SPAN>
End If</SPAN>
' Fill the myFiles array with the list of Excel files</SPAN>
' in the search folder.</SPAN>
FNum = 0</SPAN>
Do While FilesInPath <> ""</SPAN>
FNum = FNum + 1</SPAN>
ReDim Preserve MyFiles(1 To FNum)</SPAN>
MyFiles(FNum) = FilesInPath</SPAN>
FilesInPath = Dir()</SPAN>
Loop</SPAN>
' Set various application properties.</SPAN>
With Application</SPAN>
CalcMode = .Calculation</SPAN>
.Calculation = xlCalculationManual</SPAN>
.ScreenUpdating = False</SPAN>
.EnableEvents = False</SPAN>
End With</SPAN>
' Active worksheet.</SPAN>
Set BaseWks = ActiveWorkbook.Worksheets("Sheet2")</SPAN>
rnum = 2</SPAN>
' Loop through all files in the myFiles array.</SPAN>
If FNum > 0 Then</SPAN>
For FNum = LBound(MyFiles) To UBound(MyFiles)</SPAN>
Set mybook = Nothing</SPAN>
On Error Resume Next</SPAN>
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))</SPAN>
On Error GoTo 0</SPAN>
If Not mybook Is Nothing Then</SPAN>
On Error Resume Next</SPAN>
' Change this range to fit your own needs.</SPAN>
With mybook.Worksheets("xxx")</SPAN>
Set sourceRange = .Range("A2:C15")</SPAN>
End With</SPAN>
If Err.Number > 0 Then</SPAN>
Err.Clear</SPAN>
Set sourceRange = Nothing</SPAN>
Else</SPAN>
' If source range uses all columns then</SPAN>
' skip this file.</SPAN>
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then</SPAN>
Set sourceRange = Nothing</SPAN>
End If</SPAN>
End If</SPAN>
On Error GoTo 0</SPAN>
If Not sourceRange Is Nothing Then</SPAN>
SourceRcount = sourceRange.Rows.Count</SPAN>
If rnum + SourceRcount >= BaseWks.Rows.Count Then</SPAN>
MsgBox "There are not enough rows in the target worksheet."</SPAN>
BaseWks.Columns.AutoFit</SPAN>
mybook.Close savechanges:=False</SPAN>
GoTo ExitTheSub</SPAN>
Else</SPAN>
' Set the destination range.</SPAN>
Set destrange = BaseWks.Range("A" & rnum)</SPAN>
' Copy the values from the source range</SPAN>
' to the destination range.</SPAN>
With sourceRange</SPAN>
Set destrange = destrange. _</SPAN>
Resize(.Rows.Count, .Columns.Count)</SPAN>
End With</SPAN>
destrange.Value = sourceRange.Value</SPAN>
rnum = rnum + SourceRcount</SPAN>
End If</SPAN>
End If</SPAN>
mybook.Close savechanges:=False</SPAN>
End If</SPAN>
Next FNum</SPAN>
BaseWks.Columns.AutoFit</SPAN>
End If</SPAN>
ExitTheSub:</SPAN>
' Restore the application properties.</SPAN>
With Application</SPAN>
.ScreenUpdating = True</SPAN>
.EnableEvents = True</SPAN>
.Calculation = CalcMode</SPAN>
End With</SPAN>
End Sub</SPAN>
I would like to receive some help or suggestions on the following code based on what I am trying to do. It is copied from this website -</SPAN>
http://msdn.microsoft.com/en-us/library/cc837974(v=office.12).aspx </SPAN>
The code works perfectly but works a slightly different way that I want to. What it does right now is to merge cells (A2:C15) of every worksheet named "xxx" from all workbooks in a folder to a summary sheet (Sheet2) of an active workbook. The loops work perfectly by the way.</SPAN> </SPAN>
I don't want it to go through every file in the directory and merge the range from every file that meets the criteria above. Here is what I need the code to do. The code only allows to merge files with file names I type into cells A:1:A6 of my active workbook(Sheet1). Let's say I have in cell1 of my worksheet typed "myfile", the code will go out and searches the folder for myfile.xls and copies all cells from A:C15 from worksheet named "xxx". Now, I need the same when I input others file names in my sheet column. The botoom line is that the code will only loop through files based on file names in column A of my current workbook. </SPAN>
Thanks a lot everyone!!!</SPAN>
Private Sub CommandButton1_Click()</SPAN>
Dim MyPath As String, FilesInPath As String</SPAN>
Dim MyFiles() As String</SPAN>
Dim SourceRcount As Long, FNum As Long</SPAN>
Dim mybook As Workbook, BaseWks As Worksheet</SPAN>
Dim sourceRange As Range, destrange As Range</SPAN>
Dim rnum As Long, CalcMode As Long</SPAN>
' Change this to the path\folder location of your files.</SPAN>
MyPath = "</SPAN>C:\Users\Ron\test</SPAN>"</SPAN>
' Add a slash at the end of the path if needed.</SPAN>
If Right(MyPath, 1) <> "\" Then</SPAN>
MyPath = MyPath & "\"</SPAN>
End If</SPAN>
' If there are no Excel files in the folder, exit.</SPAN>
FilesInPath = Dir(MyPath & "*.xls*")</SPAN>
If FilesInPath = "" Then</SPAN>
MsgBox "No files found"</SPAN>
Exit Sub</SPAN>
End If</SPAN>
' Fill the myFiles array with the list of Excel files</SPAN>
' in the search folder.</SPAN>
FNum = 0</SPAN>
Do While FilesInPath <> ""</SPAN>
FNum = FNum + 1</SPAN>
ReDim Preserve MyFiles(1 To FNum)</SPAN>
MyFiles(FNum) = FilesInPath</SPAN>
FilesInPath = Dir()</SPAN>
Loop</SPAN>
' Set various application properties.</SPAN>
With Application</SPAN>
CalcMode = .Calculation</SPAN>
.Calculation = xlCalculationManual</SPAN>
.ScreenUpdating = False</SPAN>
.EnableEvents = False</SPAN>
End With</SPAN>
' Active worksheet.</SPAN>
Set BaseWks = ActiveWorkbook.Worksheets("Sheet2")</SPAN>
rnum = 2</SPAN>
' Loop through all files in the myFiles array.</SPAN>
If FNum > 0 Then</SPAN>
For FNum = LBound(MyFiles) To UBound(MyFiles)</SPAN>
Set mybook = Nothing</SPAN>
On Error Resume Next</SPAN>
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))</SPAN>
On Error GoTo 0</SPAN>
If Not mybook Is Nothing Then</SPAN>
On Error Resume Next</SPAN>
' Change this range to fit your own needs.</SPAN>
With mybook.Worksheets("xxx")</SPAN>
Set sourceRange = .Range("A2:C15")</SPAN>
End With</SPAN>
If Err.Number > 0 Then</SPAN>
Err.Clear</SPAN>
Set sourceRange = Nothing</SPAN>
Else</SPAN>
' If source range uses all columns then</SPAN>
' skip this file.</SPAN>
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then</SPAN>
Set sourceRange = Nothing</SPAN>
End If</SPAN>
End If</SPAN>
On Error GoTo 0</SPAN>
If Not sourceRange Is Nothing Then</SPAN>
SourceRcount = sourceRange.Rows.Count</SPAN>
If rnum + SourceRcount >= BaseWks.Rows.Count Then</SPAN>
MsgBox "There are not enough rows in the target worksheet."</SPAN>
BaseWks.Columns.AutoFit</SPAN>
mybook.Close savechanges:=False</SPAN>
GoTo ExitTheSub</SPAN>
Else</SPAN>
' Set the destination range.</SPAN>
Set destrange = BaseWks.Range("A" & rnum)</SPAN>
' Copy the values from the source range</SPAN>
' to the destination range.</SPAN>
With sourceRange</SPAN>
Set destrange = destrange. _</SPAN>
Resize(.Rows.Count, .Columns.Count)</SPAN>
End With</SPAN>
destrange.Value = sourceRange.Value</SPAN>
rnum = rnum + SourceRcount</SPAN>
End If</SPAN>
End If</SPAN>
mybook.Close savechanges:=False</SPAN>
End If</SPAN>
Next FNum</SPAN>
BaseWks.Columns.AutoFit</SPAN>
End If</SPAN>
ExitTheSub:</SPAN>
' Restore the application properties.</SPAN>
With Application</SPAN>
.ScreenUpdating = True</SPAN>
.EnableEvents = True</SPAN>
.Calculation = CalcMode</SPAN>
End With</SPAN>
End Sub</SPAN>