尝试打开文本文件时“文件已打开”

时间:2020-02-28 07:26:18

标签: excel vba file

我要做什么::我有一个Log.txt,其中列出了Excel表格中的特殊值。每当执行宏时,它都会检查新的特殊值。在通过Sub将它们添加到Log.txt之前,同一个Sub检查相应的值(它们是明确的)是否已经在Log-List中。如果不是这种情况,则应将值添加到列表中。

我的方法:您可以在下面的代码示例中查看我当前的方法。


Dim FileNum as Integer
dim DataLine as String
Dim strPath as String
Dim strEntry as String

strPath = [Path to Log.txt]  
strEntry = [Special Value]     

'In this first part the Log.txt is opened for Input and each line is saved in DataLine 
'to be compared to the special value in strEntry. If it is already in the Log.txt, the Sub to
'create a new Log-Entry is exited and is started again, once the next special value from another cell is 
'obtained (from another Sub).

FileNum = FreeFile()
Open strPath For Input As #FileNum
Do While Not EOF(FileNum)
     Line Input #FileNum, DataLine
'The value strEntry should start at position 2 of the Entry in the Log.txt (due to the quotation marks [""] in the 
'Log.txt line.
     If InStr(DataLine, strEntry) = 2 Then Exit Sub
     Loop
Close #FileNum

'After it could be verified, that strEntry is not already in the Log.txt, the txt-File should be opened 
'again, this time for Append. Then, the strEntry should be written to the txt-File, the Log.txt should close and 
'Sub is finished.

FileNum = FreeFile()
Open strPath For Append As #FileNum
Write #FileNum, strEntry 
Close #FileNum

问题:我观察到Sub的第一部分工作正常。如果strEntry中已经有Log.txt,则退出Sub,整个Macro跳转到下一个特殊值。如果txt文件中尚未包含此值,则Sub的第一部分不会退出Sub,而是跳转到第二部分,在第二部分中应将值附加到Log.txt

这是问题所在。如果我排除Sub的第一部分,则可以验证第二部分也可以正常工作(因为他只是将所有值附加到txt-File中)。但是,一旦包含了第一部分,我就会收到错误消息

文件已打开。

我不知道为什么会这样,正如我在第一部分结尾处Close #FileNum一样。

在此先感谢您的想法和解决方案。

1 个答案:

答案 0 :(得分:2)

问题是If InStr(DataLine, strEntry) = 2 Then Exit Sub在这种情况下,因为它正在退出Sub,所以您没有关闭文件。在这种情况下,文件保持打开状态。明智地使用Exit Sub。尝试使用一个入口点和一个出口点,以便您可以处置对象/变量并正确进行相关清理。

一种方法:使用布尔变量

'
'~~> Rest of your code
'
Dim continue As Boolean: continue = True

'
'~~> Rest of your code
'

Do While Not EOF(FileNum)
    Line Input #FileNum, DataLine
    If InStr(DataLine, strEntry) = 2 Then
        continue = False
        Exit Do
    End If
Loop
Close #FileNum

If continue = True Then
    FileNum = FreeFile()
    Open strPath For Append As #FileNum
    Write #FileNum, strEntry
    Close #FileNum
End If

另一种方式:使用GOTO

    FileNum = FreeFile()
    Open strPath For Input As #FileNum
    Do While Not EOF(FileNum)
        Line Input #FileNum, DataLine
        'The value strEntry should start at position 2 of the Entry in the Log.txt (due to the quotation marks [""] in the
        'Log.txt line.
        If InStr(DataLine, strEntry) = 2 Then GoTo CleanupAndExit
    Loop
    Close #FileNum

    'After it could be verified, that strEntry is not already in the Log.txt, the txt-File should be opened
    'again, this time for Append. Then, the strEntry should be written to the txt-File, the Log.txt should close and
    'Sub is finished.

    FileNum = FreeFile()
    Open strPath For Append As #FileNum
    Write #FileNum, strEntry
CleanupAndExit:
    Close #FileNum