ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 6241|回复: 14

[求助] 如何将此文本文件中的数据转换至Excel表格,有请各位大侠多多指教

[复制链接]

TA的精华主题

TA的得分主题

发表于 2004-2-13 13:24 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
pfw77uiu.txt (6.12 KB, 下载次数: 185) <BR>

TA的精华主题

TA的得分主题

发表于 2004-2-13 13:53 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
FYI tssCtvIg.rar (2 KB, 下载次数: 97) <BR>

TA的精华主题

TA的得分主题

 楼主| 发表于 2004-2-13 14:18 | 显示全部楼层

多谢赐教!是我没有说清楚,我想把这个文本文件转换成此形式。

OvVVGqEK.rar (1.76 KB, 下载次数: 84) <BR>

TA的精华主题

TA的得分主题

发表于 2004-2-13 15:05 | 显示全部楼层
可以直接复制到excel表格中你试试看

TA的精华主题

TA的得分主题

 楼主| 发表于 2004-2-17 16:34 | 显示全部楼层
你不是开玩笑吧?象你说的那样复制后,得到的文件又有什么用呢?先仔细看看我上转文件包吧!在此还是谢谢你!

TA的精华主题

TA的得分主题

发表于 2004-2-17 16:54 | 显示全部楼层
楼主的问题正是EXCEL/VBA的长处。不是很复杂,录制一个宏导入文本文件,稍加修改整理即可。但编程过程却要花一定时间。
[此贴子已经被作者于2004-2-17 16:56:43编辑过]

TA的精华主题

TA的得分主题

发表于 2004-2-17 20:57 | 显示全部楼层
Sub Macro1()<BR>Dim R As Long<BR>Dim I As Long<BR>Dim iPath As String '目录<BR>Dim iName As String '文件名称<BR>iPath = "D:\My Documents\tel\"<BR>iName = "txet_11.txt"<BR>Application.ScreenUpdating = False<BR>                         With ActiveSheet.QueryTables.Add(Connection:= _<BR>                                                         "TEXT;" &amp; iPath &amp; iName, Destination:=Range("A1"))<BR>                                                         .FieldNames = False<BR>                                                         .RowNumbers = False<BR>                                                         .FillAdjacentFormulas = False<BR>                                                         .PreserveFormatting = True<BR>                                                         .RefreshOnFileOpen = False<BR>                                                         .RefreshStyle = xlInsertDeleteCells<BR>                                                         .AdjustColumnWidth = True<BR>                                                         .RefreshPeriod = 0<BR>                                                         .TextFilePromptOnRefresh = False<BR>                                                         .TextFilePlatform = 936<BR>                                                         .TextFileStartRow = 6<BR>                                                         .TextFileParseType = xlDelimited<BR>                                                         .TextFileTextQualifier = xlTextQualifierDoubleQuote<BR>                                                         .TextFileConsecutiveDelimiter = True<BR>                                                         .TextFileTabDelimiter = True<BR>                                                         .TextFileSemicolonDelimiter = False<BR>                                                         .TextFileCommaDelimiter = False<BR>                                                         .TextFileSpaceDelimiter = True<BR>                                                         .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)<BR>                                                         .TextFileTrailingMinusNumbers = True<BR>                                                         .Refresh BackgroundQuery:=False<BR>                         End With<BR>                         Range("A1").Delete Shift:=xlToLeft<BR>                         I = Val(Mid$(Range("A65536").End(xlUp).Address, 4, 10))<BR>                         With Range(Cells(1, 1), Cells(I, 7))<BR>                                                         .Borders(xlDiagonalDown).LineStyle = xlNone<BR>                                                         .Borders(xlDiagonalUp).LineStyle = xlNone<BR>                                                         For R = 1 To 12<BR>                                                                                         If R &lt;&gt; 5 And R &lt;&gt; 6 Then<BR>                                                                                                                         With .Borders(R)<BR>                                                                                                                                                         .LineStyle = xlContinuous<BR>                                                                                                                                                         .Weight = xlThin<BR>                                                                                                                                                         .ColorIndex = xlAutomatic<BR>                                                                                                                         End With<BR>                                                                                         End If<BR>                                                         Next<BR>                         End With<BR>                         For R = I To 2 Step -1<BR>                                                         If Len(Cells(R, 1)) = 0 Then<BR>                                                                                         Cells(R, 1).EntireRow.Delete<BR>                                                         ElseIf IsNumeric(Cells(R, 1)) = False Then<BR>                                                                                         Cells(R, 1).EntireRow.Delete<BR>                                                         End If<BR>                         Next<BR>                         Range(Cells(1, 1), Cells(I, 6)).Columns.AutoFit<BR>                         Range("a2").Select<BR>End Sub<BR>

TA的精华主题

TA的得分主题

 楼主| 发表于 2004-2-18 10:35 | 显示全部楼层
