Excel精英培训网

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

[已解决][求助] 请老师邦助修改宏,谢谢

[复制链接]
发表于 2012-5-23 01:31 | 显示全部楼层 |阅读模式
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2012-5-23 13:08 | 显示全部楼层
Sub Macro1()
Dim wj As String
Range("A2:U65536").ClearContents
wj = Dir(ThisWorkbook.Path & "\*.txt")
s = 0: s1 = 1
Do While wj <> ""
    s = s + 1
    s2 = 0
    Open ThisWorkbook.Path & "\" & wj For Input As #s
    Do While Not (EOF(s))
        Line Input #s, n
        s2 = s2 + 1
        If s2 > 0 Then
            s1 = s1 + 1
            y = Split(n, ",")
            For i = 0 To UBound(y)
                Cells(s1, i + 1) = y(i)
                Cells(s1, 21) = Right(wj, 12)
            Next i
        End If
    Loop
    Close #s
    wj = Dir
Loop
End Sub
回复

使用道具 举报

发表于 2012-5-23 13:44 | 显示全部楼层
消除空行数据:
Sub Macro1()
Dim wj As String
Range("A2:L65536").ClearContents
wj = Dir(ThisWorkbook.Path & "\*.txt")
s = 0: S1 = 1
Do While wj <> ""
    s = s + 1
    s2 = 0
    Open ThisWorkbook.Path & "\" & wj For Input As #s
    Do While Not (EOF(s))
        Line Input #s, n
        s2 = s2 + 1
        If s2 > 1 Then
            Y = Split(n, ",")
            If UBound(Y) > 1 Then     '非空行
              S1 = S1 + 1
              For i = 0 To UBound(Y)
                Cells(S1, i + 1) = Y(i)
                Cells(s1, 21) = Right(wj, 12)
              Next i
           End If
        End If
    Loop
    Close #s
    wj = Dir
Loop
End Sub
回复

使用道具 举报

 楼主| 发表于 2012-5-23 19:31 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

 楼主| 发表于 2012-5-23 19:38 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

发表于 2012-5-24 07:44 | 显示全部楼层    本楼为最佳答案   
本帖最后由 zjdh 于 2012-5-24 07:48 编辑

Sub Macro1()
    Dim wj As String
    Range("A2:U65536").ClearContents
    wj = Dir(ThisWorkbook.Path & "\*.txt")
    s = 0: S1 = 1
    Do While wj <> ""
        s = s + 1
        s2 = 0
        Open ThisWorkbook.Path & "\" & wj For Input As #s
        Do While Not (EOF(s))
            Line Input #s, n
            s2 = s2 + 1
            If s2 > 0 Then
                Y = Split(n, ",")
                S1 = S1 + 1
                For i = 0 To UBound(Y)
                    Cells(S1, i + 1) = Y(i)
                    Cells(S1, 21) = Replace(Split(wj, ".")(0), ThisWorkbook.Path, "")
                Next i
            End If
        Loop
        Close #s
        wj = Dir
    Loop
End Sub
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 10:54 , Processed in 0.375420 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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