我有一个包含大量文本文件的文件夹,每个文件包含(但是以随机顺序):
A = ...
B = ...
C = ...
现在我想将这些文本文件导入excel-spreadsheet, 其中每个前缀都在列中组织,文件列为行
示例:2个文件
文件1:
A = 1
B = 2
C = 3
文件2:
A = 4
B = 5
C = 6
我会擅长看起来像:
NR / A / B / C
1/1/2/3
2/4/5/6
我还在学习VB,这对我来说有点过头了。
我找到了这样一个宏:
Sub Read_Text_Files()
Dim sPath As String, sLine As String
Dim oPath As Object, oFile As Object, oFSO As Object
Dim r As Long
'Files location
sPath = "C:\Test\"
r = 1
Set oFSO = CreateObject( _
"Scripting.FileSystemObject")
Set oPath = oFSO.GetFolder(sPath)
Application.ScreenUpdating = False
For Each oFile In oPath.Files
If LCase(Right(oFile.Name, 4)) = ".txt" Then
Open oFile For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Loop until end of file.
Input #1, sLine ' Read data
If Left(sLine, 1) = "A=" Then 'Now i need to write this to the first column of that row
If Left(sLine, 1) = "B=" Then 'For the second column.
Range("A" & r).Formula = sLine ' Write data line
r = r + 1
Loop
Close #1 ' Close file.
End If
Next oFile
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:1)
如果没有,请阅读:https://stackoverflow.com/a/11528932/2832561 我通过谷歌搜索" VBA打开文件阅读"
找到了这个如果没有,请尝试阅读:http://www.exceltrick.com/formulas_macros/vba-split-function/ 我发现这是通过谷歌搜索" VBA字符串函数解析文本"
如果没有,请尝试阅读:http://www.anthony-vba.kefra.com/vba/vbabasic2.htm 我通过Google搜索"工作簿工作表范围VBA"
找到了这个一旦您有机会尝试使用这些部分组合解决方案,您就可以针对遇到的任何问题发布具体问题。