Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default How do I combine these two?

Hello. I have 'borrowed' the following code to allow me to select the
text files I would like to open in excel:

Sub Combine()

Dim GetFiles As Variant
Dim iFiles As Long
Dim nFiles As Long
GetFiles = Application.GetOpenFilename _
(FileFilter:="Text Files (*.*),*.*", _
Title:="Select Files To Open", MultiSelect:=True)
If TypeName(GetFiles) = "Boolean" Then
''' GetFiles is False if GetOpenFileName is Canceled
MsgBox "No Files Selected", vbOKOnly, "Nothing Selected"
End
Else
''' GetFiles is Array of Strings (File Names)
''' File names include Path
nFiles = UBound(GetFiles)
For iFiles = 1 To nFiles
'' List Files in Immediate Window
Debug.Print GetFiles(iFiles)
Next
End If
End Sub

I then have 'borrowed' the following code to open files and paste the
sheet into a different worksheet in the same workbook:

Sub Multiple_Text_files()
' Get the list of files
With Application.FileSearch
.NewSearch
.LookIn = "C:\" '<== Alter to proper drive\directory
.SearchSubFolders = False
.fileName = "*.*" '<== All files
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Workbooks.OpenText fileName:=.FoundFiles(i)
Set wkbk = ActiveWorkbook
ActiveSheet.Copy After:=ThisWorkbook.Worksheets(1)
'Note: Thisworkbook refers to the workbook the macro is running
from
wkbk.Close
Next i
Else
MsgBox "There were no files found."
End If

End With
End Sub

So somehow I need to combine the two, which would give the result of
allowing me to select the text files I would like to open in Excel,
and then have the sheet in each text file be copied to a new worksheet
in the 'Master' workbook.

Please help! Thanks in advance!!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default How do I combine these two?

Steve,

Like this

Sub Combine()
Dim GetFiles As Variant
Dim iFiles As Long
Dim nFiles As Long
Dim wkbk As Workbook

GetFiles = Application.GetOpenFilename _
(FileFilter:="Text Files (*.*),*.*", _
Title:="Select Files To Open", MultiSelect:=True)
If TypeName(GetFiles) = "Boolean" Then
''' GetFiles is False if GetOpenFileName is Canceled
MsgBox "No Files Selected", vbOKOnly, "Nothing Selected"
End
Else
''' GetFiles is Array of Strings (File Names)
''' File names include Path
For iFiles = LBound(GetFiles) To UBound(GetFiles)
Workbooks.OpenText Filename:=GetFiles(iFiles)
Set wkbk = ActiveWorkbook
wkbk.ActiveSheet.Copy After:=ThisWorkbook.Worksheets(1)
'Note: Thisworkbook refers to the workbook the macro is
runningfrom
wkbk.Close

Next
End If
End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Steve" wrote in message
om...
Hello. I have 'borrowed' the following code to allow me to select the
text files I would like to open in excel:

Sub Combine()

Dim GetFiles As Variant
Dim iFiles As Long
Dim nFiles As Long
GetFiles = Application.GetOpenFilename _
(FileFilter:="Text Files (*.*),*.*", _
Title:="Select Files To Open", MultiSelect:=True)
If TypeName(GetFiles) = "Boolean" Then
''' GetFiles is False if GetOpenFileName is Canceled
MsgBox "No Files Selected", vbOKOnly, "Nothing Selected"
End
Else
''' GetFiles is Array of Strings (File Names)
''' File names include Path
nFiles = UBound(GetFiles)
For iFiles = 1 To nFiles
'' List Files in Immediate Window
Debug.Print GetFiles(iFiles)
Next
End If
End Sub

I then have 'borrowed' the following code to open files and paste the
sheet into a different worksheet in the same workbook:

Sub Multiple_Text_files()
' Get the list of files
With Application.FileSearch
.NewSearch
.LookIn = "C:\" '<== Alter to proper drive\directory
.SearchSubFolders = False
.fileName = "*.*" '<== All files
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Workbooks.OpenText fileName:=.FoundFiles(i)
Set wkbk = ActiveWorkbook
ActiveSheet.Copy After:=ThisWorkbook.Worksheets(1)
'Note: Thisworkbook refers to the workbook the macro is running
from
wkbk.Close
Next i
Else
MsgBox "There were no files found."
End If

End With
End Sub

So somehow I need to combine the two, which would give the result of
allowing me to select the text files I would like to open in Excel,
and then have the sheet in each text file be copied to a new worksheet
in the 'Master' workbook.

Please help! Thanks in advance!!



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
How do I combine worksheets w/o enough rows to combine? Amanda W. Excel Worksheet Functions 3 June 9th 09 07:26 AM
Combine cells with the same reference and combine quantities brandon Excel Discussion (Misc queries) 2 September 17th 08 05:44 PM
how to combine........ pathan Excel Discussion (Misc queries) 1 August 7th 06 09:39 AM
How Can i combine Monty Excel Discussion (Misc queries) 5 July 25th 06 09:28 PM
How do I combine IF and OR? ana_15825 - ExcelForums.com Excel Worksheet Functions 6 June 30th 05 04:51 AM


All times are GMT +1. The time now is 10:53 AM.

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

About Us

"It's about Microsoft Excel"