Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Use VBA code to open a text file with the import wizard

I am wanting to use VBA code to open a text file with the
import wizard. Does anyone know how I would invoke the
import wizard to open a text file in VBA? Any assistance
on this matter would be greatly appreaciated.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 576
Default Use VBA code to open a text file with the import wizard

George,

The following code (borrowed from this forum) will open one or more
text files and convert them to Excel. The text wizard portion is set for
Tab delimited (adjust as needed).

=========================
Sub OpenMyFile()
Dim GetFiles As Variant
Dim iFiles As Long
Dim nFiles As Long
Dim pth As String
Dim wbnm As String

'Open *.txt file and convert to *.xls
' for tab and , delimited text

Application.DisplayAlerts = False
Application.ScreenUpdating = False

pth = ActiveWorkbook.path
ChDir pth
GetFiles = Application.GetOpenFilename _
(FileFilter:="Text Files (*.txt),*.txt", _
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
Workbooks.Open FileName:=GetFiles(iFiles)
Columns("A:A").Select

' text wizard for tab delimited. modify as needed
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited,
_
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False,
Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo
_
:=Array(1, 1)

' saveas *.xls file in same folder
wbnm = ActiveWorkbook.Name
wbnm = Left(wbnm, Len(wbnm) - 4)
ActiveWorkbook.SaveAs FileName:= _
pth & "\" & wbnm & ".xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False _
, CreateBackup:=False

Range("A1").Select
Next
End If

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

--
sb
"George Wilson" wrote in message
...
I am wanting to use VBA code to open a text file with the
import wizard. Does anyone know how I would invoke the
import wizard to open a text file in VBA? Any assistance
on this matter would be greatly appreaciated.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Use VBA code to open a text file with the import wizard

Thanks

-----Original Message-----
George,

The following code (borrowed from this forum) will open

one or more
text files and convert them to Excel. The text wizard

portion is set for
Tab delimited (adjust as needed).

=========================
Sub OpenMyFile()
Dim GetFiles As Variant
Dim iFiles As Long
Dim nFiles As Long
Dim pth As String
Dim wbnm As String

'Open *.txt file and convert to *.xls
' for tab and , delimited text

Application.DisplayAlerts = False
Application.ScreenUpdating = False

pth = ActiveWorkbook.path
ChDir pth
GetFiles = Application.GetOpenFilename _
(FileFilter:="Text Files (*.txt),*.txt", _
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
Workbooks.Open FileName:=GetFiles(iFiles)
Columns("A:A").Select

' text wizard for tab delimited. modify as needed
Selection.TextToColumns Destination:=Range("A1"),

DataType:=xlDelimited,
_
TextQualifier:=xlDoubleQuote,

ConsecutiveDelimiter:=False,
Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False,

Other:=False, FieldInfo
_
:=Array(1, 1)

' saveas *.xls file in same folder
wbnm = ActiveWorkbook.Name
wbnm = Left(wbnm, Len(wbnm) - 4)
ActiveWorkbook.SaveAs FileName:= _
pth & "\" & wbnm & ".xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False _
, CreateBackup:=False

Range("A1").Select
Next
End If

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

--
sb
"George Wilson" wrote in message
...
I am wanting to use VBA code to open a text file with

the
import wizard. Does anyone know how I would invoke the
import wizard to open a text file in VBA? Any assistance
on this matter would be greatly appreaciated.



.

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 to Start Excel in Text Import Wizard for data import rlelvis Setting up and Configuration of Excel 0 July 10th 08 08:40 PM
Help with Text Import Wizard beth Excel Discussion (Misc queries) 1 April 23rd 08 04:30 AM
Text Import Wizard, Step 3 (converting .tsv file to Excel) Eve Excel Worksheet Functions 2 July 21st 07 04:06 PM
Text Import Wizard: Default File Origin Resolution [email protected] Excel Discussion (Misc queries) 0 March 21st 07 05:08 PM
Text Import Wizard: File Origin Problem Baldbloke Excel Discussion (Misc queries) 0 March 29th 06 02:51 PM


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