Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何转换多个txt文件并将其保存到excel_Excel_Vba - Fatal编程技术网

如何转换多个txt文件并将其保存到excel

如何转换多个txt文件并将其保存到excel,excel,vba,Excel,Vba,我在一个特定文件夹中有几个txt文件,我想通过将txt转换为列来将这些文件转换为excel。然后,我想通过删除txt文件并仅保留excel文件,将excel文件单独保存在同一文件夹中。 我需要的VBA代码,它可以做到这一点,也通过过滤列A中的空白和删除所有空白 感谢您的帮助尝试此功能***记住更改文件夹名称(spath)!***: 子getTextFiles() 作为变体的dimspath 作为变体的Dim-sfile Dim-lc作为变体 将txtBook设置为工作簿 Dim x作为变体 将s

我在一个特定文件夹中有几个txt文件,我想通过将txt转换为列来将这些文件转换为excel。然后,我想通过删除txt文件并仅保留excel文件,将excel文件单独保存在同一文件夹中。 我需要的VBA代码,它可以做到这一点,也通过过滤列A中的空白和删除所有空白


感谢您的帮助

尝试此功能***记住更改文件夹名称(spath)!***:

子getTextFiles()
作为变体的dimspath
作为变体的Dim-sfile
Dim-lc作为变体
将txtBook设置为工作簿
Dim x作为变体
将saveName设置为字符串
Application.DisplayAlerts=False
“重要的!!!!!
'设置保存文本文件的文件夹的路径
spath=“C:\test\”
'循环浏览文件夹中的所有文本文件
sfile=Dir(spath&“*.txt”)
在sfile“”时执行此操作
'打开文本文件
设置txtBook=Workbooks.Open(spath和sfile)
'文本到列-逗号分隔
txtBook.Sheets(1)。Columns(1)。TextToColumns目标:=txtBook.Sheets(1)。单元格(1,1),数据类型:=xlDelimited_
制表符:=False,分号:=False,逗号:=True,空格:=False,其他:=False
'查找包含数据的最后一行
lc=txtBook.Sheets(1).单元格(txtBook.Sheets(1).Rows.Count,“a”).End(xlUp).Row
'循环遍历列“A”中的所有行,如果单元格为空,则删除该行
对于x=lc至1步骤-1
如果txtBook.Sheets(1).Cells(x,1)=“”,则txtBook.Sheets(1).Cells(x,1).EntireRow.Delete
下一个x
'将文件另存为xlsx并关闭它
'不带“.txt”部分的文件名
保存名称=左(sfile,Len(sfile)-4)
txtBook.SaveAs文件名:=spath&saveName,文件格式:=51,CreateBackup:=False
txtBook,关闭
设置txtBook=Nothing
'删除旧文本文件
杀死斯帕斯和斯菲尔
'获取另一个文件
sfile=Dir()
环
Application.DisplayAlerts=True
端接头

谢谢你,亲爱的……看起来不错。但有一个小问题。我应该使用的分隔符(“{”)并且我已经将代码更改如下:“Text to Columns-逗号分隔的txtBook.Sheets(1).Columns(1).TextToColumns Destination:=txtBook.Sheets(1).Cells(1,1),DataType:=xlDelimited,Tab:=False,分号:=False,逗号:=False,空格:=False,其他:=({”),但是有一些错误。请建议plus
Other:=(){“
应更改为
Other:=True
,之后
OtherChar:=”(“{”)”
不要忘记在
OtherChar:=“{”
之前添加逗号!
Sub getTextFiles()

Dim spath As Variant
Dim sfile As Variant
Dim lc As Variant
Dim txtBook As Workbook
Dim x As Variant
Dim saveName As String

Application.DisplayAlerts = False

'IMPORTANT!!!!!
'Set path to folder where you keep the text files
spath = "C:\test\"

'Loop through all text files in the folder
sfile = Dir(spath & "*.txt")

Do While sfile <> ""

    'Open the text file
    Set txtBook = Workbooks.Open(spath & sfile)

    'Text to Columns - comma separated
    txtBook.Sheets(1).Columns(1).TextToColumns Destination:=txtBook.Sheets(1).Cells(1, 1), DataType:=xlDelimited, _
    Tab:=False, Semicolon:=False, Comma:=True, Space:=False, Other:=False

    'Find last row with data
    lc = txtBook.Sheets(1).Cells(txtBook.Sheets(1).Rows.Count, "a").End(xlUp).Row

    'Loop through all rows in column "A" and delete the row if cell is blank
    For x = lc To 1 Step -1
        If txtBook.Sheets(1).Cells(x, 1) = "" Then txtBook.Sheets(1).Cells(x, 1).EntireRow.Delete
    Next x

    'Save file as xlsx and close it

    'File name without the ".txt" part
    saveName = Left(sfile, Len(sfile) - 4)

    txtBook.SaveAs Filename:=spath & saveName, FileFormat:=51, CreateBackup:=False
    txtBook.Close


    Set txtBook = Nothing

    'Delete old text file
    Kill spath & sfile

    'Get another file
    sfile = Dir()

Loop

Application.DisplayAlerts = True

End Sub