Excel VBA可变外部链接公式

时间:2018-04-17 20:20:55

标签: excel-vba vba excel

我无法使用VBA公式。我试图将SalesEast文件中的单元格Q8(后来更改为East Workbook)引入SalesAnalysis工作簿(后来更改为Analysis Workbook)。我不认为可以使用INDIRECT,因为分析工作簿将在没有打开East Workbook的情况下打开。此宏将每月运行,因此文件名将每月更改。任何帮助将不胜感激。

Sub FixSales()

Dim OpenPath As String
Dim SalesAnalysis As String
Dim SalesSupport As String
Dim SalesEast As String
Dim SalesWest As String
Dim Analysis As Workbook
Dim East As Workbook
Dim West As Workbook
Dim Support As Workbook

OpenPath = "F:\budget\Expense Analysis\2018\2018_Q1\"
SalesAnalysis = "Sales Analysis " & Format(DateSerial(Year(Now), Month(Now), 
0), "MM_YY") & ".xlsx"
SalesEast = "Sales East " & Format(DateSerial(Year(Now), Month(Now), 0), 
"MM_YY") & ".xlsx"
SalesWest = "Sales West " & Format(DateSerial(Year(Now), Month(Now), 0), 
"MM_YY") & ".xlsx"
SalesSupport = "Sales Support " & Format(DateSerial(Year(Now), Month(Now), 
0), "MM_YY") & ".xlsx"

Workbooks.Open FileName:=OpenPath & SalesAnalysis
Set Analysis = ActiveWorkbook
Workbooks.Open FileName:=OpenPath & SalesSupport
Set Support = ActiveWorkbook
Workbooks.Open FileName:=OpenPath & SalesEast
Set East = ActiveWorkbook
Workbooks.Open FileName:=OpenPath & SalesWest
Set West = ActiveWorkbook

Analysis.Activate
Sheets("Prior YTD to Curr YTD").Select
Range("C8").Select
ActiveCell.Formula = "= ""sum('"" & OpenPath & ""["" & SalesEast & ""]"" & 
""Prior YTD to Curr YTD'!Q8)""'"

End Sub

感谢。

1 个答案:

答案 0 :(得分:0)

该公式仅引用另一个工作簿中的单元格,因此无需求和。单引号也在错误的位置,并且有太多双引号。

试试这个:

Range("C8").Formula = "='" & OpenPath & "[" & SalesEast & "]Prior YTD to Curr YTD'!Q8)"