VBA 读取文本文件
2016-05-05 23:41
375 查看
Excel在我们的日常办公中,用到非常多。因此,对于它的脚本VBA的使用,难免也比较频繁。在此总结、整理下之前写过的一段VBA读取MAP文件,统计ECU存储空间使用情况的部分代码。
该段程序,先使用Collection集合类装载读取的数据,最后统一写入将数据按照期望的格式,写入Excle表格中。这样一次性读取、写入,可以避免多次在读MAP文件和写Excel文件之间切换,增加系统资源开销。
该段程序,先使用Collection集合类装载读取的数据,最后统一写入将数据按照期望的格式,写入Excle表格中。这样一次性读取、写入,可以避免多次在读MAP文件和写Excel文件之间切换,增加系统资源开销。
Open ThisWorkbook.Path & "\XXX.MAP" For Input As #1 lineNum = 0 FundStrtLineNum = 0 bFoundStart = False '读取TXT文件 Do While Not EOF(1) lineNum = lineNum + 1 Line Input #1, txt If txt = "Memory map:" Then FundStrtLineNum = lineNum bFoundStart = True End If If bFoundStart Then If lineNum >= (FundStrtLineNum + 3) And lineNum <= (FundStrtLineNum + 35) Then txt = Trim(txt) txt = reg.Replace(txt, " ") tmpArray = Split(txt, " ") 'Collection集合类装载数组 collect.Add tmpArray End If End If Loop Close #1
'Write Data Dim thissht As Worksheet Set thissht = ThisWorkbook.Worksheets("MemMap") Debug.Print "the current sheet index is:" & thissht.Index thissht.Range("A:K").NumberFormatLocal = "@" '阻止数字变成科学计数法 Dim loopi As Integer Dim loopj As Integer For loopi = 1 To collect.Count For loopj = LBound(collect(loopi)) To UBound(collect(loopi))
'LBound:得到数组的最小下标 UBound:返回最大下标 If loopj <> 1 Then thissht.Range("D3").Offset(loopi - 1, loopj).Value = "'" & collect.Item(loopi)(loopj) Else thissht.Range("D3").Offset(loopi - 1, loopj).Value = collect.Item(loopi)(loopj) End If Next Next
相关文章推荐
- 使用Python生成Excel格式的图片
- Outlook 批量发送邮件
- Excel 曝出 Power Query 安全漏洞,1.2 亿用户易受远程 DDE 攻击
- Shell 脚本编程陷阱
- VBA将excel数据表生成JSON文件
- excel vba 限制工作表的滚动区域代码
- VBA解决Windows空当接龙的617局
- excel vba 高亮显示当前行代码
- VBS脚本写的Windows硬件检测工具分享
- 用vbscript实现隐藏任务栏图标的脚本
- 用autoit编写第一个脚本(Hello World)
- VBS调用WMI快速关闭IE的脚本
- Oracle数据库执行脚本常用命令小结
- 收集的ROS防火墙脚本
- JSP脚本漏洞面面观
- 不错的批处理脚本 第一部分
- 自动化安装smokeping-2.6.11脚本 推荐