Excel精英培训网

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

[已解决]提取文本值

[复制链接]
发表于 2011-7-26 21:33 | 显示全部楼层 |阅读模式
请问在文本文件中提取值到指定EXCEL中,应如何提取?如附件!
最佳答案
2011-7-26 23:22
本帖最后由 放浪形骸 于 2011-7-26 23:23 编辑

修改下,结果出现在SHEET1里
  1. Sub ReadFile()
  2. Dim iFile As Integer
  3. iFile = FreeFile
  4. Dim sLine As String
  5. Dim iRow As Integer
  6. Sheet1.Cells(1, 2) = "TOTNSUB"
  7. Sheet1.Cells(1, 3) = "TOTNSUBA"
  8. iRow = 1
  9. Open ThisWorkbook.Path & "\文本.TXT" For Input As iFile
  10. While Not EOF(iFile)
  11.     Line Input #iFile, sLine
  12.     sLine = Trim(sLine)
  13.     If Left(sLine, 3) = "eaw" Then
  14.         iRow = iRow + 1
  15.         Sheet1.Cells(iRow, 1) = Right(sLine, Len(sLine) - 4)
  16.     ElseIf sLine = "TOTNSUB" Then
  17.         Line Input #iFile, sLine
  18.         Sheet1.Cells(iRow, 2) = sLine
  19.     ElseIf sLine = "TOTNSUBA" Then
  20.         Line Input #iFile, sLine
  21.         Sheet1.Cells(iRow, 3) = sLine
  22.     End If
  23. Wend
  24. Close iFile
  25. End Sub
复制代码



文本提取值.rar

4.51 KB, 下载次数: 34

发表于 2011-7-26 21:51 | 显示全部楼层
回复

使用道具 举报

发表于 2011-7-26 22:29 | 显示全部楼层
其实是没看明白这个文本的东西
好像是什么统计软件的代码?
不过按照你给的这样的格式,应该是可以弄出来的
等下,上代码
回复

使用道具 举报

 楼主| 发表于 2011-7-26 22:46 | 显示全部楼层
回复 放浪形骸 的帖子

就是要查找文本中的TOTNSUB和TOTNSUBA的值,提取到EXCEL中,还有要提取eaw CQGS04中的CQGSXX,EXCEL中就是想要的结果
回复

使用道具 举报

发表于 2011-7-26 22:46 | 显示全部楼层
  1. Sub ReadFile()
  2. Dim iFile As Integer
  3. iFile = FreeFile
  4. Dim sLine As String
  5. Dim iRow As Integer
  6. Sheet2.Cells(1, 2) = "TOTNSUB"
  7. Sheet2.Cells(1, 3) = "TOTNSUBA"
  8. iRow = 1
  9. Open ThisWorkbook.Path & "\文本.TXT" For Input As iFile
  10. While Not EOF(iFile)
  11.     Line Input #iFile, sLine
  12.     sLine = Trim(sLine)
  13.     If Left(sLine, 3) = "eaw" Then
  14.         iRow = iRow + 1
  15.         Sheet2.Cells(iRow, 1) = Right(sLine, Len(sLine) - 4)
  16.     ElseIf sLine = "TOTNSUB" Then
  17.         Line Input #iFile, sLine
  18.         Sheet2.Cells(iRow, 2) = sLine
  19.     ElseIf sLine = "TOTNSUBA" Then
  20.         Line Input #iFile, sLine
  21.         Sheet2.Cells(iRow, 3) = sLine
  22.     End If
  23. Wend
  24. Close iFile
  25. End Sub
复制代码
如果需要求出的都是TOTNSUB和TOTNSUBA这两项的话,就可以用这个搞定了。
其中数据的“文本.txt”要和EXCEL文件在同一目录里
回复

使用道具 举报

发表于 2011-7-26 22:46 | 显示全部楼层
qing33670000 发表于 2011-7-26 22:46
回复 放浪形骸 的帖子

就是要查找文本中的TOTNSUB和TOTNSUBA的值,提取到EXCEL中,还有要提取eaw CQGS04中 ...

搞定了。你看下
回复

使用道具 举报

 楼主| 发表于 2011-7-26 23:10 | 显示全部楼层
回复 放浪形骸 的帖子

怎么我把你的代码输入到VBE中执行,没有反应的?
回复

使用道具 举报

发表于 2011-7-26 23:21 | 显示全部楼层
qing33670000 发表于 2011-7-26 23:10
回复 放浪形骸 的帖子

怎么我把你的代码输入到VBE中执行,没有反应的?

你看下,结果应该是出现在了SHEET2里。。
回复

使用道具 举报

发表于 2011-7-26 23:22 | 显示全部楼层    本楼为最佳答案   
本帖最后由 放浪形骸 于 2011-7-26 23:23 编辑

修改下,结果出现在SHEET1里
  1. Sub ReadFile()
  2. Dim iFile As Integer
  3. iFile = FreeFile
  4. Dim sLine As String
  5. Dim iRow As Integer
  6. Sheet1.Cells(1, 2) = "TOTNSUB"
  7. Sheet1.Cells(1, 3) = "TOTNSUBA"
  8. iRow = 1
  9. Open ThisWorkbook.Path & "\文本.TXT" For Input As iFile
  10. While Not EOF(iFile)
  11.     Line Input #iFile, sLine
  12.     sLine = Trim(sLine)
  13.     If Left(sLine, 3) = "eaw" Then
  14.         iRow = iRow + 1
  15.         Sheet1.Cells(iRow, 1) = Right(sLine, Len(sLine) - 4)
  16.     ElseIf sLine = "TOTNSUB" Then
  17.         Line Input #iFile, sLine
  18.         Sheet1.Cells(iRow, 2) = sLine
  19.     ElseIf sLine = "TOTNSUBA" Then
  20.         Line Input #iFile, sLine
  21.         Sheet1.Cells(iRow, 3) = sLine
  22.     End If
  23. Wend
  24. Close iFile
  25. End Sub
复制代码



回复

使用道具 举报

 楼主| 发表于 2011-7-26 23:37 | 显示全部楼层
回复 放浪形骸 的帖子

嗯,看到了,非常感谢!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-26 16:12 , Processed in 0.202175 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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