我有一个用户可选择的文件夹,其中包含 128 个文件。在我的代码中,我打开每个文档并将相关数据复制到我的主工作簿中。所有这些都通过用户表单进行控制。我的问题是完成此过程所需的时间(大约 50 秒) - 我肯定可以在不打开文档的情况下完成它吗?
此代码用于选择要搜索的目录:
Private Sub CBSearch_Click()
Dim Count1 As Integer
ChDir "Directory"
ChDrive "C"
Count1 = 1
inputname = Application.GetOpenFilename("data files (*.P_1),*.P_1")
TBFolderPath.Text = CurDir()
End Sub
这检索文件:
Private Sub CBRetrieve_Click()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim i As Integer
Dim StrLen As Integer
Dim Folder As String
Dim A As String
Dim ColRef As Integer
Open_Data.Hide
StrLen = Len(TBFolderPath) + 1
Folder = Mid(TBFolderPath, StrLen - 10, 10)
For i = 1 To 128
A = Right("000" & i, 3)
If Dir(TBFolderPath + "\" + Folder + "-" + A + ".P_1") <> "" Then
Workbooks.OpenText Filename:= _
TBFolderPath + "\" + Folder + "-" + A + ".P_1" _
, Origin:=xlMSDOS, StartRow:=31, 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)), TrailingMinusNumbers:=True
Columns("B:B").Delete Shift:=xlToLeft
Rows("2:2").Delete Shift:=xlUp
Range(Range("A1:B1"), Range("A1:B1").End(xlDown)).Copy
Windows("Document.xls").Activate
ColRef = (2 * i) - 1
Cells(15, ColRef).Select
ActiveSheet.Paste
Windows(Folder + "-" + A + ".P_1").Activate
ActiveWindow.Close
End If
Next i
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
TBFolderPath 是用户表单中文本框的内容,是文件的位置。
对不起,我的代码太乱了!
编辑:数据的一个例子是:
TA2000 PLOT DATA FILE
FileName: c:\file
Version: 3.01
PlotNumber: 1
TotalPoints: 982
FrIndex: 460
F1Index: 427
F2Index: 498
FaIndex: 513
Transducer Type: 8024-004-A9
Serial Number:
Date: 09-Aug-2013
Operator: LSP
20-80kHz
Time: 10:51:35
Clf pF:
Range mS: 0.5
Aut/Man: Auto
Shunt pF:
Shunt uH:
Step size: 150 Hz
Rate: Max
Start: 1.0
Stop: 150.0
A---------B-------------C--------------D--------E
0---------0.003695---1.000078---0.2-----12
0---------0.004018---1.150238---0.2-----12
.
.
.
我对 A 和 C 感兴趣的地方。数据有大约 1000 个条目。