Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1725|回复: 2

[已解决]清除同路径TXT文档中的指定字符和空格,谢谢爱疯老师!

[复制链接]
发表于 2016-10-21 15:45 | 显示全部楼层 |阅读模式
本帖最后由 lhj323323 于 2016-10-23 14:17 编辑

老师

我已将需求在上传的附件中进行了详细的说明,请下载来看看,先谢谢了
去除.rar (7.79 KB, 下载次数: 4)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2016-10-21 22:38 | 显示全部楼层    本楼为最佳答案   
Private Sub CommandButton1_Click()
    Dim fso As Object, tf As Object, regex As Object
    Dim p As String, f As String, fld As String, str As String
    Dim i As Integer, arr

    '1)创建子文件夹
    p = ThisWorkbook.Path & "\"
    fld = "处理后"
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FolderExists(p & fld) = False Then fso.CreateFolder (p & fld)

    '2)设置替换规则
    Set regex = CreateObject("VBScript.RegExp")
    regex.Global = True
    regex.Pattern = "\{.*\};?"

    '3)处理各原文本
    f = Dir(p & "*.txt")
    Do While f <> ""
        '读
        Set tf = fso.OpenTextFile(p & f)
        arr = Split(tf.readall, vbLf)
        tf.Close
        '写
        Set tf = fso.CreateTextFile(p & fld & "\" & f, True)
        For i = 0 To UBound(arr)
            str = arr(i)
            str = regex.Replace(str, "")    '按正则规则替换掉
            str = Application.Trim(str)     '只保留单词之间的单个空格
            If Len(str) > 1 Then tf.writeline (str)
        Next i
        tf.Close
        f = Dir
    Loop
End Sub

去除2.rar (13.85 KB, 下载次数: 6)
回复

使用道具 举报

 楼主| 发表于 2016-10-23 14:17 | 显示全部楼层
爱疯 发表于 2016-10-21 22:38
Private Sub CommandButton1_Click()
    Dim fso As Object, tf As Object, regex As Object
    Dim p  ...

谢谢爱疯老师!,问题解决了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-3-28 22:45 , Processed in 0.346037 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表