Excel精英培训网

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

[已解决]提取txt文件中值

[复制链接]
发表于 2016-12-25 10:30 | 显示全部楼层 |阅读模式
各位大大帮帮忙, 我有一个txt文件 无标题.jpg
提取文档的指定内容到 excel文内。 格式如下:

无标题1.jpg
请老师帮忙看看,该怎么实现这种需求。先谢谢了
33.zip (8.86 KB, 下载次数: 21)
发表于 2016-12-25 18:28 | 显示全部楼层
请测试:

  1. Sub wanao()
  2.     Dim txtLine, FileObj, TextObj, FilePath
  3.     Dim x As Integer
  4.     Set FileObj = CreateObject("Scripting.FileSystemObject")
  5.     FilePath = ThisWorkbook.Path & "\33.txt"
  6.     Set TextObj = FileObj.OpenTextFile(FilePath, 1, True)
  7.     x = Cells(Rows.Count, "a").End(xlUp).Row
  8.     Do While Not TextObj.AtEndOfStream
  9.         txtLine = Trim(TextObj.readline)
  10.         If InStr(txtLine, "interface") Then
  11.             x = x + 1
  12.             Cells(x, 1) = x - 1
  13.             Cells(x, "c") = Replace(txtLine, "interface GigabitEthernet", "")
  14.         End If
  15.         If InStr(txtLine, "eth-trunk") Then
  16.             Cells(x, "d") = txtLine
  17.         End If
  18.         If InStr(txtLine, "description") Then
  19.             Cells(x, "i") = Trim(Replace(txtLine, "description", ""))
  20.         End If
  21.     Loop
  22.     Set TextObj = Nothing
  23.     Set FileObj = Nothing
  24. End Sub
复制代码

回复

使用道具 举报

 楼主| 发表于 2016-12-25 22:11 | 显示全部楼层
非常好呀。 谢谢 wanao2008  如果有注释就好了
回复

使用道具 举报

 楼主| 发表于 2016-12-25 23:06 | 显示全部楼层
无标题2.jpg 还有一个小瑕疵
回复

使用道具 举报

发表于 2016-12-26 19:41 | 显示全部楼层    本楼为最佳答案   
本帖最后由 wanao2008 于 2016-12-27 20:29 编辑

请测试:
  1. Sub wanao()
  2.     Dim txtLine, FileObj, TextObj, FilePath
  3.     Dim x As Integer
  4.     Set FileObj = CreateObject("Scripting.FileSystemObject")
  5.     FilePath = ThisWorkbook.Path & "\33.txt"
  6.     Set TextObj = FileObj.OpenTextFile(FilePath, 1, True)
  7.     x = Cells(Rows.Count, "a").End(xlUp).Row
  8.     Do While Not TextObj.AtEndOfStream
  9.         txtLine = Trim(TextObj.readline)
  10.         If InStr(txtLine, "interface") Then
  11.             x = x + 1
  12.             Cells(x, 1) = x - 1
  13.             Cells(x, "c") = "'" & Replace(txtLine, "interface GigabitEthernet", "")
  14.         End If
  15.         If InStr(txtLine, "eth-trunk") Then
  16.             Cells(x, "d") = txtLine
  17.         End If
  18.         If InStr(txtLine, "description") Then
  19.             Cells(x, "i") = Trim(Replace(txtLine, "description", ""))
  20.         End If
  21.         If InStr(txtLine, "shutdown") Then
  22.             Cells(x, "e") = txtLine
  23.         End If
  24.     Loop
  25.     Set TextObj = Nothing
  26.     Set FileObj = Nothing
  27. End Sub
复制代码


回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 04:49 , Processed in 0.339823 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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