其實下面是根據humanmagic老師寫的陳述句改的,目的要實作多個文本檔案洗掉,然后保留每一次的結果。例如,目標檔案洗掉A后,拿洗掉結果檔案再洗掉B、、、、以此類推。最后以結果檔案是刪掉ABCD、、后的結果,同時每刪一步的檔案都是保留的。如下圖:

下面陳述句刪第一、二個檔案正常,但第三個開始雖然能生成CDE結果檔案,但實際上還是保留第一二步的洗掉結果,第三個開始沒有洗掉。
麻煩老師們看看問題出在哪里?
Private Sub Command10_Click()
Dim a As Object, temp As String
Set a = CreateObject("scripting.dictionary")
Open Text2 For Input As #1 "Text2是第一個要刪的檔案路徑“
Line Input #1, temp
Do Until EOF(1)
Line Input #1, temp
a(Left(temp, 6)) = ""
Loop
Close #1
Open Text1 For Input As #1 “Text1原始檔案路徑”
Open Left(Text1, Len(Text1) - 4) + "_洗掉A.txt" For Output As #2 “第一次洗掉結果檔案路徑”
Line Input #1, temp
Print #2, temp
Do Until EOF(1)
Line Input #1, temp
If Not a.Exists(Left(temp, 6)) Then
Print #2, temp
End If
Loop
Close #1, #2
if Text3 <>"" then "Text3是第二個要刪的檔案路徑“
Open Text3 For Input As #1
Line Input #1, temp
Do Until EOF(1)
Line Input #1, temp
a(Left(temp, 6)) = ""
Loop
Close #1
Open Left(Text1, Len(Text1) - 4) + "_洗掉A.txt" For Input As #1 “第一次洗掉結果檔案路徑,作為第二次洗掉的原檔案”
Open Left(Text1, Len(Text1) - 4) + "_洗掉B.txt" For Output As #2 “第二次洗掉結果檔案路徑”
Line Input #1, temp
Print #2, temp
Do Until EOF(1)
Line Input #1, temp
If Not a.Exists(Left(temp, 6)) Then
Print #2, temp
End If
Loop
Close #1, #2
end if
if Text4 <>"" then "Text4是第三個要刪的檔案路徑“
Open Text4 For Input As #1
Line Input #1, temp
Do Until EOF(1)
Line Input #1, temp
a(Left(temp, 6)) = ""
Loop
Close #1
Open Left(Text1, Len(Text1) - 4) + "_洗掉B.txt" For Input As #1 “第二次洗掉結果檔案路徑,作為第三次洗掉的原檔案”
Open Left(Text1, Len(Text1) - 4) + "_洗掉C.txt" For Output As #2 “第三次洗掉結果檔案路徑”
Line Input #1, temp
Print #2, temp
Do Until EOF(1)
Line Input #1, temp
If Not a.Exists(Left(temp, 6)) Then
Print #2, temp
End If
Loop
Close #1, #2
end if
if Text5 <>"" then "Text5是第四個要刪的檔案路徑“
Open Text5 For Input As #1
Line Input #1, temp
Do Until EOF(1)
Line Input #1, temp
a(Left(temp, 6)) = ""
Loop
Close #1
Open Left(Text1, Len(Text1) - 4) + "_洗掉C.txt" For Input As #1 “第三次洗掉結果檔案路徑,作為第四次洗掉的原檔案”
Open Left(Text1, Len(Text1) - 4) + "_洗掉D.txt" For Output As #2 “第四次洗掉結果檔案路徑”
Line Input #1, temp
Print #2, temp
Do Until EOF(1)
Line Input #1, temp
If Not a.Exists(Left(temp, 6)) Then
Print #2, temp
End If
Loop
Close #1, #2
end if
MsgBox "頻次剔除完成,請在原檔案夾查看結果!", 32, "提示"
End Sub
uj5u.com熱心網友回復:
每次完成后加一句清空字典就行了,否則字典在累積所有要刪的檔案a.RemoveAll
添加在
Close #1, #2
的下一行
uj5u.com熱心網友回復:
加上a.RemoveAll結果一樣,只能刪前兩個檔案。不知道原因在哪uj5u.com熱心網友回復:
代碼邏輯沒有問題,你要確定Text4、Text5是否存在,或者檔案編碼,或者檔案結構Private Sub Command10_Click()
Dim a As Object, temp As String
Set a = CreateObject("scripting.dictionary")
Open Text2 For Input As #1 "Text2是第一個要刪的檔案路徑“
Line Input #1, temp
Do Until EOF(1)
Line Input #1, temp
a(Left(temp, 6)) = ""
Loop
Close #1
Open Text1 For Input As #1 “Text1原始檔案路徑”
Open Left(Text1, Len(Text1) - 4) + "_洗掉A.txt" For Output As #2 “第一次洗掉結果檔案路徑”
Line Input #1, temp
Print #2, temp
Do Until EOF(1)
Line Input #1, temp
If Not a.Exists(Left(temp, 6)) Then
Print #2, temp
End If
Loop
Close #1, #2
a.RemoveAl
if Text3 <>"" then "Text3是第二個要刪的檔案路徑“
Open Text3 For Input As #1
Line Input #1, temp
Do Until EOF(1)
Line Input #1, temp
a(Left(temp, 6)) = ""
Loop
Close #1
Open Left(Text1, Len(Text1) - 4) + "_洗掉A.txt" For Input As #1 “第一次洗掉結果檔案路徑,作為第二次洗掉的原檔案”
Open Left(Text1, Len(Text1) - 4) + "_洗掉B.txt" For Output As #2 “第二次洗掉結果檔案路徑”
Line Input #1, temp
Print #2, temp
Do Until EOF(1)
Line Input #1, temp
If Not a.Exists(Left(temp, 6)) Then
Print #2, temp
End If
Loop
Close #1, #2
a.RemoveAl
end if
if Text4 <>"" then "Text4是第三個要刪的檔案路徑“
Open Text4 For Input As #1
Line Input #1, temp
Do Until EOF(1)
Line Input #1, temp
a(Left(temp, 6)) = ""
Loop
Close #1
Open Left(Text1, Len(Text1) - 4) + "_洗掉B.txt" For Input As #1 “第二次洗掉結果檔案路徑,作為第三次洗掉的原檔案”
Open Left(Text1, Len(Text1) - 4) + "_洗掉C.txt" For Output As #2 “第三次洗掉結果檔案路徑”
Line Input #1, temp
Print #2, temp
Do Until EOF(1)
Line Input #1, temp
If Not a.Exists(Left(temp, 6)) Then
Print #2, temp
End If
Loop
Close #1, #2
a.RemoveAl
end if
if Text5 <>"" then "Text5是第四個要刪的檔案路徑“
Open Text5 For Input As #1
Line Input #1, temp
Do Until EOF(1)
Line Input #1, temp
a(Left(temp, 6)) = ""
Loop
Close #1
Open Left(Text1, Len(Text1) - 4) + "_洗掉C.txt" For Input As #1 “第三次洗掉結果檔案路徑,作為第四次洗掉的原檔案”
Open Left(Text1, Len(Text1) - 4) + "_洗掉D.txt" For Output As #2 “第四次洗掉結果檔案路徑”
Line Input #1, temp
Print #2, temp
Do Until EOF(1)
Line Input #1, temp
If Not a.Exists(Left(temp, 6)) Then
Print #2, temp
End If
Loop
Close #1, #2
a.RemoveAl
end if
MsgBox "頻次剔除完成,請在原檔案夾查看結果!", 32, "提示"
End Sub
uj5u.com熱心網友回復:
核對了所有Text陳述句, 測驗檔案全部改成ANSI,跑出來還是只能刪到第AB兩步,下圖是跑出來的效果,5個檔案都能生成,而且"目標0_洗掉E.txt"也有記錄,只不過從B開始到E記錄都一樣。
下面是實際代碼:
Private Sub Command10_Click()
Dim a As Object, temp As String
Set a = CreateObject("scripting.dictionary")
If Text2 <> "" Then
Open Text2 For Input As #1
Line Input #1, temp
Do Until EOF(1)
Line Input #1, temp
a(Left(temp, Text8)) = ""
Loop
Close #1
Open Text1 For Input As #1
Open Left(Text1, Len(Text1) - 4) + "_洗掉A.txt" For Output As #2
Line Input #1, temp
Print #2, temp
Do Until EOF(1)
Line Input #1, temp
If Not a.Exists(Left(temp, Text8)) Then
Print #2, temp
End If
Loop
Close #1, #2
a.RemoveAll
End If
If Text3 <> "" Then
Open Text3 For Input As #1
Line Input #1, temp
Do Until EOF(1)
Line Input #1, temp
a(Left(temp, Text8)) = ""
Loop
Close #1
Open Left(Text1, Len(Text1) - 4) + "_洗掉A.txt" For Input As #1
Open Left(Text1, Len(Text1) - 4) + "_洗掉B.txt" For Output As #2
Line Input #1, temp
Print #2, temp
Do Until EOF(1)
Line Input #1, temp
If Not a.Exists(Left(temp, Text8)) Then
Print #2, temp
End If
Loop
Close #1, #2
a.RemoveAll
End If
If Text4 <> "" Then
Open Text4 For Input As #1
Line Input #1, temp
Do Until EOF(1)
Line Input #1, temp
a(Left(temp, Text8)) = ""
Loop
Close #1
Open Left(Text1, Len(Text1) - 4) + "_洗掉B.txt" For Input As #1
Open Left(Text1, Len(Text1) - 4) + "_洗掉C.txt" For Output As #2
Line Input #1, temp
Print #2, temp
Do Until EOF(1)
Line Input #1, temp
If Not a.Exists(Left(temp, Text8)) Then
Print #2, temp
End If
Loop
Close #1, #2
a.RemoveAll
End If
If Text5 <> "" Then
Open Text5 For Input As #1
Line Input #1, temp
Do Until EOF(1)
Line Input #1, temp
a(Left(temp, Text8)) = ""
Loop
Close #1
Open Left(Text1, Len(Text1) - 4) + "_洗掉C.txt" For Input As #1
Open Left(Text1, Len(Text1) - 4) + "_洗掉D.txt" For Output As #2
Line Input #1, temp
Print #2, temp
Do Until EOF(1)
Line Input #1, temp
If Not a.Exists(Left(temp, Text8)) Then
Print #2, temp
End If
Loop
Close #1, #2
a.RemoveAll
End If
If Text6 <> "" Then
Open Text6 For Input As #1
Line Input #1, temp
Do Until EOF(1)
Line Input #1, temp
a(Left(temp, Text8)) = ""
Loop
Close #1
Open Left(Text1, Len(Text1) - 4) + "_洗掉D.txt" For Input As #1
Open Left(Text1, Len(Text1) - 4) + "_洗掉E.txt" For Output As #2
Line Input #1, temp
Print #2, temp
Do Until EOF(1)
Line Input #1, temp
If Not a.Exists(Left(temp, Text8)) Then
Print #2, temp
End If
Loop
Close #1, #2
a.RemoveAll
End If
MsgBox "頻次剔除完成,請在原檔案夾查看結果!", 32, "提示"
End Sub
uj5u.com熱心網友回復:
直接用excel表VBA測驗,Text全部改成檔案完整路徑,執行結果一樣,還是只能刪前兩個檔案。轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/184970.html
標籤:VBA
