LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Importing Multiple Text Files

I have a Workbook (hereinafter called Workbook 1) in which I want the user to
be able to search for 4 tab-delimited text files and import them, each as a
separate worksheet.

So far, I've been able to use a couple of macros (downloaded from the
Internet - see below).

The first macro will allow the user to browse for the text files and combine
them into a single NEW workbook (hereinafter called Workbook 2) where each
text file is in a separate worksheet.

I then have to have the user save and close Workbook 2 - I have no macro for
this; I just have to give the user instructions to save and close Workbook 2
I've found this saving and closing of Workbook 2 to be essential, otherwise
it seems to create problems with filenames when repeating the operations.

The second macro then allows the user to browse for Workbook 2 and import
its worksheets into Workbook 1.

But what I'd really like is for the above two operations to be combined into
one. In other words, for the user to search for the text files and import
them directly into Workbook 1.

I'm not that familiar with VB but am okay with recording macros. Any help
would be sincerely appreciated. Here are the macros I've been referring to:

First Macro; Getting and combining text files into Workbook 2:
Sub A_CombineTextFiles()

' Macro obtained by Felicity Shagwell
' on May 28 2007 from:
'
http://exceltips.vitalnews.com/Pages...orkboo k.html

Dim FilesToOpen
Dim x As Integer
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim sDelimiter As String

On Error GoTo ErrHandler
Application.ScreenUpdating = False

sDelimiter = "|"

FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Text Files (*.txt), *.txt", _
MultiSelect:=True, Title:="Browse and Select Text Files to Open")

If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If

x = 1
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
wkbTemp.Sheets(1).Copy
Set wkbAll = ActiveWorkbook
wkbTemp.Close (False)
wkbAll.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=False, Space:=False, _
Other:=True, OtherChar:="|"
x = x + 1

While x <= UBound(FilesToOpen)
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
With wkbAll
wkbTemp.Sheets(1).Move After:=.Sheets(.Sheets.Count)
.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=False, Space:=False, _
Other:=True, OtherChar:=sDelimiter
End With
x = x + 1
Wend

ExitHandler:
Application.ScreenUpdating = True
Set wkbAll = Nothing
Set wkbTemp = Nothing
Exit Sub

ErrHandler:
MsgBox Err.Description
Resume ExitHandler

End Sub

Second macro; importing worksheets from Workbook 2 into Workbook 1:
Sub B_CombineWorkbooks()

'Obtained by Felicity Shagwell on May 29 2007 from"
'http://exceltips.vitalnews.com/Pages/T1123_Merging_Many_Workbooks.html

Dim FilesToOpen
Dim x As Integer

On Error GoTo ErrHandler
Application.ScreenUpdating = False

FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Microsoft Excel Files (*.xls), *.xls", _
MultiSelect:=True, Title:="Files to Merge")

If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If

x = 1
While x <= UBound(FilesToOpen)
Workbooks.Open Filename:=FilesToOpen(x)
Sheets().Move After:=ThisWorkbook.Sheets _
(ThisWorkbook.Sheets.Count)
x = x + 1
Wend

ExitHandler:
Application.ScreenUpdating = True
Exit Sub

ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub





 
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
Importing multiple text files to worksheets josnah[_3_] Excel Programming 4 June 3rd 06 11:44 AM
Importing multiple Text files into Excel [email protected] Excel Programming 2 April 9th 06 01:35 PM
Importing from multiple text files tmort[_15_] Excel Programming 1 November 8th 05 01:42 PM
importing multiple text files into the same worksheet Mike D Excel Discussion (Misc queries) 4 July 15th 05 10:39 AM
importing multiple text files??? tashayu Excel Discussion (Misc queries) 0 December 19th 04 02:43 PM


All times are GMT +1. The time now is 11:03 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"