Consulting

Results 1 to 14 of 14

Thread: Error msg: File is already opened

  1. #1

    Error msg: File is already opened

    Hi all, Could anyone help me to figure out what's the problem?

    With Activesheet
    sPath = "C:\" 
    For I = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
    sText = .Range("D" & I)
    itext = FreeFile
    sOutput = sPath & Trim(.Name) & ".Txt"
    On Error Resume Next
    Kill sOutput
    On Error GoTo 0
    Open sOutput For Output As #iText <--- It pops up a message telling me that the file is already opened.
    Print #iText, sText
    Next I
    End With
    Close #iText 
    Application.ScreenUpdating = True
    End Sub

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Does this always happen? Have you tried running the code again?

    Is there any other code?

  3. #3
    I don't think so... That's all the codes....

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Have you tried running it again?

  5. #5
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    I tried that in my Excel...Nothing happens....No errors...
    Can you upload your file for us?

  6. #6
    It works now... ! Thanks!

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    How does it work. It fails for me.

    If you have mor than one entry in A2:A65536 it wil fail, beacuse you open the same file number each time, or try to. It can only work if A3 down are empty.

    And why would you kill the file every iteration of the loop anyway?

  8. #8
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Quote Originally Posted by xld
    because you open the same file number each time, or try to.
    The filenumber actually changes on each iteration, try stepping through.

    I think the problem is that the file isn't being closed.

    This works for me.
       With ActiveSheet
    	 
    	sPath = "C:\"
    	 
    	For I = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
    		 
    		sText = .Range("D" & I)
    		 
    		iText = FreeFile
    		 
    		sOutput = sPath & Trim(.Name) & ".Txt"
    		 
    		On Error Resume Next
    		Kill sOutput
    		On Error GoTo 0
    		 
    		Open sOutput For Output As #iText
    		Print #iText, sText
    		Close #iText
    	Next I

  9. #9
    Open the same file number each time? which sentence tells this?
    I know it's the looping problem, but I may not understand the meaning behinds the codes very clearly....

  10. #10
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Dreamer

    What are you actually trying to do with this code?

    As far as I can see it creates a file, writes a value to it then deletes the file, creates the file again, writes a value to it, deletes it etc etc.

  11. #11
    I try to kill any existing file before opening any files..
    that's why I write that kill statement..

    I wonder how can I close the file... so that I can prevent error message...

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Norie
    The filenumber actually changes on each iteration, try stepping through.

    I think the problem is that the file isn't being closed.
    Yolu are rfght, I didn't notice that, I just got the same eror as the OP.

    If the file number is upped, the fact that the previous file is till open shouldn't matter, it is all done by file number?

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Dreamer
    I wonder how can I close the file... so that I can prevent error message...
    Just close it

    Close #iText

  14. #14
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Quote Originally Posted by xld
    If the file number is upped, the fact that the previous file is till open shouldn't matter, it is all done by file number?
    I don't actually think that's the way it works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •