View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
NickHK NickHK is offline
external usenet poster
 
Posts: 4,391
Default Runtime Error 13 help

Paul,
You sure about that ?

<From Help
fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen < False Then
MsgBox "Open " & fileToOpen
End If
</From Help

And in the Immediate window, pressing Cancel:
?typename(Application.GetOpenFilename("Text Files (*.txt),*.txt", 0, 0, 0,
True))
Boolean

Martin,
Whilst you are testing if the user cancelled, you are not doing anything
about it e.g. exitting the sub. So you trying to loop on a boolean value,
hence the type mismatch.
Use:
If myFileName = False Then Exit Sub
and delete the End If towards the bottom of you routine.

NickHK


wrote in message
oups.com...
On Feb 1, 9:21 am, Martin wrote:
I got the Runtime Error 13 Type mismatch error when I modified my VBA to

the
following in order to select multiple files to be imported. What should

be
the correct code? Thanks.

Sub Import()

Dim myFileName As Variant

myFileName = Application.GetOpenFilename("Text Files

(*.txt),*.txt",
0, 0, 0, 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
Next
End If
End Sub


Hi
You might need
If myFileName = "False"

myFileName returns the text "False" if Cancel is pressed, not the
Boolean FALSE.
regards
Paul