简体   繁体   English

Excel VBA 动态数组溢出仅在宏结束后完成

[英]Excel VBA Dynamic Array spill only complete after end of macro

My Dataset contains several transactions in March 2020 in foreign currency.我的数据集包含 2020 年 3 月的几笔外币交易。 Column A contains the date of each transaction, while column B contains the transaction value in foreign currency (EUR). A 列包含每笔交易的日期,而 B 列包含外币 (EUR) 的交易价值。

I want to calculate the value of each transaction in local currency (being ZAR) in column C using VBA.我想使用 VBA 在 C 列中以当地货币(即 ZAR)计算每笔交易的价值。

Firstly, I obtain all the exchange rates for March 2020 using the STOCKHISTORY function:首先,我使用 STOCKHISTORY function 获取 2020 年 3 月的所有汇率:

Dim exchangeRateSummary As Range

Range("I1").Formula2R1C1 = "=STOCKHISTORY(R1C5,R1C6,R1C7)"  'where R1C5 contains EUR/ZAR, R1C6 contains 01-Mar-20, and R1C7 contains 31-Mar-20
Set exchangeRateSummary = Range("I1").CurrentRegion  'save range as variable

Then I add a VLookup in column C to determine the local currency value of each transaction, by looping through each row:然后我在 C 列中添加一个 VLookup 来确定每笔交易的当地货币价值,方法是遍历每一行:

Dim j As Integer

For j = 2 To 100
cells(j,3).value=cells(j,2).value * Application.WorksheetFunction.VLookup(Cells(j,1),exchangeRateSummary,2,False)
Next j

When I run the code, I get the following error: "Unable to get the VLookup property of the WorksheetFunction class."运行代码时,出现以下错误:“无法获取 WorksheetFunction class 的 VLookup 属性。” When I press End, the spill range of exchange rates (created with the stockhistory function) appears on my Excel sheet.当我按下 End 时,汇率的溢出范围(使用 stockhistory 函数创建)出现在我的 Excel 表上。 When I run the code again, the VLookup works, and provides the correct local currency values in column C.当我再次运行代码时,VLookup 工作,并在 C 列中提供正确的本地货币值。

The problem is therefore that the VLookup does not work within that same macro, as the spill range created by the stock history function only appears after the macro was executed.因此,问题在于 VLookup 在同一个宏中不起作用,因为股票历史 function 创建的溢出范围仅在宏执行后出现。

One option is to make these codes separate macros, but I would like to automate this process in one click.一种选择是使这些代码分开宏,但我想一键自动执行此过程。

Is there a way I can instruct the macro to first calculate and spill that range, before continuing with the VLookup to that range?有没有一种方法可以指示宏首先计算并溢出该范围,然后继续 VLookup 到该范围?

Your question is not clear and you should try to improve that.您的问题不清楚,您应该尝试改进它。 That said, with what youve provided, I think you may be able to do something by forcing calculation in your function where you call the vlookup .也就是说,根据您提供的内容,我认为您可以通过在您调用vlookup的 function 中强制计算来做一些事情。 My guess is that simply wrapping the vlookup might be good enough without having to include the Workshet.Calculate call but you'll have to experiment yourself unless you can provide more info in your question.我的猜测是,简单地包装 vlookup 可能就足够了,而不必包含Workshet.Calculate调用,但是除非您可以在问题中提供更多信息,否则您必须自己进行试验。

so something like: in your worksheet eg stock field in $A$1 , start date in $A$2 call STOCKHISTORY from B1像这样:在您的工作表中,例如 $A$1 中的股票字段, $A$1 $A$2的开始日期从 B1 调用 STOCKHISTORY

in Cell B1
=STOCKHISTORY(A1, A2)

Then call a function that wraps around vlookup, triggered by a change in $B$1 , passing your lookup value (say "31-Jan-2020").然后调用一个围绕 vlookup 的 function,由$B$1的变化触发,传递您的查找值(例如“2020 年 1 月 31 日”)。

=vlookup_wrapper("31-Jan-2020", B1)

and your wrapper looks something like this你的包装看起来像这样

Option Explicit

Function vlookup_wrapper(ByVal lookup_val As Variant, ByVal spill_formula) As Variant
  'parameter spill_formula never used, only to trigger this function to be called
  Dim ws As Worksheet
  Set ws = ThisWorkbook.Sheets("sheet_with_stockhistory_formula")
  ' force stock history to finish its SPILL
  ws.Calculate

  'Define range from output of STOCKHISTORY spill
  Dim r_in As Range
  Set r_in = ws.Range(ws.Range("B1"), ws.Range("B1").End(xlDown))

  ' Assume want column 2 back
  vlookup_wrapper = Application.WorksheetFunction.VLookup(lookup_val, r_in, 2, False)
  
End Function

Spill range reference via #通过#溢出范围参考

Referring to a spill range can be done via the "#" suffix, ie H2# if the top cell containing the dynamic STOCKHISTORY formula resides eg in H2 .如果包含动态 STOCKHISTORY 公式的顶部单元格位于例如H2中,则可以通过“#”后缀引用溢出范围,即H2# Couldn't test, but VBA should do this job.无法测试,但 VBA 应该可以完成这项工作。

So a possible invented lookup result might be received eg via因此,可能会收到一个可能发明的查找结果,例如通过

Dim result: result = [VLOOKUP(100,H2#,2,FALSE)]

or或者

result = Evaluate("VLookup(" & mySearch & "," & "H2#" & ",2,False")


 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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