Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default opening order macro for files in a folder

I've setup a pretty neat macro that opens 50+ csv file and then copies the
range (A28:B128) consecutively underneath each other but I'm worried that I
have no control over the order in which these files are being opened/inserted
underneath each other...I want to choose the opening order based on the file
name only (either a-z or z-a)..knowing how to order an open by date is also
very helpful to me....thank you in advance......Here's the opening macro I've
been using:

Sub monthly_reports_opens_change_dir_()
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim SourceRcount As Long
Dim N As Long
Dim rnum As Long
Dim MyPath As String
Dim SaveDriveDir As String
Dim FName As Variant

SaveDriveDir = CurDir
MyPath = "C:\Documents and Settings\May 2006"
ChDrive MyPath
ChDir MyPath

FName = Application.GetOpenFilename(filefilter:="csv Files (*.csv),
*.csv", _
MultiSelect:=True)
If IsArray(FName) Then
Application.ScreenUpdating = False
Set basebook = ThisWorkbook
rnum = 1
basebook.Worksheets(1).Cells.Clear
'clear all cells on the first sheet

For N = LBound(FName) To UBound(FName)
Set mybook = Workbooks.Open(FName(N))
Set sourceRange = mybook.Worksheets(1).Range("A28:B128")
SourceRcount = sourceRange.Rows.Count
Set destrange = basebook.Worksheets(1).Cells(rnum, "B")

basebook.Worksheets(1).Cells(rnum, "A").Value = mybook.Name
' This will add the workbook name in column D if you want

sourceRange.Copy destrange
' Instead of this line you can use the code below to copy only
the values

' With sourceRange
' Set destrange = basebook.Worksheets(1).Cells(rnum,
"A"). _
' Resize(.Rows.Count, .Columns.Count)
' End With
' destrange.Value = sourceRange.Value

mybook.Close False
rnum = rnum + SourceRcount
Next
End If
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default opening order macro for files in a folder

Hi Sanbitter Man

You can fill a array with the file names you select and sort the array
On my ADO page there is a example and function
http://www.rondebruin.nl/ado.htm#files


--
Regards Ron De Bruin
http://www.rondebruin.nl



"Sanbitter Man" <Sanbitter wrote in message
...
I've setup a pretty neat macro that opens 50+ csv file and then copies the
range (A28:B128) consecutively underneath each other but I'm worried that I
have no control over the order in which these files are being opened/inserted
underneath each other...I want to choose the opening order based on the file
name only (either a-z or z-a)..knowing how to order an open by date is also
very helpful to me....thank you in advance......Here's the opening macro I've
been using:

Sub monthly_reports_opens_change_dir_()
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim SourceRcount As Long
Dim N As Long
Dim rnum As Long
Dim MyPath As String
Dim SaveDriveDir As String
Dim FName As Variant

SaveDriveDir = CurDir
MyPath = "C:\Documents and Settings\May 2006"
ChDrive MyPath
ChDir MyPath

FName = Application.GetOpenFilename(filefilter:="csv Files (*.csv),
*.csv", _
MultiSelect:=True)
If IsArray(FName) Then
Application.ScreenUpdating = False
Set basebook = ThisWorkbook
rnum = 1
basebook.Worksheets(1).Cells.Clear
'clear all cells on the first sheet

For N = LBound(FName) To UBound(FName)
Set mybook = Workbooks.Open(FName(N))
Set sourceRange = mybook.Worksheets(1).Range("A28:B128")
SourceRcount = sourceRange.Rows.Count
Set destrange = basebook.Worksheets(1).Cells(rnum, "B")

basebook.Worksheets(1).Cells(rnum, "A").Value = mybook.Name
' This will add the workbook name in column D if you want

sourceRange.Copy destrange
' Instead of this line you can use the code below to copy only
the values

' With sourceRange
' Set destrange = basebook.Worksheets(1).Cells(rnum,
"A"). _
' Resize(.Rows.Count, .Columns.Count)
' End With
' destrange.Value = sourceRange.Value

mybook.Close False
rnum = rnum + SourceRcount
Next
End If
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub




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
Default folder for opening/saving files Dino Excel Discussion (Misc queries) 2 July 5th 07 11:28 PM
"Folder Select" Dialogue - Opening multiple files from selected folder Rob[_26_] Excel Programming 2 September 30th 05 02:47 PM
Opening Files from a folder in sorted order. Excel Beginner Excel Programming 2 March 15th 05 03:35 PM
Opening files from a variable (todays' date) folder name Hari[_3_] Excel Programming 2 June 10th 04 04:03 PM
Opening all txt files in a folder and saving as excel Jason Excel Programming 1 February 23rd 04 09:01 PM


All times are GMT +1. The time now is 03:38 AM.

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

About Us

"It's about Microsoft Excel"