使用下面的代码,我试图将锚标记/超链接添加到html文件中的数字。虽然我可以在“本地窗口”中看到正确的值,但文件中未更新相同的值。 不知道怎么了。
Sub HyperlinkPRs() '''https://stackoverflow.com/questions/10434335/text-file-in-vba-open-find-replace-saveas-close-file
Dim rng As Range
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
Dim var As String
lr = Worksheets("RawData").Cells(Rows.Count, 7).End(xlUp).Row
Set rng = Sheets("RawData").Range("G2:G" & lr)
' Edit as needed
sFileName = ThisWorkbook.Path & "\" & "data.html"
iFileNum = FreeFile
Open sFileName For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum
''' Replace code
For i = 1 To lr
pr = Sheets("RawData").Range("G" & i).Value
link = "<a href=""" & _
"www.xyz.com/cgi-binr.pl?entry=" & _
pr & _
""">" & _
pr & _
"</a>" & "</td>"
sTemp = Replace(sTemp, pr & "</td>", link)
Next
iFileNum = FreeFile
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
End Sub
本地窗口中的值(正确):
pr: 9525027
link = <a href="www.xyz.com/cgi-binr.pl?entry=9525027">9525027</a></td>
替换为:
9525027<a href="www.xyz.com/cgi-binr.pl?entry="></a></td>
在文本文件中输入:
<td class=xl6516703 style='border-top:none;border-left:none'> </td>
<td class=xl6516703 style='border-top:none;border-left:none'> </td>
<td class=xl6516703 style='border-top:none;border-left:none'> </td>
<td class=xl6516703 style='border-top:none;border-left:none'> </td>
<td class=xl6516703 style='border-top:none;border-left:none'>14</td>
<td class=xl6516703 style='border-top:none;border-left:none'>24</td>
<td class=xl7616703 style='border-top:none;border-left:none'>9525027</td>
输出:
<td class=xl6516703 style='border-top:none;border-left:none'> <a
href="www.xyz.com_pr.pl?entry="></a></td>
<td class=xl6516703 style='border-top:none;border-left:none'> <a href="www.xyz.com_pr.pl?entry="></a></td>
<td class=xl6516703 style='border-top:none;border-left:none'> <a href="www.xyz.com_pr.pl?entry="></a></td>
<td class=xl6516703 style='border-top:none;border-left:none'> <a href="www.xyz.com_pr.pl?entry="></a></td>
<td class=xl6516703 style='border-top:none;border-left:none'>14<a href="www.xyz.com_pr.pl?entry="></a></td>
<td class=xl6516703 style='border-top:none;border-left:none'>24<a href="www.xyz.com_pr.pl?entry="></a></td>
<td class=xl7616703 style='border-top:none;border-left:none'>9525027<a href="www.xyz.com_pr.pl?entry="></a></td>
答案 0 :(得分:1)
这是您发布的(勉强)修改的例程。使用您的输入数据样本,我将您期望的输出生成到临时文件中。注意检查以确定HTML中是否存在旧标记子字符串。另外,我的输入数据仅包含三个数字标签。我没有使用 </td>
修改标签。
Sub HyperlinkPRs()
Dim rng As Range
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
Dim var As String
lr = Worksheets("RawData").Cells(Rows.Count, 7).End(xlUp).Row
Set rng = Sheets("RawData").Range("G2:G" & lr)
' Edit as needed
'sFileName = ThisWorkbook.Path & "\" & "data.html"
sFileName = "C:\Temp\test1.html"
iFileNum = FreeFile
Open sFileName For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum
Dim oldTag As String
Dim newLink As String
For i = 1 To lr
pr = Sheets("RawData").Range("G" & i).Value
oldTag = pr & "</td>"
If InStr(1, sTemp, oldTag) > 0 Then
newLink = "<a href=""" & _
"www.xyz.com/cgi-binr.pl?entry=" & _
pr & _
""">" & _
pr & _
"</a>" & "</td>"
sTemp = Replace(sTemp, oldTag, newLink)
Debug.Print "replaced " & oldTag
End If
Next
sFileName = "C:\Temp\test1out.html" 'temp file for debug
iFileNum = FreeFile
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
End Sub
输入数据(C:\ Temp \ test1.html):
<td class=xl6516703 style='border-top:none;border-left:none'> </td>
<td class=xl6516703 style='border-top:none;border-left:none'> </td>
<td class=xl6516703 style='border-top:none;border-left:none'> </td>
<td class=xl6516703 style='border-top:none;border-left:none'> </td>
<td class=xl6516703 style='border-top:none;border-left:none'>14</td>
<td class=xl6516703 style='border-top:none;border-left:none'>24</td>
<td class=xl7616703 style='border-top:none;border-left:none'>9525027</td>
由代码生成的输出数据(C:\ Temp \ test1out.html):
<td class=xl6516703 style='border-top:none;border-left:none'> </td>
<td class=xl6516703 style='border-top:none;border-left:none'> </td>
<td class=xl6516703 style='border-top:none;border-left:none'> </td>
<td class=xl6516703 style='border-top:none;border-left:none'> </td>
<td class=xl6516703 style='border-top:none;border-left:none'><a href="www.xyz.com/cgi-binr.pl?entry=14">14</a></td>
<td class=xl6516703 style='border-top:none;border-left:none'><a href="www.xyz.com/cgi-binr.pl?entry=24">24</a></td>
<td class=xl7616703 style='border-top:none;border-left:none'><a href="www.xyz.com/cgi-binr.pl?entry=9525027">9525027</a></td>