使用VBA将TXT批量转换为XLS
我正在尝试使用VBA将一个包含.txt文件的目录转换为.xls。我正在使用以下代码:使用VBA将TXT批量转换为XLS,vba,excel,batch-file,text,Vba,Excel,Batch File,Text,我正在尝试使用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
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
Sub-TXTconvertXLS()
"变数",
将wb设置为工作簿
作为字符串的Dim strFile
作为字符串的Dim strDir
'目录
strDir=“\\xx\xx\xx\xx\Desktop\Test\Test1”
strFile=Dir(strDir&“*.txt”)
'循环
当strFile“”时执行
设置wb=工作簿。打开(strDir和strFile)
与wb
.SaveAs替换(wb.FullName,.txt,.xls),50
.接近真实
以
设置wb=Nothing
环
端接头
问题是:当我运行它时,它会立即声明在目录中已经有一个文件名为它试图保存的名称。它显示的名称甚至有一个.xls扩展名,即使目录中确实没有.xls!任何帮助都将不胜感激-谢谢 在
循环之前,您似乎缺少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
Do While strFile“”
设置wb=工作簿。打开(strDir和strFile)
与wb
.SaveAs替换(wb.FullName,.txt,.xls),50
.Close False“在循环之前,您似乎缺少strFile=Dir
。没有它,您将重新处理相同的TXT文件。(看)兄弟,这是你必须自己去发现的!