我是Excel编程的新手,正在制作我的第一个程序。我在调用函数中使用变量时遇到问题(我认为它们被称为函数!),问题是我不认为变量在我的函数中被扩展,有人可以请帮助我解决它。
Sub HyperLinkReplace()
Dim MyPath As String
MyPath = Environ("UserProfile") & "\Games\"
'MsgBox "MyPath: value is " & MyPath
Call ReplaceHyperlinkURL("C:\Users\Kids\Games\", "& MyPath &")
End Sub
Public Sub ReplaceHyperlinkURL(FindString As String, ReplaceString As String)
Dim LinkURL, PreStr, PostStr, NewURL As String
Dim FindPos, ReplaceLen, URLLen As Integer
Dim MyDoc As Worksheet
Dim MyCell As Range
On Error GoTo ErrHandler
Set MyDoc = ActiveSheet
For Each MyCell In MyDoc.UsedRange
If MyCell.Hyperlinks.Count > 0 Then
LinkURL = MyCell(1).Hyperlinks(1).Address
FindPos = InStr(1, LinkURL, FindString)
If FindPos > 0 Then 'If FindString is found
ReplaceLen = Len(FindString)
URLLen = Len(LinkURL)
PreStr = Mid(LinkURL, 1, FindPos - 1)
PostStr = Mid(LinkURL, FindPos + ReplaceLen, URLLen)
NewURL = PreStr & ReplaceString & PostStr
MyCell(1).Hyperlinks(1).Address = NewURL 'Change the URL
End If
End If
Next MyCell
Exit Sub
ErrHandler:
MsgBox ("ReplaceHyperlinkURL error")
End Sub
上面我做错了什么?
由于
答案 0 :(得分:1)
将Call ReplaceHyperlinkURL("C:\Users\Kids\Games\", "& MyPath &")
更改为
Call ReplaceHyperlinkURL("C:\Users\Kids\Games\", MyPath)
MyPath
已经是一个变量,用引号括起来使它成为一个字符串文字。