jaynorman46
New Member
- Joined
- Apr 5, 2011
- Messages
- 23
Hi,
Im new to this forum and am sorry if this question has been asked already..
I have code that combines multiple workbooks into a single sheet, However the problem is the order that they are combined. All of these files are in a single folder and there is no other type of file in the folder, also all files are .xlsm and not .xlsx. I have renamed all of them using the alt+255 function so that all of the of the files are
(1).xlsm
(2).xlsm
(3).xlsm
etc... all the way to (339).xlsm
The problem,
the code imports in this order
(1).xlsm
(10).xlsm
(100).xlsm
(101).xlsm
etc....
and I need it in 1,2,3,4 order
Is there anything I can do about this? I have tried multiple different types of Loops and posted codes for this and they all do the same order..
Here is the code(based off of schielrn (Board Regular) code post Jan 18th, 2008, 12:05 PM):
Sub MergeFiles()
Dim path As String, ThisWB As String, lngFilecounter As Long
Dim wbDest As Workbook, shtDest As Worksheet, ws As Worksheet
Dim Filename As String, Wkb As Workbook
Dim CopyRng As Range, Dest As Range
ThisWB = ActiveWorkbook.Name
Set wbDest = ActiveWorkbook
path = ("C:\Documents and Settings\JNorma1\My Documents\Happy Jack Daily site reports 2010")
Application.EnableEvents = False
Application.ScreenUpdating = False
Set shtDest = ActiveWorkbook.Sheets("Upload File")
Filename = Dir(path & "\*.xls", vbNormal)
If Len(Filename) = 0 Then Exit Sub
Do Until Filename = vbNullString
If Not Filename = ThisWB Then
Set Wkb = Workbooks.Open(Filename:=path & "\" & Filename)
Set CopyRng = Wkb.Sheets(1).Range("G31:N45")
Set Dest = shtDest.Range("A" & shtDest.UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1)
CopyRng.Copy
wbDest.Activate
Sheets("Upload File").Select
Dest.PasteSpecial xlPasteValuesAndNumberFormats
Application.CutCopyMode = False 'Clear Clipboard
Wkb.Close True
End If
Filename = Dir()
Loop
Range("A1").Select
Application.EnableEvents = True
Application.ScreenUpdating = True
MsgBox "Done!"
End Sub
Im new to this forum and am sorry if this question has been asked already..
I have code that combines multiple workbooks into a single sheet, However the problem is the order that they are combined. All of these files are in a single folder and there is no other type of file in the folder, also all files are .xlsm and not .xlsx. I have renamed all of them using the alt+255 function so that all of the of the files are
(1).xlsm
(2).xlsm
(3).xlsm
etc... all the way to (339).xlsm
The problem,
the code imports in this order
(1).xlsm
(10).xlsm
(100).xlsm
(101).xlsm
etc....
and I need it in 1,2,3,4 order
Is there anything I can do about this? I have tried multiple different types of Loops and posted codes for this and they all do the same order..
Here is the code(based off of schielrn (Board Regular) code post Jan 18th, 2008, 12:05 PM):
Sub MergeFiles()
Dim path As String, ThisWB As String, lngFilecounter As Long
Dim wbDest As Workbook, shtDest As Worksheet, ws As Worksheet
Dim Filename As String, Wkb As Workbook
Dim CopyRng As Range, Dest As Range
ThisWB = ActiveWorkbook.Name
Set wbDest = ActiveWorkbook
path = ("C:\Documents and Settings\JNorma1\My Documents\Happy Jack Daily site reports 2010")
Application.EnableEvents = False
Application.ScreenUpdating = False
Set shtDest = ActiveWorkbook.Sheets("Upload File")
Filename = Dir(path & "\*.xls", vbNormal)
If Len(Filename) = 0 Then Exit Sub
Do Until Filename = vbNullString
If Not Filename = ThisWB Then
Set Wkb = Workbooks.Open(Filename:=path & "\" & Filename)
Set CopyRng = Wkb.Sheets(1).Range("G31:N45")
Set Dest = shtDest.Range("A" & shtDest.UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1)
CopyRng.Copy
wbDest.Activate
Sheets("Upload File").Select
Dest.PasteSpecial xlPasteValuesAndNumberFormats
Application.CutCopyMode = False 'Clear Clipboard
Wkb.Close True
End If
Filename = Dir()
Loop
Range("A1").Select
Application.EnableEvents = True
Application.ScreenUpdating = True
MsgBox "Done!"
End Sub