简体   繁体   中英

VBA, Updating link to other workbook in formula daily

I am very new to VBA programming and have an issue.

I have a base file called liquidity forecast in which I fill data from other documents in. The documents I collect data from is updated every day so the date in the name changes every day.

The Name goes "(Date, "YYMMDD") & "SE_Laizy.xlsx" so an example would be, 160229SE_Laizy.xlsx

When I collect data I use Index match formula. My problem is trying to update the link within the formula by using a date value.

Currently I write it like this,

ActiveCell.Range((Cells(1, 1)), (Cells(1, 1))).FormulaR1C1 = _
    "=INDEX('[" & Format(Date, "YYMMDD") &  "SE_Laizy.xlsx"]Visa'!R1:R1048576,MATCH(R2C,'[" & Format(Date, "YYMMDD") & "SE_Laizy.xlsx"]Visa'!C1,0),MATCH(""Ub perioden"",'[" & Format(Date, "YYMMDD") & "SE_Laizy.xlsx"]Visa'!R2,0))"

All I get from this is a NA. Any help would be appreciated!

I've added a parent worksheet reference and broken the INDEX and MATCH functions into the three primary sections.

With Worksheets("Sheet2")
    .Cells(1, 1).FormulaR1C1 = _
        "=INDEX('[" & Format(Date, "YYMMDD") & "SE_Laizy.xlsx]Visa'!C1:C16384, " & _
            "MATCH(R2C,'[" & Format(Date, "YYMMDD") & "SE_Laizy.xlsx]Visa'!C1, 0), " & _
            "MATCH(""Ub perioden"", '[" & Format(Date, "YYMMDD") & "SE_Laizy.xlsx]Visa'!R2, 0))"
End With

As noted, there were some misplaced quotes in the external workbook name. I'm not sure what to do with your Range object definition. For all intents and purposes, the one supplied simply resolves down to [A1].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM