有没有办法检查excel中的数据链接状态?
我的代码更改了数据链接,但没有检查以确保链接实际指向有用的内容。
Dim rCountryOld As Range
Dim rCountryNew As Range
Dim vLink() As Variant
Dim sDefPath As String
Dim i As Integer
With ThisWorkbook
Set rCountryOld = .Names("CountryLink").RefersToRange
Set rCountryNew = .Names("Country").RefersToRange
sDefPath = Left(.Names("FileList").RefersToRange.Cells(1, 2).Value, _
InStrRev(.Names("FileList").RefersToRange.Cells(1, 2).Value, "\") - 1)
'check for a change in country
If rCountryOld <> rCountryNew Then
vLink = .LinkSources
For i = LBound(vLink) To UBound(vLink) 'if changed, find the country datasheet link
If UCase(Left(vLink(i), InStrRev(vLink(i), "\") - 1)) = UCase(sDefPath) Then
Exit For
End If
Next i
.ChangeLink Name:=vLink(i), NewName:=WorksheetFunction.VLookup(rCountryNew, .Names("FileList").RefersToRange, 2, False), _
Type:=xlExcelLinks 'change to the new country's datasheet
rCountryOld.Value = rCountryNew.Value
End If
End With
感谢您的帮助!