You do this loop 1000 times--no matter if you open it the first time through or
not...
You could still loop through the 1000 numbers and exit after you've done the
import...
for i = 1 to 9999
on error resume next
workbooks.open(.....)
if err.number <> 0 then
err.clear
'but stay in the loop
else
'but if there wasn't an error, you found it
exit for
end if
next i
But I think it would be better to just look for a file named DEAE????.Txt. But
I'm confused about your post and your code.
It looks like you describe the file as having 4 numeric characters
(DEAE****.txt), but I may be reading too much into that. But your code looks
for files named DEAE1.txt, DEAE2.txt.
I'm gonna assume your code is the way you want it.
Option Explicit
Sub testme()
Dim myPath As String
Dim myFile As String
Dim FoundOne As Boolean
myPath = "C:\my documents\excel\test\"
If Right(myPath, 1) <> "\" Then
myPath = myPath & "\"
End If
myFile = ""
On Error Resume Next
myFile = Dir(myPath & "DEAE*.txt")
On Error GoTo 0
If myFile = "" Then
MsgBox "no files found"
Exit Sub
End If
FoundOne = False
Do While myFile <> ""
If IsNumeric(Mid(myFile, 5, Len(myFile) - 8)) Then
'found one that's numeric
FoundOne = True
Exit Do
End If
myFile = Dir()
Loop
If FoundOne = False Then
MsgBox "no files found"
Else
Workbooks.OpenText Filename:=myPath & myFile, _
Origin:=xlWindows, StartRow:=1, _
DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, _
Space:=False, Other:=True, OtherChar:="|", _
FieldInfo:=Array(Array(1, 1), Array(2, 1), _
Array(3, 1), Array(4, 1), Array(5, 1), _
Array(6, 1), Array(7, 1), Array(8, 1))
End If
End Sub
carloshernandezy wrote:
>
> Opening a *.txt with a variable name is to slow, the name has varible
> string like DEAE****.txt where the **** is a number between 0000 and
> 9999. There is only one archive with this variable name in the DIR. I
> use this code but it is too slowly, what can I do.
> __________________________________________________________________
> For i = 1 To 9999
>
> On Error Resume Next
> Workbooks.OpenText Filename:="DEAE" & i & ".txt", Origin:= _
> xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier:=
> _
> xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True,
> Semicolon:=False, _
> Comma:=True, Space:=False, Other:=True, OtherChar:="|",
> FieldInfo:= _
> Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1),
> Array(5, 1), Array(6, 1), Array(7 _
> , 1), Array(8, 1))
>
> Next i
> __________________________________________________________________
>
> Thanks to all
--
Dave Peterson
Bookmarks