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

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

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. 使用VBAtxt批量转换excel保存

    2024-02-20 10:02:01       46 阅读
  2. EXCEL VBA sheet批量转转PDF

    2024-02-20 10:02:01       34 阅读
  3. 使用 Newtonsoft.Json 单数据转换对象

    2024-02-20 10:02:01       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-20 10:02:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-20 10:02:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-20 10:02:01       82 阅读
  4. Python语言-面向对象

    2024-02-20 10:02:01       91 阅读

热门阅读

  1. classpath:springmvc.xml

    2024-02-20 10:02:01       53 阅读
  2. 沁恒CH32V30X学习笔记10---pwm输出

    2024-02-20 10:02:01       46 阅读
  3. CSS的定位position,字体图标,修饰

    2024-02-20 10:02:01       53 阅读
  4. 前端开发常用函数整理

    2024-02-20 10:02:01       50 阅读
  5. Vue3自定义全局指令

    2024-02-20 10:02:01       45 阅读
  6. 【C】printf和scanf函数的探索

    2024-02-20 10:02:01       40 阅读
  7. SQL Server中类似MySQL的REPLACE INTO语句

    2024-02-20 10:02:01       44 阅读
  8. SpringCloud微服务调用丢失请求头

    2024-02-20 10:02:01       38 阅读