我是放在模块里的。建议你也放到模块里。
程序代码:
Option Explicit
Public Type AssemblyType
AssemblyNumber As String
Description As String
QuantitySetup As String
End Type
Public Type ComponentType2
AvailStock As String
Qtytopull As String
Vendor As String
Batch As String
MvmtReqdWorkcenter As String
End Type
Public Type ComponentType
ComponentNumber As String
DescriptionTypeSecBin As String
AvailCount As Long
Avail() As ComponentType2
End Type
Public Type Pull_List_Tpye
Type As String
Plant As String
IndividualRefno As String
Parentreferencenumber As String
Storagelocationsforremoval As String
Storagelocationforreplenishment As String
Exclusions As String
CostCenter As String
Assembly() As AssemblyType
Component() As ComponentType
End Type
Public Sub Red_Pull_List(FileName As String, ByRef d As Pull_List_Tpye)
'D 是按显式申明地址传递的,该变量保存读取后的结果
If Dir(FileName) = "" Then Exit Sub
Dim File_Zt As Long '0=未读取,1=未进入头,2=头,3=Assembly,4=Component Title ,5=Component
File_Zt = 0
Dim fr As Long
fr = FreeFile
Dim s As String
Dim assemblycount As Long
Dim Componentcount As Long
assemblycount = -1
Componentcount = -1
Open FileName For Input As #1
File_Zt = 1
Do While Not EOF(fr)
Line Input #fr, s
Select Case File_Zt
Case 0, 1
If InStr(1, s, "Pull List") > 0 Then
File_Zt = 2
End If
Case 2
If InStr(1, s, "Type") > 0 Then
d.Type = Trim(Mid(s, 6))
ElseIf InStr(1, s, "Plant") > 0 Then
d.Plant = Trim(Mid(s, 7))
ElseIf InStr(1, s, "Individual Ref.no") > 0 Then
d.IndividualRefno = Trim(Mid(s, 19))
d.Parentreferencenumber = Trim(Mid(d.IndividualRefno, InStr(1, d.IndividualRefno, "Parent reference number:") + 24))
d.IndividualRefno = Trim(Left(d.IndividualRefno, InStr(1, d.IndividualRefno, "Parent reference number:") - 1))
ElseIf InStr(1, s, "Storage locations for removal") > 0 Then
d.Storagelocationsforremoval = Trim(Mid(s, 31))
ElseIf InStr(1, s, "Storage location for replenishment") > 0 Then
d.Storagelocationforreplenishment = Trim(Mid(s, 36))
ElseIf InStr(1, s, "Exclusions") > 0 Then
d.Exclusions = Trim(Mid(s, 12))
ElseIf InStr(1, s, "Cost Center") > 0 Then
d.CostCenter = Trim(Mid(s, 13))
ElseIf InStr(1, s, "Assembly Number") > 0 Then
File_Zt = 3
End If
Case 3
If InStr(1, s, "-----------") = 0 Then
If InStr(1, s, "Component Number") > 0 Then
File_Zt = 4
Else
If Len(s) > 0 Then
assemblycount = assemblycount + 2
ReDim Preserve d.Assembly(assemblycount)
With d.Assembly(assemblycount - 1)
.AssemblyNumber = Trim(Left(s, 19))
.Description = Trim(Mid(s, 20, 39))
.QuantitySetup = Trim(Mid(s, 58, 18))
End With
If Len(s) > 135 Then
With d.Assembly(assemblycount)
.AssemblyNumber = Trim(Mid(s, 77, 19))
.Description = Trim(Mid(s, 96, 39))
.QuantitySetup = Trim(Mid(s, 134))
End With
Else
assemblycount = assemblycount - 1
ReDim Preserve d.Assembly(assemblycount)
End If
End If
End If
End If
Case 4
If InStr(1, s, "Type Sec Bin") > 0 Then
File_Zt = 5
End If
Case 5
If Len(s) > 0 Then
If InStr(1, s, "-----------") = 0 And Asc(s) <> 124 Then
If Asc(s) > 32 Then
Componentcount = Componentcount + 1
ReDim Preserve (Componentcount)
With (Componentcount)
.AvailCount = -1
.ComponentNumber = Trim(Left(s, 19))
.DescriptionTypeSecBin = Trim(Mid(s, 20))
End With
Else
With (Componentcount)
.AvailCount = .AvailCount + 1
ReDim Preserve .Avail(.AvailCount)
.Avail(.AvailCount).AvailStock = Trim(Mid(s, 45, 18))
.Avail(.AvailCount).Qtytopull = Trim(Mid(s, 64, 14))
.Avail(.AvailCount).Vendor = Trim(Mid(s, 78, 11))
.Avail(.AvailCount).Batch = Trim(Mid(s, 89, 11))
If Len(s) > 127 Then
.Avail(.AvailCount).MvmtReqdWorkcenter = Trim(Mid(s, 100, 27))
Else
.Avail(.AvailCount).MvmtReqdWorkcenter = Trim(Mid(s, 100))
End If
End With
End If
End If
End If
End Select
Loop
Close #1
End Sub
调用:
Dim d As Pull_List_Tpye
Call Red_Pull_List(Text1.Text, d)
Stop
你暂停后看本地数据,就知道如何引用了。
这个TXT,基本上都解析了,就是 文件头和文件尾,还是 每项的 小计没去解析。
代码没怎么去注释,自己慢慢看。应该还算可以,都是比较简单的命令,只是繁琐一些。
----------------
同样未考虑中文问题,如果文件包含中文,会错的一塌糊涂。
[此贴子已经被作者于2015-12-14 16:42编辑过]