更新工作簿中的四个链接

时间:2015-10-28 17:24:52

标签: excel vba excel-vba

我正在尝试更新我创建的工作簿中的四个链接。我使用我在网上找到的代码将以下代码拼凑在一起。我尝试替换的链接具有基于日期和状态的动态文件名。我希望excel命令链接名称的方式与编辑链接窗口中的命名方式相同。看来情况并非如此。

我遇到的问题是我打算将varlink(1)作为替换varlink(4)的链接替换。无论如何要确保我用“丢失”链接等替换“丢失”链接?

open FsCheck.Xunit
open Swensen.Unquote

[<Property>]
let ``Add5AndMap returns composed result`` (number : int) =
    let actual = InteropGuyFactory().CreateInteropGuy().Add5AndMap number

    let expected = number |> add5 |> mapResult
    expected =! actual

1 个答案:

答案 0 :(得分:2)

如果您遍历每个链接并使用Select Case确定您正在使用的链接,则可以确定要更改的正确链接。

根据您在OP中的内容,查看我修改过的以下代码。

Sub UpDateLinks()

Dim Date1 As String
Dim StateAbbrev As String, sLink As String, sNewName as String
Dim varLinks As Variant
Dim i As Integer
Dim ws As Worksheet

Set ws = Sheets("Inputs")

With ws

    StateAbbrev = .Range("StateAbbrev")
    Date1 = .Range("AD1")

End With

varLinks = ActiveWorkbook.LinkSources(xlExcelLinks)

For i = 1 To UBound(varLinks)

    Dim x As Integer
    If InStr(1, varLinks(i), "Loss Trends") Then sLink = "Loss Trends"
    If InStr(1, varLinks(i), "Prem Trends") Then sLink = "Prem Trends"
    If InStr(1, varLinks(i), "Fast Track Loss Trends") Then sLink = "Fast Track Loss Trends"
    If InStr(1, varLinks(i), "Section A") Then sLink = "Section A"

    sNewName = "F:\MyHouse\" & Date1 & "\" & StateAbbrev & "\Home\" & StateAbbrev & " " & sLink & " " & Date1 & ".xlsm"

    If sLink = "Section A" Then sNewName = Replace(sNewName,".xlsm","-Revised.xlsm")         

    ActiveWorkbook.ChangeLink _
            Name:=varLinks(i), NewName:=sNewName, Type:=xlExcelLInks

Next

End Sub