|
楼主 |
发表于 2023-7-7 21:38
|
显示全部楼层
谢谢老师!
我把您和解0人老师的代码,如下这么合并,还能再简化一下吗?
- Sub test()
- '第一步,先写入到id 1 'by excelhome论坛 解0人
- Dim str$
- Dim Myr%, arr
- Dim i&, s$, s1$, s2$, reg
- Open ThisWorkbook.Path & "/111A.TXT" For Binary As #1
- i = LOF(1)
- str = Input(i, 1)
- Close #1
- With Sheet3
- Myr = .Range("a65536").End(xlUp).Row
- arr = .Range("a2:b" & Myr)
-
- For i = 1 To UBound(arr)
- s = arr(i, 1)
- s1 = s1 & "<stk setcode=""" & d & """ code=""" & s & """/>"
- Next
- End With
- Set reg = CreateObject("VBScript.RegExp")
- With reg
- .MultiLine = True
- .Pattern = "(<cell id=""1"" .+?>\s*)(<.*?>)?(\s*<\/cell>)"
- str = .Replace(str, "$1" & s1 & "$3")
- End With
- Set reg = Nothing
- Open ThisWorkbook.Path & "/111C.txt" For Output As #1 '---------------------暂存到111C.txt,下面的第二步再进行修改
- Print #1, str
- Close #1
- '第二步,再根据不同的id进行改名,by excelhome论坛 wanghan519 EH高级
- Dim xml As Object
- Dim node As Object
- Dim nd As Object
- Set xml = CreateObject("MSXML2.DOMDocument")
- xml.Load ThisWorkbook.Path & "\111C.txt" '-----------------------------再从111C.txt进行修改
- For Each node In xml.SelectNodes("//cell")
- Select Case node.getAttribute("id")
- Case "1"
- node.SetAttribute "text", "北京"
- Case "4"
- node.SetAttribute "text", "南京"
- Case "2"
- node.SetAttribute "text", "哈哈"
- For Each nd In node.SelectNodes("stk")
- node.RemoveChild nd
- Next
- Case "6"
- node.SetAttribute "text", "嘻嘻"
- For Each nd In node.SelectNodes("stk")
- node.RemoveChild nd
- Next
- End Select
- Next
- xml.Save ThisWorkbook.Path & "\111C.txt" '-----------------------------定稿于111C.txt
- End Sub
复制代码 |
|