Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 336
Default VBA for Importing Text Files

My Current VBA for import test files are as follow, how can I select mutiple
files at one time instead of clicking one by one? Ctrl and Shift button does
not work in this. Thanks.

Sub Import()

Dim myFileName As Variant

Do

myFileName = Application.GetOpenFilename( _
filefilter:="Text Files, *.Txt", Title:="Locate & Select
the Reel No.")
If myFileName = False Then
MsgBox "End of Import. Copy Worksheet to Smartscope Summary.xls"
'user hit cancel
Else
With ActiveSheet.QueryTables.Add( _
Connection:="TEXT;" & myFileName, _
Destination:=Range("A1"))

.Name = "smartscope"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If

Loop Until myFileName = False




End Sub



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default VBA for Importing Text Files

Martin,
Check the help for the MultiSelect argument to GetOpenFilename .
Note that you get an array returned, that you can then loop through.

NickHK

"Martin" wrote in message
...
My Current VBA for import test files are as follow, how can I select

mutiple
files at one time instead of clicking one by one? Ctrl and Shift button

does
not work in this. Thanks.

Sub Import()

Dim myFileName As Variant

Do

myFileName = Application.GetOpenFilename( _
filefilter:="Text Files, *.Txt", Title:="Locate &

Select
the Reel No.")
If myFileName = False Then
MsgBox "End of Import. Copy Worksheet to Smartscope

Summary.xls"
'user hit cancel
Else
With ActiveSheet.QueryTables.Add( _
Connection:="TEXT;" & myFileName, _
Destination:=Range("A1"))

.Name = "smartscope"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If

Loop Until myFileName = False




End Sub





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 336
Default VBA for Importing Text Files

I can select multiple files now but nothing was imported. Whats wrong? The
code is a follows:

Sub Import()

Dim myFileName As Variant

Do

fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt),*.txt", , , , True)
If myFileName = False Then
MsgBox "End of Import. Copy Worksheet to Smartscope Summary.xls"
'user hit cancel
Else
With ActiveSheet.QueryTables.Add( _
Connection:="TEXT;" & myFileName, _
Destination:=Range("A1"))

.Name = "smartscope"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If

Loop Until myFileName = False




End Sub



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default VBA for Importing Text Files

martin,
What is "fileToOpen" ? You mean myFileName

fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt),*.txt", , , , True)
If myFileName = False Then

Do you mean:
myFileName = Application.GetOpenFilename("Text Files (*.txt),*.txt", , , ,
True)
If myFileName = False Then

Also, as I said, this will return an array that you need to loop through.
Dim i as long
for i=lbound(myFileName) to ubound(myFileName)
With ActiveSheet.QueryTables.Add( _
Connection:="TEXT;" & myFileName(i), _
Destination:=Range("A1"))
'etc....

And remove your current Do..Loop

This puts all the querytables onto of each other. Is that what you want ?

NickHK

"Martin" wrote in message
...
I can select multiple files now but nothing was imported. Whats wrong? The
code is a follows:

Sub Import()

Dim myFileName As Variant

Do

fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt),*.txt", , , , True)
If myFileName = False Then
MsgBox "End of Import. Copy Worksheet to Smartscope

Summary.xls"
'user hit cancel
Else
With ActiveSheet.QueryTables.Add( _
Connection:="TEXT;" & myFileName, _
Destination:=Range("A1"))

.Name = "smartscope"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If

Loop Until myFileName = False




End Sub





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 336
Default VBA for Importing Text Files

I try tomorrow. Going off now. Thanks.

"NickHK" wrote:

martin,
What is "fileToOpen" ? You mean myFileName

fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt),*.txt", , , , True)
If myFileName = False Then

Do you mean:
myFileName = Application.GetOpenFilename("Text Files (*.txt),*.txt", , , ,
True)
If myFileName = False Then

Also, as I said, this will return an array that you need to loop through.
Dim i as long
for i=lbound(myFileName) to ubound(myFileName)
With ActiveSheet.QueryTables.Add( _
Connection:="TEXT;" & myFileName(i), _
Destination:=Range("A1"))
'etc....

And remove your current Do..Loop

This puts all the querytables onto of each other. Is that what you want ?

NickHK

"Martin" wrote in message
...
I can select multiple files now but nothing was imported. Whats wrong? The
code is a follows:

Sub Import()

Dim myFileName As Variant

Do

fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt),*.txt", , , , True)
If myFileName = False Then
MsgBox "End of Import. Copy Worksheet to Smartscope

