如何从第20行开始,从文件夹中的多个文本文件导入数据,同时使用excel VBA将当前文件名添加到第一列
我是stackoverflow的新手,也是一名新手程序员,希望我的提问方式和地点正确,这样我可以得到一些帮助。 问题是: 我有一个文件夹c:/SENSOR/其中包含多个名为“LANCE 60.txt”、“LANCE 61.txt”、“LANCE 62.txt”等文件 每个文件的格式如下:如何从第20行开始,从文件夹中的多个文本文件导入数据,同时使用excel VBA将当前文件名添加到第一列,excel,vba,Excel,Vba,我是stackoverflow的新手,也是一名新手程序员,希望我的提问方式和地点正确,这样我可以得到一些帮助。 问题是: 我有一个文件夹c:/SENSOR/其中包含多个名为“LANCE 60.txt”、“LANCE 61.txt”、“LANCE 62.txt”等文件 每个文件的格式如下: File name | Date | Time | Temp | Depth | Salinity | Conductivity | Speed | 例如: #B Created: 18.02.20
File name | Date | Time | Temp | Depth | Salinity | Conductivity | Speed |
例如:
#B Created: 18.02.2017 08:10:14
#5 Field separation: 0
#2 Date & Time: 1
#6 Decimal point: ,
#7 Date def.: dd.mm.yyyy .
#8 Time def.: :
## Axis 0 Temperature(°C) clRed FALSE
## Axis 1 Depth(m) clBlue TRUE
## Axis 2 Salinity(psu) clGreen FALSE
## Axis 3 Conductivity(mS/cm) $00FF80FF FALSE
## Axis 4 Sound Velocity(m/sec) $00404080 FALSE
## Series 0 Temp(°C) clRed 0 Temp(°C) 2
## Series 1 Depth(m) clBlue 1 Depth(m) 2
## Series 2 Salinity(psu) clGreen 2 Salinity(psu) 2
## Series 3 Conduct(mS/cm) $00FF80FF 3 Conduct(mS/cm) 2
## Series 4 Sound Velocity(m/sec) $00404080 4 Velocity(m/sec) 2
## Recorder 10 DST CTD 8771
## Chart 10S8771DAT
#D Data: 248 16.02.2017 14:50:00 18.02.2017 08:00:00
1 16.02.2017 14:50:00 22,60 8,65 0,00 0,00 1489,74
2 16.02.2017 15:00:00 8,61 77,46 31,24 33,24 1481,28
3 16.02.2017 15:10:00 5,66 244,84 31,53 31,09 1472,82
4 16.02.2017 15:20:00 5,22 415,69 31,59 30,86 1473,94
5 16.02.2017 15:30:00 4,75 586,96 31,64 30,58 1474,88
6 16.02.2017 15:40:00 4,42 755,45 31,61 30,35 1476,25
...
我只需要在一个excel工作表中添加所有文件(跳过包含我根本不需要的信息的前19行,并在数据实际开始时盯着第20行),但我需要首先从输入的每一行中删除行号,而是将TXT文件名包含在结果工作表的第一列中
以便得到如下所示的图纸:
File name | Date | Time | Temp | Depth | Salinity | Conductivity | Speed |
我从一个类似的问题中借用了一些代码,并根据我的目的对其进行了修改。。。
到目前为止,我已经设法从第20行开始,删除每个文件的行号,并将一个文件接着一个文件添加到工作表中,但我找不到在第一列中包含文件名的方法
Sub Sample()
Dim myfiles
Dim i As Integer
myfiles = Application.GetOpenFilename(filefilter:="TXT Files (*.txt),
*.txt", MultiSelect:=True)
If Not IsEmpty(myfiles) Then
For i = LBound(myfiles) To UBound(myfiles)
With ActiveSheet.QueryTables.Add(Connection:= _"TEXT;" & myfiles(i), Destination:=Range("A" & Rows.Count).End(xlUp).Offset(1, 0))
.Name = "Sample"
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 20
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(xlSkipColumn, xlDMYFormat, xlGeneralFormat, xlGeneralFormat, xlGeneralFormat, xlGeneralFormat, xlGeneralFormat, xlGeneralFormat)
.TextFileTrailingMinusNumbers = True
.TextFileThousandsSeparator = " "
.TextFileDecimalSeparator = ","
.Refresh BackgroundQuery:=False
End With
Next i
Else
MsgBox "No File Selected"
End If
End Sub
(我也希望代码不要询问读取哪些文件以及从何处读取,而是自动使用路径“C:\SENSOR\”)
如果有人能提供任何帮助,我们将不胜感激!:) 必须修改这一点
您的文本文件没有正确分隔。所以我用空格分割它们,然后将它们放入以制表符分隔的数组中,以便将它们写入7列范围。在空白纸上试一试,看看是否适合你。对我来说没问题
Sub readWrite()
Dim fileNum As Integer, dataLine As String, arrayCounter As Long, mySplitArray, tempSplit
Dim myfiles, fileName As String
Dim i As Integer, myArray()
arrayCounter = 0
ReDim myArray(1 To 1)
myfiles = Application.GetOpenFilename(filefilter:="TXT Files (*.txt), *.txt", MultiSelect:=True)
If Not IsEmpty(myfiles) Then
For i = LBound(myfiles) To UBound(myfiles)
fileName = myfiles(i)
tempSplit = Split(fileName, "\")
fileName = tempSplit(UBound(tempSplit))
fileNum = FreeFile()
Open myfiles(i) For Input As #fileNum
While Not EOF(fileNum)
Line Input #fileNum, dataLine
If left(dataLine, 1) <> "#" Then
arrayCounter = arrayCounter + 1
ReDim Preserve myArray(1 To arrayCounter)
myArray(arrayCounter) = fileName & " " & dataLine
End If
Wend
For k = LBound(myArray) To UBound(myArray)
myString = ""
mySplitArray = Split(myArray(k), " ")
For j = LBound(mySplitArray) To UBound(mySplitArray)
If mySplitArray(j) <> "" Then
If j = 0 Then
myString = myString & mySplitArray(j) & " " & mySplitArray(j + 1)
Else
If j <> 1 And j <> 2 Then
myString = myString & vbTab & mySplitArray(j)
End If
End If
End If
Next j
Range("A" & k & ":H" & k) = Split(myString, vbTab)
' Debug.Print myString
Next k
Close fileNum
Next i
End If
End Sub
Sub readWrite()
Dim fileNum为整数,dataLine为字符串,arrayCounter为长,mySplitArray,tempSplit
Dim myfiles,文件名为字符串
Dim i作为整数,myArray()
arrayCounter=0
重拨myArray(1对1)
myfiles=Application.GetOpenFilename(文件过滤器:=“TXT文件(*.TXT),*.TXT”,MultiSelect:=True)
如果不是IsEmpty(我的文件),那么
对于i=LBound(myfiles)到UBound(myfiles)
fileName=myfiles(i)
tempslit=Split(文件名“\”)
fileName=tempslit(UBound(tempslit))
fileNum=FreeFile()
打开myfiles(i)作为#fileNum输入
而不是EOF(fileNum)
行输入#fileNum,数据行
如果左(数据线,1)“#”,则
arrayCounter=arrayCounter+1
ReDim保留myArray(1到arrayCounter)
myArray(arrayCounter)=文件名和数据行
如果结束
温德
对于k=LBound(myArray)到UBound(myArray)
myString=“”
mySplitArray=Split(myArray(k),“”)
对于j=LBound(mySplitArray)到UBound(mySplitArray)
如果mysplitaray(j)“,那么
如果j=0,则
myString=myString&mySplitArray(j)&“&mySplitArray(j+1)
其他的
如果j 1和j 2,那么
myString=myString&vbTab&mySplitArray(j)
如果结束
如果结束
如果结束
下一个j
范围(“A”&k&“:H”&k)=拆分(myString,vbTab)
'Debug.Print myString
下一个k
关闭fileNum
接下来我
如果结束
端接头
如果您不需要该对话框,您可以硬编码文件名值,如myfiles=“C:\SENSOR*.txt”,但我认为如果文件夹中有多个文件,您可能会遇到问题。该文件夹中有大量文件!如果不想对循环进行编码,那么可能需要保持文件提示!好啊好的,添加文件名有什么想法吗?你应该可以添加myfiles变量作为数组中的另一列。哇!谢谢你,约翰!差不多了。。。但是有两个问题:在第一列中,我只需要文件名(没有路径,没有文件扩展名),并且我丢失了日期:(加上它添加了一个填充有“#N/a”的列)示例输出行:C:\SENSOR\LANCE 138.txt 14:50:00 22,60 8,65 0,00 0,00 1489,74#N/AI必须修改上述答案以删除单个数字并替换为文件名。我再次更改了它,因此它现在应该只显示文件名,而不是路径。当然,此代码取决于示例文本文件的结构。如果cture更改,则代码可能无法工作,您必须对其进行修改。但只要您的文本文件采用相同的格式,您就不会有问题。(所有文件都具有相同的结构)现在我遇到了不同的问题。示例输出行:LANCE 1 16.02.2017 14:50:00 22,60 8,65 0,00 0,00 LANCE 2 16.02.2017 15:00:00 8,61 77,46 31,24 33,24因此文件名缺少标识符(它们都称为“LANCE”,而不是“LANCE 01”,“LANCE 02”..第二列保留了输入的行号。我误解了数据“速度”的最后一列