|
发表于 2017-2-11 12:25
|
显示全部楼层
本楼为最佳答案
- Sub tst()
- Application.ScreenUpdating = False
- On Error Resume Next
- ipath = ThisWorkbook.Path & ""
- ifile = ipath & "原数据.txt"
- Open ifile For Input As #1
- Do While Not EOF(1)
- Line Input #1, itxt
- s1 = s1 & itxt & vbTab
- Loop
- [A1].Resize(UBound(Split(s1, vbTab)), 1) = Application.Transpose(Split(s1, vbTab))
- Close #1
- jfile = ipath & "替换数据.txt"
- Open jfile For Input As #2
- Do While Not EOF(2)
- Line Input #2, itxt
- s2 = s2 & itxt & vbTab
- Loop
- [B1].Resize(UBound(Split(s2, vbTab)), 1) = Application.Transpose(Split(s2, vbTab))
- Close #2
- a = Range("A1:A" & [A1].End(4).Row)
- b = Range("B1:B" & [B1].End(4).Row)
- If UBound(b) < UBound(a) / 2 Then MsgBox "待替换的文本个数不够": Exit Sub
- For i = 1 To UBound(a)
- If i Mod 2 = 0 Then
- a(i, 1) = Replace(a(i, 1), "123456", b(i / 2, 1))
- End If
- Next
- kfile = ipath & "替换ok.txt"
- Open kfile For Output As #1
- For i = 1 To UBound(a)
- Print #1, a(i, 1)
- Next
- Close #1
- Range("A1:B" & Application.Max(UBound(a), UBound(b))).ClearContents
- MsgBox "替换OK!"
- Application.ScreenUpdating = True
- End Sub
- 请测试!
复制代码 |
|