Summary.xls"
'user hit cancel
Else
With ActiveSheet.QueryTables.Add( _
Connection:="TEXT;" & myFileName, _
Destination:=Range("A1"))

.Name = "smartscope"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If

Loop Until myFileName = False




End Sub








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 336
Default VBA for Importing Text Files

Hi Nick,

I got the error "For without next". My code according to your suggestion a

Sub Import()

Dim myFileName As Variant

myFileName = Application.GetOpenFilename("Text Files (*.txt),*.txt",
, , , True)
If myFileName = False Then

Dim i As Long
For i = LBound(myFileName) To UBound(myFileName)
With ActiveSheet.QueryTables.Add( _
Connection:="TEXT;" & myFileName, _
Destination:=Range("A1"))


.Name = "smartscope"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

End Sub

"NickHK" wrote:

martin,
What is "fileToOpen" ? You mean myFileName

fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt),*.txt", , , , True)
If myFileName = False Then

Do you mean:
myFileName = Application.GetOpenFilename("Text Files (*.txt),*.txt", , , ,
True)
If myFileName = False Then

Also, as I said, this will return an array that you need to loop through.
Dim i as long
for i=lbound(myFileName) to ubound(myFileName)
With ActiveSheet.QueryTables.Add( _
Connection:="TEXT;" & myFileName(i), _
Destination:=Range("A1"))
'etc....

And remove your current Do..Loop

This puts all the querytables onto of each other. Is that what you want ?

NickHK

"Martin" wrote in message
...
I can select multiple files now but nothing was imported. Whats wrong? The
code is a follows:

Sub Import()

Dim myFileName As Variant

Do

fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt),*.txt", , , , True)
If myFileName = False Then
MsgBox "End of Import. Copy Worksheet to Smartscope

Summary.xls"
'user hit cancel
Else
With ActiveSheet.QueryTables.Add( _
Connection:="TEXT;" & myFileName, _
Destination:=Range("A1"))

.Name = "smartscope"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If

Loop Until myFileName = False




End Sub






  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default VBA for Importing Text Files

Martin,
Yes, the error message is telling you what is wrong; add a Next after your
"End with" to complete the loop.
Also, note that myFileName is an array, so you have to address its elements
as myFileName(i).

NickHK

"Martin" wrote in message
...
Hi Nick,

I got the error "For without next". My code according to your suggestion

a

Sub Import()

Dim myFileName As Variant

myFileName = Application.GetOpenFilename("Text Files

(*.txt),*.txt",
, , , True)
If myFileName = False Then

Dim i As Long
For i = LBound(myFileName) To UBound(myFileName)
With ActiveSheet.QueryTables.Add( _
Connection:="TEXT;" & myFileName, _
Destination:=Range("A1"))


.Name = "smartscope"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

End Sub

"NickHK" wrote:

martin,
What is "fileToOpen" ? You mean myFileName

fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt),*.txt", , , , True)
If myFileName = False Then

Do you mean:
myFileName = Application.GetOpenFilename("Text Files (*.txt),*.txt", , ,

,
True)
If myFileName = False Then

Also, as I said, this will return an array that you need to loop

through.
Dim i as long
for i=lbound(myFileName) to ubound(myFileName)
With ActiveSheet.QueryTables.Add( _
Connection:="TEXT;" & myFileName(i), _
Destination:=Range("A1"))
'etc....

And remove your current Do..Loop

This puts all the querytables onto of each other. Is that what you want

?

NickHK

"Martin" wrote in message
...
I can select multiple files now but nothing was imported. Whats wrong?

The
code is a follows:

Sub Import()

Dim myFileName As Variant

Do

fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt),*.txt", , , , True)
If myFileName = False Then
MsgBox "End of Import. Copy Worksheet to Smartscope

Summary.xls"
'user hit cancel
Else
With ActiveSheet.QueryTables.Add( _
Connection:="TEXT;" & myFileName, _
Destination:=Range("A1"))

.Name = "smartscope"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If

Loop Until myFileName = False




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
Importing text files automaticly. Uradox Excel Discussion (Misc queries) 5 November 7th 07 02:57 AM
Importing text-files GARY Excel Discussion (Misc queries) 6 December 13th 06 02:57 PM
Importing Text Files smith_gw Excel Discussion (Misc queries) 1 May 5th 05 10:42 PM
Importing text files Dominique Feteau[_2_] Excel Programming 1 December 16th 04 12:25 PM
importing text files msweeney Excel Programming 3 September 24th 03 01:49 AM


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