Excel精英培训网

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

[已解决]txt文件名和内容批量导入excel文件

[复制链接]
发表于 2016-11-23 09:39 | 显示全部楼层 |阅读模式
百度云附件.求助啊..以前是一个文件一个文件手工导入的.网上收索到的宏都对不上号.急需大师求解
最佳答案
2016-11-23 12:44
网上搬砖来的
  1. Sub tqwb()
  2.     Dim a$, b$, i%
  3.     Dim txtName
  4.     txtName = Dir(ThisWorkbook.Path & "\*.txt")
  5.     Range("a:c").ClearContents
  6.     [a1:c1] = [{"文件名称","编号","数量"}]
  7.     n = 2
  8.     Do While txtName <> ""
  9.         Open ThisWorkbook.Path & "" & txtName For Input As #1
  10.         i = 1
  11.         Do While Not EOF(1)
  12.             Input #1, a, b
  13.             Cells(n, 1) = Replace(txtName, ".txt", "")
  14.             Cells(n, 2) = "'" & a
  15.              Cells(n, 3) = b
  16.             i = i + 1: n = n + 1
  17.         Loop
  18.         Close #1
  19.        txtName = Dir
  20.     Loop
  21. End Sub
复制代码
文件截图.png
导入EXCEL后的内容.png
TXT的内容.png

123.rar

147.02 KB, 下载次数: 15

文件

发表于 2016-11-23 12:44 | 显示全部楼层    本楼为最佳答案   
网上搬砖来的
  1. Sub tqwb()
  2.     Dim a$, b$, i%
  3.     Dim txtName
  4.     txtName = Dir(ThisWorkbook.Path & "\*.txt")
  5.     Range("a:c").ClearContents
  6.     [a1:c1] = [{"文件名称","编号","数量"}]
  7.     n = 2
  8.     Do While txtName <> ""
  9.         Open ThisWorkbook.Path & "" & txtName For Input As #1
  10.         i = 1
  11.         Do While Not EOF(1)
  12.             Input #1, a, b
  13.             Cells(n, 1) = Replace(txtName, ".txt", "")
  14.             Cells(n, 2) = "'" & a
  15.              Cells(n, 3) = b
  16.             i = i + 1: n = n + 1
  17.         Loop
  18.         Close #1
  19.        txtName = Dir
  20.     Loop
  21. End Sub
复制代码

评分

参与人数 2 +13 收起 理由
望帝春心 + 12 神马都是浮云~
huashang917 + 1 我和小伙伴都惊呆了

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2016-11-23 12:59 | 显示全部楼层
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-23 22:44 , Processed in 0.824627 second(s), 16 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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