Excel精英培训网

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

[已解决]怎么写批量处理文件夹的文本文件的VBA代码?

[复制链接]
发表于 2011-6-28 21:54 | 显示全部楼层 |阅读模式
附件: help3.rar (3.56 KB, 下载次数: 3)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2011-6-28 22:34 | 显示全部楼层
回复

使用道具 举报

发表于 2011-6-28 23:55 | 显示全部楼层    本楼为最佳答案   
写了两个供参考(一个改自录制宏)
Sub 宏1()
fn$ = Dir(ThisWorkbook.Path & "\test\?.txt")
Do While fn <> ""
  Open ThisWorkbook.Path & "\test\" & fn For Input As #1
  Line Input #1, s$
  Close #1
  i% = Len(s)
  If InStr(s, " ") Then i = InStr(s, " ")
    Workbooks.OpenText Filename:=ThisWorkbook.Path & "\test\" & fn, Origin:=936, StartRow:=1 _
        , DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(i, 1)), _
        TrailingMinusNumbers:=True
    Columns("A:A").Select
    Selection.Delete Shift:=xlToLeft
    ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\test\a" & fn, FileFormat:=xlText, _
        CreateBackup:=False
    ActiveWorkbook.Save
    ActiveWindow.Close 0
    fn = Dir
Loop
End Sub
Sub 宏2()

fn$ = Dir(ThisWorkbook.Path & "\test\?.txt")
Do While fn <> ""
  Open ThisWorkbook.Path & "\test\" & fn For Input As #1
  Open ThisWorkbook.Path & "\test\b" & fn For Output As #2
  Do While Not EOF(1)
    Line Input #1, s$
    If InStr(s, " ") Then s = Mid(s, InStr(s, " ") + 1)
    Print #2, s
  Loop
  Close #1
  Close #2
  fn = Dir
Loop
End Sub
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-24 22:32 , Processed in 0.218279 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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