Excel精英培训网

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

[已解决]若干VBA如何合并成一个

[复制链接]
发表于 2015-10-2 15:39 | 显示全部楼层 |阅读模式
本帖最后由 lijian8003 于 2015-10-2 17:21 编辑

下列VBA是将txt数据导入excel,共有1330个,如何用 For y = 1 To 1330 合并成一个?

Sub 导入1()
Sheets("1").Select
Application.DisplayAlerts = False
Dim s() As String
Dim s2() As String
On Error Resume Next
Range("b1:az210").ClearContents
Open "d:\数据\lishi\0001.txt" For Input As #1
s = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
Close #1
      For x = 0 To UBound(s)
         s2 = Split(s(x), " ")
         ss = UBound(s2)
       Range("b" & x + 1).Resize(, ss + 1) = s2
   Next
Application.DisplayAlerts = True
End Sub

Sub 导入2()
Sheets("2").Select
Application.DisplayAlerts = False
Dim s() As String
Dim s2() As String
On Error Resume Next
Range("b1:az210").ClearContents
Open "d:\数据\lishi\0002.txt" For Input As #1
s = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
Close #1
      For x = 0 To UBound(s)
         s2 = Split(s(x), " ")
         ss = UBound(s2)
       Range("b" & x + 1).Resize(, ss + 1) = s2
   Next
Application.DisplayAlerts = True
End Sub

......

Sub 导入1330()
Sheets("1330").Select
Application.DisplayAlerts = False
Dim s() As String
Dim s2() As String
On Error Resume Next
Range("b1:az210").ClearContents
Open "d:\数据\lishi\1330.txt" For Input As #1
s = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
Close #1
      For x = 0 To UBound(s)
         s2 = Split(s(x), " ")
         ss = UBound(s2)
       Range("b" & x + 1).Resize(, ss + 1) = s2
   Next
Application.DisplayAlerts = True
End Sub
最佳答案
2015-10-2 15:46
Sub test1()
    Dim i
    For i = 1 To 1330
        Call test2(i)
    Next i
End Sub

Sub test2(x)
    Sheets(CStr(i)).Select
    Application.DisplayAlerts = False
    Dim s() As String
    Dim s2() As String
    On Error Resume Next
    Range("b1:az210").ClearContents
    Open "d:\数据\lishi\" & Format(i, "0000") & ".txt" For Input As #1
    s = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
    Close #1
    For x = 0 To UBound(s)
        s2 = Split(s(x), " ")
        ss = UBound(s2)
        Range("b" & x + 1).Resize(, ss + 1) = s2
    Next
    Application.DisplayAlerts = True
End Sub
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2015-10-2 15:46 | 显示全部楼层    本楼为最佳答案   
Sub test1()
    Dim i
    For i = 1 To 1330
        Call test2(i)
    Next i
End Sub

Sub test2(x)
    Sheets(CStr(i)).Select
    Application.DisplayAlerts = False
    Dim s() As String
    Dim s2() As String
    On Error Resume Next
    Range("b1:az210").ClearContents
    Open "d:\数据\lishi\" & Format(i, "0000") & ".txt" For Input As #1
    s = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
    Close #1
    For x = 0 To UBound(s)
        s2 = Split(s(x), " ")
        ss = UBound(s2)
        Range("b" & x + 1).Resize(, ss + 1) = s2
    Next
    Application.DisplayAlerts = True
End Sub

评分

参与人数 1 +3 收起 理由
lijian8003 + 3 赞一个!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 08:22 , Processed in 0.257169 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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