Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Flling an array

I need to fill an array with all (variabel) excel files in a specific
folder.
Example: Folder = d:\temp contains file1.xls, file2.xls etc
In stead of typing array("file1.xls", file2.xls", etc) the array must be
filled with the VBA code.
How can I do this.

Kind regards
Adri


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Flling an array

Hi Adri

Use Getopenfilename to do this

Sub testing()
Dim FName As Variant
Dim N As Long
FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xls), *.xls", _
MultiSelect:=True)

If IsArray(FName) Then
For N = LBound(FName) To UBound(FName)
Workbooks.Open (FName(N))
Next
End If
End Sub



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


"Adri" wrote in message ...
I need to fill an array with all (variabel) excel files in a specific
folder.
Example: Folder = d:\temp contains file1.xls, file2.xls etc
In stead of typing array("file1.xls", file2.xls", etc) the array must be
filled with the VBA code.
How can I do this.

Kind regards
Adri




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 93
Default Flling an array

This function will return an array containing all the XLS files from a given folder

Function GetXLSFiles(ByVal Folder As String) As Varian
Set fso = CreateObject("Scripting.FileSystemObject"
a = "
For Each file In fso.getfolder(Folder).File
If fso.getextensionname(file) = "xls" The
a = a & file.Name & ",
End I
Nex
Set fso = Nothin
a = Left(a, Len(a) - 1
GetXLSFiles = Split(a, ","
End Functio

CA The array that is returned use option base 0 --irrecpective of the global setting. You might need to redimension it to comply with the global setting.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Flling an array

Hi Ron,
Thans you for answering but I don't understand how I can use it solves my
problem.
In my code I have this line:

myWorkbooks = Array("file1.xls", "file2.xls", "file4.xls")

How can Array automaticly be filled with all files from folder D:\temp
(preferred not opening all files first)?

Kind regards, Adri


Ron de Bruin wrote:
| Hi Adri
|
| Use Getopenfilename to do this
|
| Sub testing()
| Dim FName As Variant
| Dim N As Long
| FName = Application.GetOpenFilename(filefilter:="Excel Files
| (*.xls), *.xls", _ MultiSelect:=True)
|
| If IsArray(FName) Then
| For N = LBound(FName) To UBound(FName)
| Workbooks.Open (FName(N))
| Next
| End If
| End Sub
|
|
|
|
| "Adri" wrote in message
| ...
|| I need to fill an array with all (variabel) excel files in a specific
|| folder.
|| Example: Folder = d:\temp contains file1.xls, file2.xls etc
|| In stead of typing array("file1.xls", file2.xls", etc) the array
|| must be filled with the VBA code.
|| How can I do this.
||
|| Kind regards
|| Adri


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

Hi Adri

You can select the files you want with the CTRL key down
If you use CTRL-A in the dialog you select all the files

Fname is the Array of the selected files

You can loop through the files in the Array with this
For N = LBound(FName) To UBound(FName)

I use open the files as a example but you can do what you want.

What do you want to do with the files in the Array?
Maybe we can give you a better answer then


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


"Adri" wrote in message ...
Hi Ron,
Thans you for answering but I don't understand how I can use it solves my
problem.
In my code I have this line:

myWorkbooks = Array("file1.xls", "file2.xls", "file4.xls")

How can Array automaticly be filled with all files from folder D:\temp
(preferred not opening all files first)?

Kind regards, Adri


Ron de Bruin wrote:
| Hi Adri
|
| Use Getopenfilename to do this
|
| Sub testing()
| Dim FName As Variant
| Dim N As Long
| FName = Application.GetOpenFilename(filefilter:="Excel Files
| (*.xls), *.xls", _ MultiSelect:=True)
|
| If IsArray(FName) Then
| For N = LBound(FName) To UBound(FName)
| Workbooks.Open (FName(N))
| Next
| End If
| End Sub
|
|
|
|
| "Adri" wrote in message
| ...
|| I need to fill an array with all (variabel) excel files in a specific
|| folder.
|| Example: Folder = d:\temp contains file1.xls, file2.xls etc
|| In stead of typing array("file1.xls", file2.xls", etc) the array
|| must be filled with the VBA code.
|| How can I do this.
||
|| Kind regards
|| Adri






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Flling an array

Ron de Bruin wrote:
| Hi Adri
|
| You can select the files you want with the CTRL key down
| If you use CTRL-A in the dialog you select all the files
|
| Fname is the Array of the selected files
|
| You can loop through the files in the Array with this
| For N = LBound(FName) To UBound(FName)
|
| I use open the files as a example but you can do what you want.
|
| What do you want to do with the files in the Array?
| Maybe we can give you a better answer then
|
Hi Ron,
I have this code (from the internet). It copies the contence of the seprate
files to one workbook .
To prevent a lot of typing (the file are at a network server with long
names) I want to use the macro to fill the array.
I hope you understand the problem now.


Option Explicit
Sub GetData()

Dim myWorkbooks As Variant
Dim myFolder As String
Dim testStr As String
Dim newWkbk As Workbook
Dim tempWkbk As Workbook
Dim wCtr As Long

myFolder = "D:\Temp"
If Right(myFolder, 1) < "\" Then
myFolder = myFolder & "\"
End If

Set newWkbk = Workbooks.Add(1)
ActiveSheet.Name = "DummyToDelete"

myWorkbooks = Array("file1.xls", "file2.xls", "file4.xls")

For wCtr = LBound(myWorkbooks) To UBound(myWorkbooks)
testStr = ""
On Error Resume Next
testStr = Dir(myFolder & myWorkbooks(wCtr))
On Error GoTo 0
If testStr = "" Then
MsgBox myWorkbooks(wCtr) & " Is missing!" & vbLf & _
"Processing stopped"
Exit Sub
End If

Set tempWkbk = Workbooks.Open(Filename:=myFolder &
myWorkbooks(wCtr), _
ReadOnly:=True)

tempWkbk.Worksheets(1).Copy _
after:=newWkbk.Worksheets(newWkbk.Worksheets.Count )

tempWkbk.Close savechanges:=False

Next wCtr

Application.DisplayAlerts = False
newWkbk.Worksheets("DummyToDelete").Delete
Application.DisplayAlerts = True

Call RenSheets
End Sub


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Flling an array

Hi Adri

Look at this examples first
http://www.rondebruin.nl/copy3.htm


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


"Adri" wrote in message ...
Ron de Bruin wrote:
| Hi Adri
|
| You can select the files you want with the CTRL key down
| If you use CTRL-A in the dialog you select all the files
|
| Fname is the Array of the selected files
|
| You can loop through the files in the Array with this
| For N = LBound(FName) To UBound(FName)
|
| I use open the files as a example but you can do what you want.
|
| What do you want to do with the files in the Array?
| Maybe we can give you a better answer then
|
Hi Ron,
I have this code (from the internet). It copies the contence of the seprate
files to one workbook .
To prevent a lot of typing (the file are at a network server with long
names) I want to use the macro to fill the array.
I hope you understand the problem now.


Option Explicit
Sub GetData()

Dim myWorkbooks As Variant
Dim myFolder As String
Dim testStr As String
Dim newWkbk As Workbook
Dim tempWkbk As Workbook
Dim wCtr As Long

myFolder = "D:\Temp"
If Right(myFolder, 1) < "\" Then
myFolder = myFolder & "\"
End If

Set newWkbk = Workbooks.Add(1)
ActiveSheet.Name = "DummyToDelete"

myWorkbooks = Array("file1.xls", "file2.xls", "file4.xls")

For wCtr = LBound(myWorkbooks) To UBound(myWorkbooks)
testStr = ""
On Error Resume Next
testStr = Dir(myFolder & myWorkbooks(wCtr))
On Error GoTo 0
If testStr = "" Then
MsgBox myWorkbooks(wCtr) & " Is missing!" & vbLf & _
"Processing stopped"
Exit Sub
End If

Set tempWkbk = Workbooks.Open(Filename:=myFolder &
myWorkbooks(wCtr), _
ReadOnly:=True)

tempWkbk.Worksheets(1).Copy _
after:=newWkbk.Worksheets(newWkbk.Worksheets.Count )

tempWkbk.Close savechanges:=False

Next wCtr

Application.DisplayAlerts = False
newWkbk.Worksheets("DummyToDelete").Delete
Application.DisplayAlerts = True

Call RenSheets
End Sub




  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Flling an array

Ron, thank you very much
TestFile6() wil do the job for me.
Lats little request. How can the sheetnames get a name without .xls
Adri


Ron de Bruin wrote:
| Hi Adri
|
| Look at this examples first
| http://www.rondebruin.nl/copy3.htm
|
|
|
| "Adri" wrote in message
| ...
|| Ron de Bruin wrote:
||| Hi Adri
|||
||| You can select the files you want with the CTRL key down
||| If you use CTRL-A in the dialog you select all the files
|||
||| Fname is the Array of the selected files
|||
||| You can loop through the files in the Array with this
||| For N = LBound(FName) To UBound(FName)
|||
||| I use open the files as a example but you can do what you want.
|||
||| What do you want to do with the files in the Array?
||| Maybe we can give you a better answer then
|||
|| Hi Ron,
|| I have this code (from the internet). It copies the contence of the
|| seprate files to one workbook .
|| To prevent a lot of typing (the file are at a network server with
|| long names) I want to use the macro to fill the array.
|| I hope you understand the problem now.
||
||
|| Option Explicit
|| Sub GetData()
||
|| Dim myWorkbooks As Variant
|| Dim myFolder As String
|| Dim testStr As String
|| Dim newWkbk As Workbook
|| Dim tempWkbk As Workbook
|| Dim wCtr As Long
||
|| myFolder = "D:\Temp"
|| If Right(myFolder, 1) < "\" Then
|| myFolder = myFolder & "\"
|| End If
||
|| Set newWkbk = Workbooks.Add(1)
|| ActiveSheet.Name = "DummyToDelete"
||
|| myWorkbooks = Array("file1.xls", "file2.xls", "file4.xls")
||
|| For wCtr = LBound(myWorkbooks) To UBound(myWorkbooks)
|| testStr = ""
|| On Error Resume Next
|| testStr = Dir(myFolder & myWorkbooks(wCtr))
|| On Error GoTo 0
|| If testStr = "" Then
|| MsgBox myWorkbooks(wCtr) & " Is missing!" & vbLf & _
|| "Processing stopped"
|| Exit Sub
|| End If
||
|| Set tempWkbk = Workbooks.Open(Filename:=myFolder &
|| myWorkbooks(wCtr), _
|| ReadOnly:=True)
||
|| tempWkbk.Worksheets(1).Copy _
|| after:=newWkbk.Worksheets(newWkbk.Worksheets.Count )
||
|| tempWkbk.Close savechanges:=False
||
|| Next wCtr
||
|| Application.DisplayAlerts = False
|| newWkbk.Worksheets("DummyToDelete").Delete
|| Application.DisplayAlerts = True
||
|| Call RenSheets
|| End Sub


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Flling an array

Try this

ActiveSheet.Name = Left(mybook.Name, Len(mybook.Name) - 4)


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


"Adri" wrote in message ...
Ron, thank you very much
TestFile6() wil do the job for me.
Lats little request. How can the sheetnames get a name without .xls
Adri


Ron de Bruin wrote:
| Hi Adri
|
| Look at this examples first
| http://www.rondebruin.nl/copy3.htm
|
|
|
| "Adri" wrote in message
| ...
|| Ron de Bruin wrote:
||| Hi Adri
|||
||| You can select the files you want with the CTRL key down
||| If you use CTRL-A in the dialog you select all the files
|||
||| Fname is the Array of the selected files
|||
||| You can loop through the files in the Array with this
||| For N = LBound(FName) To UBound(FName)
|||
||| I use open the files as a example but you can do what you want.
|||
||| What do you want to do with the files in the Array?
||| Maybe we can give you a better answer then
|||
|| Hi Ron,
|| I have this code (from the internet). It copies the contence of the
|| seprate files to one workbook .
|| To prevent a lot of typing (the file are at a network server with
|| long names) I want to use the macro to fill the array.
|| I hope you understand the problem now.
||
||
|| Option Explicit
|| Sub GetData()
||
|| Dim myWorkbooks As Variant
|| Dim myFolder As String
|| Dim testStr As String
|| Dim newWkbk As Workbook
|| Dim tempWkbk As Workbook
|| Dim wCtr As Long
||
|| myFolder = "D:\Temp"
|| If Right(myFolder, 1) < "\" Then
|| myFolder = myFolder & "\"
|| End If
||
|| Set newWkbk = Workbooks.Add(1)
|| ActiveSheet.Name = "DummyToDelete"
||
|| myWorkbooks = Array("file1.xls", "file2.xls", "file4.xls")
||
|| For wCtr = LBound(myWorkbooks) To UBound(myWorkbooks)
|| testStr = ""
|| On Error Resume Next
|| testStr = Dir(myFolder & myWorkbooks(wCtr))
|| On Error GoTo 0
|| If testStr = "" Then
|| MsgBox myWorkbooks(wCtr) & " Is missing!" & vbLf & _
|| "Processing stopped"
|| Exit Sub
|| End If
||
|| Set tempWkbk = Workbooks.Open(Filename:=myFolder &
|| myWorkbooks(wCtr), _
|| ReadOnly:=True)
||
|| tempWkbk.Worksheets(1).Copy _
|| after:=newWkbk.Worksheets(newWkbk.Worksheets.Count )
||
|| tempWkbk.Close savechanges:=False
||
|| Next wCtr
||
|| Application.DisplayAlerts = False
|| newWkbk.Worksheets("DummyToDelete").Delete
|| Application.DisplayAlerts = True
||
|| Call RenSheets
|| End Sub




  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Flling an array

Ron, perfect
I used Left(naam, Len(naam) - 4) and that didn't work.
Now I know why.
Thank you, learned a lot today.
Kind regards, Adri

Ron de Bruin wrote:
| Try this
|
| ActiveSheet.Name = Left(mybook.Name, Len(mybook.Name) - 4)
|
|
|
| "Adri" wrote in message
| ...
|| Ron, thank you very much
|| TestFile6() wil do the job for me.
|| Lats little request. How can the sheetnames get a name without .xls
|| Adri
||
||
|| Ron de Bruin wrote:
||| Hi Adri
|||
||| Look at this examples first
||| http://www.rondebruin.nl/copy3.htm
|||
|||
|||
||| "Adri" wrote in message
||| ...
|||| Ron de Bruin wrote:
||||| Hi Adri
|||||
||||| You can select the files you want with the CTRL key down
||||| If you use CTRL-A in the dialog you select all the files
|||||
||||| Fname is the Array of the selected files
|||||
||||| You can loop through the files in the Array with this
||||| For N = LBound(FName) To UBound(FName)
|||||
||||| I use open the files as a example but you can do what you want.
|||||
||||| What do you want to do with the files in the Array?
||||| Maybe we can give you a better answer then
|||||
|||| Hi Ron,
|||| I have this code (from the internet). It copies the contence of the
|||| seprate files to one workbook .
|||| To prevent a lot of typing (the file are at a network server with
|||| long names) I want to use the macro to fill the array.
|||| I hope you understand the problem now.
||||
||||
|||| Option Explicit
|||| Sub GetData()
||||
|||| Dim myWorkbooks As Variant
|||| Dim myFolder As String
|||| Dim testStr As String
|||| Dim newWkbk As Workbook
|||| Dim tempWkbk As Workbook
|||| Dim wCtr As Long
||||
|||| myFolder = "D:\Temp"
|||| If Right(myFolder, 1) < "\" Then
|||| myFolder = myFolder & "\"
|||| End If
||||
|||| Set newWkbk = Workbooks.Add(1)
|||| ActiveSheet.Name = "DummyToDelete"
||||
|||| myWorkbooks = Array("file1.xls", "file2.xls", "file4.xls")
||||
|||| For wCtr = LBound(myWorkbooks) To UBound(myWorkbooks)
|||| testStr = ""
|||| On Error Resume Next
|||| testStr = Dir(myFolder & myWorkbooks(wCtr))
|||| On Error GoTo 0
|||| If testStr = "" Then
|||| MsgBox myWorkbooks(wCtr) & " Is missing!" & vbLf & _
|||| "Processing stopped"
|||| Exit Sub
|||| End If
||||
|||| Set tempWkbk = Workbooks.Open(Filename:=myFolder &
|||| myWorkbooks(wCtr), _
|||| ReadOnly:=True)
||||
|||| tempWkbk.Worksheets(1).Copy _
|||| after:=newWkbk.Worksheets(newWkbk.Worksheets.Count )
||||
|||| tempWkbk.Close savechanges:=False
||||
|||| Next wCtr
||||
|||| Application.DisplayAlerts = False
|||| newWkbk.Worksheets("DummyToDelete").Delete
|||| Application.DisplayAlerts = True
||||
|||| Call RenSheets
|||| End Sub




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Flling an array

You are welcome


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


"Adri" wrote in message ...
Ron, perfect
I used Left(naam, Len(naam) - 4) and that didn't work.
Now I know why.
Thank you, learned a lot today.
Kind regards, Adri

Ron de Bruin wrote:
| Try this
|
| ActiveSheet.Name = Left(mybook.Name, Len(mybook.Name) - 4)
|
|
|
| "Adri" wrote in message
| ...
|| Ron, thank you very much
|| TestFile6() wil do the job for me.
|| Lats little request. How can the sheetnames get a name without .xls
|| Adri
||
||
|| Ron de Bruin wrote:
||| Hi Adri
|||
||| Look at this examples first
||| http://www.rondebruin.nl/copy3.htm
|||
|||
|||
||| "Adri" wrote in message
||| ...
|||| Ron de Bruin wrote:
||||| Hi Adri
|||||
||||| You can select the files you want with the CTRL key down
||||| If you use CTRL-A in the dialog you select all the files
|||||
||||| Fname is the Array of the selected files
|||||
||||| You can loop through the files in the Array with this
||||| For N = LBound(FName) To UBound(FName)
|||||
||||| I use open the files as a example but you can do what you want.
|||||
||||| What do you want to do with the files in the Array?
||||| Maybe we can give you a better answer then
|||||
|||| Hi Ron,
|||| I have this code (from the internet). It copies the contence of the
|||| seprate files to one workbook .
|||| To prevent a lot of typing (the file are at a network server with
|||| long names) I want to use the macro to fill the array.
|||| I hope you understand the problem now.
||||
||||
|||| Option Explicit
|||| Sub GetData()
||||
|||| Dim myWorkbooks As Variant
|||| Dim myFolder As String
|||| Dim testStr As String
|||| Dim newWkbk As Workbook
|||| Dim tempWkbk As Workbook
|||| Dim wCtr As Long
||||
|||| myFolder = "D:\Temp"
|||| If Right(myFolder, 1) < "\" Then
|||| myFolder = myFolder & "\"
|||| End If
||||
|||| Set newWkbk = Workbooks.Add(1)
|||| ActiveSheet.Name = "DummyToDelete"
||||
|||| myWorkbooks = Array("file1.xls", "file2.xls", "file4.xls")
||||
|||| For wCtr = LBound(myWorkbooks) To UBound(myWorkbooks)
|||| testStr = ""
|||| On Error Resume Next
|||| testStr = Dir(myFolder & myWorkbooks(wCtr))
|||| On Error GoTo 0
|||| If testStr = "" Then
|||| MsgBox myWorkbooks(wCtr) & " Is missing!" & vbLf & _
|||| "Processing stopped"
|||| Exit Sub
|||| End If
||||
|||| Set tempWkbk = Workbooks.Open(Filename:=myFolder &
|||| myWorkbooks(wCtr), _
|||| ReadOnly:=True)
||||
|||| tempWkbk.Worksheets(1).Copy _
|||| after:=newWkbk.Worksheets(newWkbk.Worksheets.Count )
||||
|||| tempWkbk.Close savechanges:=False
||||
|||| Next wCtr
||||
|||| Application.DisplayAlerts = False
|||| newWkbk.Worksheets("DummyToDelete").Delete
|||| Application.DisplayAlerts = True
||||
|||| Call RenSheets
|||| 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
Complex conditional summing - array COUNT works, array SUM gives#VALUE fatcatfan Excel Worksheet Functions 4 November 18th 09 06:41 PM
Array formula: how to join 2 ranges together to form one array? Rich_84 Excel Worksheet Functions 2 April 1st 09 06:38 PM
Prevent cell/array references from changing when altering/moving thecell/array nme Excel Discussion (Misc queries) 1 September 19th 08 01:53 PM
meaning of : IF(Switch; Average(array A, array B); array A) DXAT Excel Worksheet Functions 1 October 24th 06 06:11 PM
variant array containing cel adresses convert to actual ranges-array Peter[_21_] Excel Programming 5 December 10th 03 09:50 PM


All times are GMT +1. The time now is 07:14 PM.

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"