0
저는 Excel 프로그래밍을 처음 사용하고 제 첫 프로그램을 만들고 있습니다. 호출 함수에서 변수를 사용하는 데 문제가 있습니다 (함수라고 생각합니다!). 문제는 변수가 함수에서 확장되고 있다고 생각하지 않는다는 것입니다. 누군가 해결할 수 있도록 도와 주시겠습니까?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
위의 내용 중 내가 잘못 했습니까?
감사
감사합니다. 완벽했습니다. – Dan
다른 포인터 : 'Dim LinkURL, PreStr, PostStr, NewURL As String'과 같은 변수를 선언 할 때 마지막 문자열 만 문자열이됩니다. 다른 문자열은 아무 것도 쓰지 않으면 기본적으로'Variant '로 선언됩니다. –
@PatrickLepelletier, 잘 발견되었습니다. +1 – cyboashu