原贴 http://topic.csdn.net/u/20080425/19/eabb186d-1a47-414c-ab42-df015f83bd9e.html
文件1: 1 ,民V5018, 1.57
1 ,5210, 1.29, 359.5959, 91.0546, 28.521, 180.0003, 268.5425, 28.521
2 ,KK 1, 0, 113.1237, 90.2903, 20.068, 0, 0, 0
15 ,A 4, 0, 222.1614, 89.2742, 53.332, 0, 0, 0
16 ,A 5, 0, 223.4622, 89.4523, 48.679, 0, 0, 0
20 ,民V5017, 2.15, 206.4857, 89.5433, 320.498, 26.491, 270.0529, 320.498
21 ,5212, 1.29, 295.4938, 90.1534, 56.235, 115.4947, 269.4451, 56.236
……
2 ,民V5013, 1.568
22 ,民V5017, 1.29, 0, 90.1854, 56.238, 180.0011, 269.4119, 56.238
23 ,5213, 1.29, 102.4757, 90.3851, 41.473, 282.4806, 269.2124, 41.473
24 ,L 1, 0, 19.2003, 94.0807, 8.691, 0, 0, 0 文件2: z5
5
20
民V5017
民V5018
5212
……
5233
民V5013
民V5017 现在要在文件 1 中查找含有文件 2 第 4 行和最后一行内容(这两行内容相同)的两行数据,分别赋值给变量 A 和 B。
附加条件是,在含有第 5 行(民V5018)的记录之后的含有第 4 行的记录是 A, 而在含有倒数第 2 行数据的记录之后含有最后一行内容的记录是 B. 解结代码为
这样:
Dim strFile As String, arrLine() As String, strTmp As String
Dim index As Integer
Dim reff(1 To 2) As String
Dim cont(1 To 2) As String Dim A As String, B As String
Open "c:/file2.txt" For Binary As #1
strFile = Space(LOF(1) - 4)
Get #1, , strFile
Close #1 arrLine = Split(strFile, vbCrLf) reff(1) = Trim(arrLine(4))
cont(1) = Trim(arrLine(3)) index = UBound(arrLine)
reff(2) = Trim(arrLine(index - 1))
cont(2) = Trim(arrLine(index)) index = 0
Open "c:/file1.txt" For Input As #1
Do Until EOF(1)
Line Input #1, strTmp
If InStr(strTmp, reff(1)) Then index = 1
If InStr(strTmp, reff(2)) Then index = 2
If (index = 1) And InStr(strTmp, cont(1)) Then A = strTmp
If (index = 2) And InStr(strTmp, cont(2)) Then B = strTmp
Loop
Close #1 Debug.Print A
Debug.Print B
为什么B 这行会提取不出呢???
文件1: 1 ,民V5018, 1.57
1 ,5210, 1.29, 359.5959, 91.0546, 28.521, 180.0003, 268.5425, 28.521
2 ,KK 1, 0, 113.1237, 90.2903, 20.068, 0, 0, 0
15 ,A 4, 0, 222.1614, 89.2742, 53.332, 0, 0, 0
16 ,A 5, 0, 223.4622, 89.4523, 48.679, 0, 0, 0
20 ,民V5017, 2.15, 206.4857, 89.5433, 320.498, 26.491, 270.0529, 320.498
21 ,5212, 1.29, 295.4938, 90.1534, 56.235, 115.4947, 269.4451, 56.236
……
2 ,民V5013, 1.568
22 ,民V5017, 1.29, 0, 90.1854, 56.238, 180.0011, 269.4119, 56.238
23 ,5213, 1.29, 102.4757, 90.3851, 41.473, 282.4806, 269.2124, 41.473
24 ,L 1, 0, 19.2003, 94.0807, 8.691, 0, 0, 0 文件2: z5
5
20
民V5017
民V5018
5212
……
5233
民V5013
民V5017 现在要在文件 1 中查找含有文件 2 第 4 行和最后一行内容(这两行内容相同)的两行数据,分别赋值给变量 A 和 B。
附加条件是,在含有第 5 行(民V5018)的记录之后的含有第 4 行的记录是 A, 而在含有倒数第 2 行数据的记录之后含有最后一行内容的记录是 B. 解结代码为
这样:
Dim strFile As String, arrLine() As String, strTmp As String
Dim index As Integer
Dim reff(1 To 2) As String
Dim cont(1 To 2) As String Dim A As String, B As String
Open "c:/file2.txt" For Binary As #1
strFile = Space(LOF(1) - 4)
Get #1, , strFile
Close #1 arrLine = Split(strFile, vbCrLf) reff(1) = Trim(arrLine(4))
cont(1) = Trim(arrLine(3)) index = UBound(arrLine)
reff(2) = Trim(arrLine(index - 1))
cont(2) = Trim(arrLine(index)) index = 0
Open "c:/file1.txt" For Input As #1
Do Until EOF(1)
Line Input #1, strTmp
If InStr(strTmp, reff(1)) Then index = 1
If InStr(strTmp, reff(2)) Then index = 2
If (index = 1) And InStr(strTmp, cont(1)) Then A = strTmp
If (index = 2) And InStr(strTmp, cont(2)) Then B = strTmp
Loop
Close #1 Debug.Print A
Debug.Print B
为什么B 这行会提取不出呢???
http://topic.csdn.net/u/20080504/21/47a6ee2e-c498-4027-bf1e-4a05d1d1af4f.html
1 ,5210, 1.29, 359.5959, 91.0546, 28.521, 180.0003, 268.5425, 28.521
2 ,KK 1, 0, 113.1237, 90.2903, 20.068, 0, 0, 0
15 ,A 4, 0, 222.1614, 89.2742, 53.332, 0, 0, 0
16 ,A 5, 0, 223.4622, 89.4523, 48.679, 0, 0, 0
20 ,民V5017, 2.15, 206.4857, 89.5433, 320.498, 26.491, 270.0529, 320.498
21 ,5212, 1.29, 295.4938, 90.1534, 56.235, 115.4947, 269.4451, 56.236
……
2 ,民V5017, 1.568
22 ,民V5018, 1.29, 0, 90.1854, 56.238, 180.0011, 269.4119, 56.238
23 ,5213, 1.29, 102.4757, 90.3851, 41.473, 282.4806, 269.2124, 41.473
24 ,L 1, 0, 19.2003, 94.0807, 8.691, 0, 0, 0 文件2: z5
5
20
民V5017
民V5018
5212
……
5233
民V5017
民V5018现在要在文件 1 中查找含有文件 2 第 4 行和最后一行内容(这两行内容相同)的两行数据,分别赋值给变量 A 和 B。
附加条件是,在含有第 5 行(民V5018)的记录之后的含有第 4 行的记录是 A, 而在含有倒数第 2 行数据的记录之后含有最后一行内容的记录是 B. 换了这种情况又不行拉