<DIV class=quote><B>以下是引用<I>ExcelMan在2004-2-17 20:57:00</I>的发言:</B><BR>Sub Macro1()<BR>Dim R As Long<BR>Dim I As Long<BR>Dim iPath As String '目录<BR>Dim iName As String '文件名称<BR>iPath = "D:\My Documents\tel\"<BR>iName = "txet_11.txt"<BR>Application.ScreenUpdating = False<BR>                         With ActiveSheet.QueryTables.Add(Connection:= _<BR>                                                         "TEXT;" &amp; iPath &amp; iName, Destination:=Range("A1"))<BR>                                                         .FieldNames = False<BR>                                                         .RowNumbers = False<BR>                                                         .FillAdjacentFormulas = False<BR>                                                         .PreserveFormatting = True<BR>                                                         .RefreshOnFileOpen = False<BR>                                                         .RefreshStyle = xlInsertDeleteCells<BR>                                                         .AdjustColumnWidth = True<BR>                                                         .RefreshPeriod = 0<BR>                                                         .TextFilePromptOnRefresh = False<BR>                                                         .TextFilePlatform = 936<BR>                                                         .TextFileStartRow = 6<BR>                                                         .TextFileParseType = xlDelimited<BR>                                                         .TextFileTextQualifier = xlTextQualifierDoubleQuote<BR>                                                         .TextFileConsecutiveDelimiter = True<BR>                                                         .TextFileTabDelimiter = True<BR>                                                         .TextFileSemicolonDelimiter = False<BR>                                                         .TextFileCommaDelimiter = False<BR>                                                         .TextFileSpaceDelimiter = True<BR>                                                         .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)<BR>                                                         .TextFileTrailingMinusNumbers = True<BR>                                                         .Refresh BackgroundQuery:=False<BR>                         End With<BR>                         Range("A1").Delete Shift:=xlToLeft<BR>                         I = Val(Mid$(Range("A65536").End(xlUp).Address, 4, 10))<BR>                         With Range(Cells(1, 1), Cells(I, 7))<BR>                                                         .Borders(xlDiagonalDown).LineStyle = xlNone<BR>                                                         .Borders(xlDiagonalUp).LineStyle = xlNone<BR>                                                         For R = 1 To 12<BR>                                                                                         If R &lt;&gt; 5 And R &lt;&gt; 6 Then<BR>                                                                                                                         With .Borders(R)<BR>                                                                                                                                                         .LineStyle = xlContinuous<BR>                                                                                                                                                         .Weight = xlThin<BR>                                                                                                                                                         .ColorIndex = xlAutomatic<BR>                                                                                                                         End With<BR>                                                                                         End If<BR>                                                         Next<BR>                         End With<BR>                         For R = I To 2 Step -1<BR>                                                         If Len(Cells(R, 1)) = 0 Then<BR>                                                                                         Cells(R, 1).EntireRow.Delete<BR>                                                         ElseIf IsNumeric(Cells(R, 1)) = False Then<BR>                                                                                         Cells(R, 1).EntireRow.Delete<BR>                                                         End If<BR>                         Next<BR>                         Range(Cells(1, 1), Cells(I, 6)).Columns.AutoFit<BR>                         Range("a2").Select<BR>End Sub<BR></DIV><BR>感谢您及朋友们的热心帮助,但我是一个新手对所谓VBA等没有用过,也不知如何下手。想请高手们将方法步骤说得详细些,领我入门。在此多谢啦!

TA的精华主题

TA的得分主题

发表于 2004-2-18 20:28 | 显示全部楼层
我个人认为还用获取外部数据的方法是最快的.见附件:

JOc2jRkA.rar (2.65 KB, 下载次数: 99) <BR>

TA的精华主题

TA的得分主题

发表于 2004-2-18 22:28 | 显示全部楼层
请<b>第<FONT color=#ff0000>9</FONT>楼</b>的同志留意<b>第<FONT color=#ff0000>3</FONT>楼</b>内容


步骤:


1、随便生成1文件,按下“Alt+F11”进入vb编辑器


2、点击菜单“插入”,再点击“模块”,


3、将我回帖内容复制到其中,以下两句以实际为准,


                         iPath = "D:\My Documents\tel\"<BR>                         iName = "txet_11.txt"


                 如文件在“C:\”下,名称为“text_911”时,则应改为<BR>                         iPath = "C:\"<BR>                         iName = "txet_911.txt"


4、按下“Alt+F11”返回工作表界面,然后按下“Alt+F8”,再按下“执行”或直接按回车键。


       


程序分为两部分,第一部分是“Range("A1").Delete Shift:=xlToLeft”以上的语句,作用是调入文件内容;其余是第二部分,是整理数据,因为原文本文件的数据不规则。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-26 05:12 , Processed in 0.048573 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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