使用VBA将多个txt批量转换成excel表并保存

VBA将多个txt批量转换成excel表并保存

python 复制代码
Sub ykcbf() 
    Set fso = CreateObject("scripting.filesystemobject")
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    p = ThisWorkbook.Path & ""
    On Error Resume Next
    For Each f In fso.GetFolder(p).Files
        If f.Name Like "*.txt" Then
            fn = fso.GetBaseName(f)
            zrr = Split(ReadUTFText(f), Chr(13))
            ReDim brr(1 To 1000, 1 To 6)
            m = 0
            For i = 0 To UBound(zrr)
                If zrr(i) <> Empty Then
                    s = WorksheetFunction.Trim(zrr(i))
                    b = Split(s, ",")
                    m = m + 1
                    brr(m, 1) = b(0)
                    brr(m, 3) = b(4)
                    brr(m, 4) = b(1)
                    brr(m, 5) = fn
                    brr(m, 6) = b(2)
                End If
            Next
            Application.SheetsInNewWorkbook = 1
            Set wb = Workbooks.Add
            With wb.Sheets(1)
                .Columns(4).NumberFormatLocal = "@"
                .[a1:f1] = Array("姓名", "电话", "省份", "身份证号", "住址", "民族")
                .[a2].Resize(m, 6) = brr
                With .[a1].Resize(m + 1, 6)
                    .Borders.LineStyle = 1
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlCenter
                    .EntireColumn.AutoFit
                End With
                .SaveAs p & fn
                .Close 1
            End With
        End If
    Next f
    Application.ScreenUpdating = True
    MsgBox "OK!"
End Sub
相关推荐
开开心心就好1 天前
绿色版PDF多功能工具,支持编辑转换
人工智能·windows·pdf·ocr·excel·语音识别·harmonyos
优选资源分享2 天前
资条生成器 V1.3 - 财务 HR 专属 Excel 工具
excel·实用工具
m0_502724952 天前
vue3在线预览excel表格
javascript·vue.js·excel
Java小王子呀2 天前
JAVA 导出Excel中添加下拉框用POI
java·excel
傻啦嘿哟2 天前
Python 操作 Excel 条件格式指南
开发语言·python·excel
jgyzl3 天前
2026.3.20 用EasyExcel实现excel报表的导入与导出
java·python·excel
Mr.45674 天前
Spring Boot 3 + EasyExcel 3.x 实战:构建高效、可靠的Excel导入导出服务
spring boot·后端·excel
如意机反光镜裸4 天前
excel怎么快速导入oracle
数据库·oracle·excel
开开心心就好4 天前
免费轻量级PDF阅读器,打开速度快
windows·计算机视觉·visualstudio·pdf·计算机外设·excel·myeclipse