Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1883|回复: 1

[已解决]TXT文件数据替换

[复制链接]
发表于 2017-2-6 21:51 | 显示全部楼层 |阅读模式
本帖最后由 金樽空对月 于 2017-2-12 21:19 编辑

把“原数据”文档中的12个“123456”,分别用“替换数据”文档中的12组数据替换。
最佳答案
2017-2-11 12:25
  1. Sub tst()
  2. Application.ScreenUpdating = False
  3.     On Error Resume Next
  4.     ipath = ThisWorkbook.Path & ""
  5.     ifile = ipath & "原数据.txt"
  6.     Open ifile For Input As #1
  7.         Do While Not EOF(1)
  8.             Line Input #1, itxt
  9.             s1 = s1 & itxt & vbTab
  10.         Loop
  11.         [A1].Resize(UBound(Split(s1, vbTab)), 1) = Application.Transpose(Split(s1, vbTab))
  12.     Close #1
  13.     jfile = ipath & "替换数据.txt"
  14.     Open jfile For Input As #2
  15.         Do While Not EOF(2)
  16.             Line Input #2, itxt
  17.             s2 = s2 & itxt & vbTab
  18.         Loop
  19.         [B1].Resize(UBound(Split(s2, vbTab)), 1) = Application.Transpose(Split(s2, vbTab))
  20.     Close #2
  21.     a = Range("A1:A" & [A1].End(4).Row)
  22.     b = Range("B1:B" & [B1].End(4).Row)
  23.     If UBound(b) < UBound(a) / 2 Then MsgBox "待替换的文本个数不够": Exit Sub
  24.     For i = 1 To UBound(a)
  25.         If i Mod 2 = 0 Then
  26.             a(i, 1) = Replace(a(i, 1), "123456", b(i / 2, 1))
  27.         End If
  28.     Next
  29.     kfile = ipath & "替换ok.txt"
  30.     Open kfile For Output As #1
  31.     For i = 1 To UBound(a)
  32.         Print #1, a(i, 1)
  33.     Next
  34.     Close #1
  35.     Range("A1:B" & Application.Max(UBound(a), UBound(b))).ClearContents
  36.     MsgBox "替换OK!"
  37. Application.ScreenUpdating = True
  38. End Sub

  39. 请测试!
复制代码

123.zip

422 Bytes, 下载次数: 8

发表于 2017-2-11 12:25 | 显示全部楼层    本楼为最佳答案   
  1. Sub tst()
  2. Application.ScreenUpdating = False
  3.     On Error Resume Next
  4.     ipath = ThisWorkbook.Path & ""
  5.     ifile = ipath & "原数据.txt"
  6.     Open ifile For Input As #1
  7.         Do While Not EOF(1)
  8.             Line Input #1, itxt
  9.             s1 = s1 & itxt & vbTab
  10.         Loop
  11.         [A1].Resize(UBound(Split(s1, vbTab)), 1) = Application.Transpose(Split(s1, vbTab))
  12.     Close #1
  13.     jfile = ipath & "替换数据.txt"
  14.     Open jfile For Input As #2
  15.         Do While Not EOF(2)
  16.             Line Input #2, itxt
  17.             s2 = s2 & itxt & vbTab
  18.         Loop
  19.         [B1].Resize(UBound(Split(s2, vbTab)), 1) = Application.Transpose(Split(s2, vbTab))
  20.     Close #2
  21.     a = Range("A1:A" & [A1].End(4).Row)
  22.     b = Range("B1:B" & [B1].End(4).Row)
  23.     If UBound(b) < UBound(a) / 2 Then MsgBox "待替换的文本个数不够": Exit Sub
  24.     For i = 1 To UBound(a)
  25.         If i Mod 2 = 0 Then
  26.             a(i, 1) = Replace(a(i, 1), "123456", b(i / 2, 1))
  27.         End If
  28.     Next
  29.     kfile = ipath & "替换ok.txt"
  30.     Open kfile For Output As #1
  31.     For i = 1 To UBound(a)
  32.         Print #1, a(i, 1)
  33.     Next
  34.     Close #1
  35.     Range("A1:B" & Application.Max(UBound(a), UBound(b))).ClearContents
  36.     MsgBox "替换OK!"
  37. Application.ScreenUpdating = True
  38. End Sub

  39. 请测试!
复制代码

123(ok).rar

16.6 KB, 下载次数: 2

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-29 23:28 , Processed in 0.784458 second(s), 7 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表