[英]Batch Convert TXT to XLS Using VBA
我正在嘗試使用VBA將充滿.txt文件的目錄轉換為.xls。 我正在使用以下代碼:
Sub TXTconvertXLS()
'Variables
Dim wb As Workbook
Dim strFile As String
Dim strDir As String
'Directories
strDir = "\\xx\xx\xx\xx\Desktop\Test\Test1\"
strFile = Dir(strDir & "*.txt")
'Loop
Do While strFile <> ""
Set wb = Workbooks.Open(strDir & strFile)
With wb
.SaveAs Replace(wb.FullName, ".txt", ".xls"), 50
.Close True
End With
Set wb = Nothing
Loop
End Sub
問題是:當我運行它時,它立即指出在目錄中已經存在一個文件,該文件的名稱試圖保存。 即使目錄中肯定沒有.xls,它顯示的名稱甚至帶有.xls擴展名! 任何幫助將不勝感激-謝謝!
您似乎在Loop
之前缺少strFile = Dir
。 沒有它,您將重新處理相同的TXT文件。
Do While strFile <> ""
Set wb = Workbooks.Open(strDir & strFile)
With wb
.SaveAs Replace(wb.FullName, ".txt", ".xls"), 50
.Close False '<-already saved in the line directly above
End With
Set wb = Nothing
strFile = Dir '<- stuffs the next filename into strFile
Loop
請參閱目錄功能
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.