Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag for Hong Kong

asked on

Problem to open text file

Hi,
I get
User generated imagedue to last line below

Repeat1:
    File0 = InputBox("Enter Text file name to check any comparison:", "Text file")
    
    If InStr(File0, ".") > 0 Then
        'File1 = Mid(File0, 1, InStr(File0, ".") - 1)
        File1 = File0
    Else
        File1 = File0 & ".asc"
    End If
    
    'If Dir(File1 & ".xls") = "" And Dir(File1 & ".xlsm") = "" And Dir(File1 & ".xlsx") = "" Then
    If Dir(File1) = "" Then
        MsgBox "Invalid Text file name is entered!"
        GoTo Repeat1
    End If
    
    Worksheets(Sheet0).Columns(38).ClearContents
    Set Range0 = Worksheets(Sheet0).Range("BL2:BL2")
    Range0.Font.Size = 10
    Range0.Font.Bold = True
    Range0.Font.Color = RGB(192, 80, 77)
    
    RowID = 1: Count0 = 0
    Open File1 For Input As #iFile

Open in new window

after I've put

c:\cmp\0002ASC_COSCO FORTUNE_032E_E_SD_STACO_0002.06187.asc

as file name to the prompt to open this file.

https://1drv.ms/u/s!Ai8CrEskdewXky2ITSgFsh6EUct9
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Open File1 For Input As #iFile
How do you define #iFile ?

quick solution would be:

Open File1 For Input As #1

Open in new window

or try like this:
...
Dim iFile As Integer
iFile = FreeFile

RowID = 1: Count0 = 0
Open File1 For Input As #iFile
...

Open in new window

Maybe you should enclose the filename with "-s, because the filename contains a space, probably Excel tries to open 'c:\cmp\0002ASC_COSCO' as file which fails.
Your first part should be

Do Until File1 <> ""

    File0 = InputBox("Enter Text file name to check any comparison:", "Text file")
    
    If InStr(File0, ".") > 0 Then
        File1 = File0
    Else
        File1 = File0 & ".asc"
    End If
    
    If File1 = "" Then
        MsgBox "Invalid Text file name is entered!"
    End If
Loop

Open in new window

SOLUTION
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Note: this is an Integer data type.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bill Prew
Bill Prew

@HuaMinChen,

I think it would be most helpful if you described what you are trying to accomplish with the code.  A number of experts are 'guessing' at your intention, but it would help to have more info from you.

Is the file you want to "open" going to be an Excel workbook file, or a text file?

Do you want to actually open it in Excel, or just open it as a text file for reading?

It's not clear what you want to happen after you have "access" to the file, so it's hard to advise you on the best code to use.

~bp
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial