我正在尝试使用VBA解析文本文档并返回文本文件中给出的路径.
例如,文本文件看起来像:
*Blah blah instructions
*Blah blah instructions on line 2
G:\\Folder\...\data.xls
D:\\AnotherFolder\...\moredata.xls
Run Code Online (Sandbox Code Playgroud)
我希望VBA一次加载1行,如果它以a开头,*则移动到下一行(类似于被注释的那行).对于带有文件路径的行,我想将该路径写入单元格,例如A2第一条路径,B2下一条路径等.
我希望回答的主要内容是:
1.使用VBA读取文本文件的最佳/简单方法是什么?
2.我该如何逐行完成?
我正在尝试使用以下宏读取文本文件(这些文件由外部程序生成,无法编写).
While Not EOF(int_current_file)
Line Input #int_current_file, buf
If Left(buf, 1) <> "#" Then
buf1 = Split(buf, "=")
Print #int_master_file, CInt(Left(buf, 2)) & ";" & CInt(Mid(buf, 3, 4)) & ";" & CInt(Mid(buf, 7, 3)) & ";" & CInt(Mid(buf, 10, 1)) & ";" & CDbl(buf1(1) / 100000000) & ";" & CDate(file_date) & ";" & Mid(file_name, 4, 3)
End If
'Line Input #int_current_file, buf
'Debug.Print Data
Wend
Run Code Online (Sandbox Code Playgroud)
但是,在此文件的第二行,我有以下字符串:
=01082013=01072013=31072013=06082013=1640=380441=21=000001249=#02IFS86.G84=IFSSS5=7?K!?i—?42??4?{¤?o$]?•?p ?1‹;±~†?RL??‰®?? ????^±>_‰
当宏尝试读取此行时,error 62会发生Input past end of file.
我该如何解决这个问题?
我有这个宏来批量导入同一文件夹中包含的excel电子表格100+ .txt文件:
Sub QueryImportText()
Dim sPath As String, sName As String
Dim i As Long, qt As QueryTable
With ThisWorkbook
.Worksheets.Add After:= _
.Worksheets(.Worksheets.Count)
End With
ActiveSheet.Name = Format(Now, "yyyymmdd_hhmmss")
sPath = "C:\Users\TxtFiles\"
sName = Dir(sPath & "*.txt")
i = 0
Do While sName <> ""
i = i + 1
Cells(1, i).Value = sName
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & sPath & sName, Destination:=Cells(2, i))
.Name = Left(sName, Len(sName) - 4)
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False …Run Code Online (Sandbox Code Playgroud) 如何在 Visual Basic 中将 4x4 数组和单个整数保存到文件中?
我正在 Visual Basic 中制作 2048。我有一个名为“Grid”的 4x4 数组和一个名为 score 的整数变量。我希望能够保存我的游戏并从我停止的地方继续。我如何将数组的内容保存到一个文件中,然后将它连同分数一起加载回来?