使用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
相关推荐
技术小甜甜42 分钟前
【Copilot】能选 Claude 和 GPT-6 了,Excel 分析该交给谁
gpt·excel·copilot·claude·microsoft365·gpt-6
小白今天也很酷1 天前
MCXN947更新板载DAP_Link固件的方法
c语言·python·单片机·ubuntu·excel·1024程序员节
ratel️2 天前
小墨析 · Office Agent 日常办公不烧心
python·langchain·word·excel·fastapi·js·ppt
Logic1012 天前
没有TextSplit函数的老版本也能拆分单元格的内容
excel·拆分数据·excel技巧·数据拆分·拆分单元格·textsplit·旧版excel
2601_967987372 天前
你们都用Python实现了哪些自动化办公?
python·excel·自动化办公·1024程序员节·学习资料·跨平台操作
城数派2 天前
1901-2025年中国省/市/县三级逐月平均气温数据Excel+Shp格式
excel
gtstore3 天前
从 Excel 迁移到仓库管理系统的数据准备与切换方案
excel·数据迁移·仓库管理系统·进销存·信息化实施
ms365copilot3 天前
我不再自己做 Word、Excel 和 PPT 了,我让 Copilot 直接帮我创建 (1)
word·powerpoint·excel·copilot
cspttty4 天前
2027采购岗求职:大数据管理与应用专业如何把Excel、SQL变成业务能力
excel
ms365copilot4 天前
Excel Copilot 一键定位修改的内容
excel·copilot