我必须将选定目录中的十几个 txt 文件加载到数组或 Excel 表中。txt文件结构如下:
*
SST - 0010
Narzędzie - 08A38902
Miernik 0010 Nr seryjny = 90375091 Nr artykułu = 1010953
Moment obrotowy = 2,080 N.m Kąt obrotu = 5380,000 grd
Wartość zadana = 5,000 N.m DG = 0,000 N.m GG = 10,000 N.m
Kąt docelowy = 0,000 grd Moment docelowy = 5,000 N.m
Wartość progowa = 0,200 N.m Wartość dokr. = 5,000 N.m
wartość KPIL = Wył. Czas martwy = 0,00 s Współcz.nach. = > 1,00 Prędkość
kątowa = 0,000
Cm = 2.42 Cmk = 1.04 Xpoprz = 2.15
Czas [s] Kanał 1 [N.m] Kanał 2 [grd]
0 0,21 0
0,008 0,23 18
0,016 0,24 40,5
0,024 0,26 59,5
0,032 0,27 87,5
0,04 0,28 112,5
0,048 0,3 137,5
...
...
...
*
我必须将第 14 行的行加载到 EndOfFile。
数据分为 3 列,以表格分隔。我想将数据复制到 3 个 excel 列中以供进一步使用。
每个文件都应该加载到下一组列中。
如果这不是问题,我更喜欢使用嵌入在工作表中的按钮来运行宏。
我真的尝试了不同的方法来完成这项任务,但我失败了,所以我请求你的帮助:)。
上次我尝试过这段代码:
Sub LOAD_REAL_DATA()
Dim Filt As String
Dim FilterIndex As Integer
Dim Title As String
Dim FileName As Variant
Filt = "All Files (*.*),*.*"
Title = "Select a Txt File to Import"
FileName = Application.GetOpenFilename(FileFilter:=Filt, Title:=Title)
If FileName = False Then
MsgBox "No File Was Selected"
Exit Sub
End If
With Application.ActiveSheet
Cells.Select
Selection.QueryTable.Delete
Selection.ClearContents
End With
Workbooks.Open FileName
End Sub
我收到“400 错误”消息...
使用这些代码,它完成了大部分工作,但在 L42 回复下的评论中列出了一些问题。
Sub LOAD_TOOL_DATA()
Dim a, b, c As Integer
Dim TARFIL
On Error GoTo nofile
TEMPNAM = ActiveWorkbook.Name
Application.ScreenUpdating = False
TARFIL = Application.GetOpenFilename(filefilter:="All Files (*.*), *.*", MultiSelect:=True)
'Set multiselect to true so you can select all file you want to load
b = UBound(TARFIL, 1) 'get the size of the array of files you just created
c = 1
'Loop through those files
Do
Sheets("Arkusz1").Select
a = 1
'this loop is to ensure you do not copy same files
Do
Select Case Cells(a, 1).Value
Case TARFIL(c)
GoTo jump
Case ""
Cells(a, 1).Value = TARFIL(c)
x = 1
Case Else
a = a + 1
x = 0
End Select
Loop Until x = 1
'this part opens the filename. In this case the txt file have 12 colums.
' if you have fewer columns then delete some Array(x,x) on the FieldInfo: part. You can also get this by recording Macro.
Workbooks.OpenText FileName:=TARFIL(c), startRow:=14, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False _
, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 2), Array(8, 1), _
Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1))
OPNFIL = ActiveWorkbook.Name
'this part specifies that it will only copy data from row 5 as indicated
Range(Cells(5, 1), Cells(Application.WorksheetFunction.CountA(Columns("A:A")) + 1, 12)).Select
Selection.Copy
Windows(TEMPNAM).Activate
Sheets("Arkusz1").Select
Cells(Application.WorksheetFunction.CountA(Columns("A:A")) + 1, 1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Windows(OPNFIL).Close
jump:
c = c + 1
Loop Until c > b
Exit Sub
nofile:
' MsgBox "No File Selected", vbInformation, "Load File Error"
End Sub
好的,伙计们,这段代码几乎可以完美运行,但是:;)
Sub LOAD_TOOL_DATA()
Dim a, b, c As Integer
Dim TARFIL 'Array for the file data
On Error GoTo nofile
TEMPNAM = ActiveWorkbook.Name
Application.ScreenUpdating = False
TARFIL = Application.GetOpenFilename(filefilter:="All Files (*.*), *.*", MultiSelect:=True)
'Set multiselect to true so you can select all file you want to load
b = UBound(TARFIL, 1) 'get the size of the array of files you just created
c = 1
'Loop through those files
Do
Sheets(8).Select
a = 1
'This loop is to ensure you do not copy same files
Do
Select Case Cells(a, 1).Value
Case TARFIL(c)
GoTo jump
Case ""
Cells(a, 1).Value = TARFIL(c)
x = 1
Case Else
a = a + 1
x = 0
End Select
Loop Until x = 1
'this part opens the filename. In this case the txt file have 3 colums.
' if you have fewer/ more columns then delete/ add some Array(x,x) on the FieldInfo: part (where (x,x) is (column, row) index.
Workbooks.OpenText FileName:=TARFIL(c), startRow:=14, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1))
OPNFIL = ActiveWorkbook.Name
'this part specifies that it will only copy data from row 1 to EOF and from column 1 to 3
Range(Cells(1, 1), Cells(Application.WorksheetFunction.CountA(Columns("A:A")) + 1, 3)).Select
Selection.Copy
Windows(TEMPNAM).Activate
Sheets(8).Select
Cells(Application.WorksheetFunction.CountA(Columns("A:A")) + 1, 1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Windows(OPNFIL).Close
jump:
c = c + 1
Loop Until c > b
Application.ScreenUpdating = True
Exit Sub
nofile:
' MsgBox "No File Selected", vbInformation, "Load File Error"
End Sub
- 多文件选择不起作用,
- 在第一行的目标工作表中,它使用文件名粘贴文件路径(我不需要),
如何修改以选择不同的目标(其他工作表和单元格地址 - 比如说 B9- 到 EOF)?