在excel VBA中使用索引匹配

时间:2015-07-29 13:24:36

标签: vba excel-vba excel

Dim DateValues As Variant
Dim Shortfall As Variant
Dim Region As Variant
Dim Bucket As Variant

Dim Shortfall_rng As Range
Dim Region_rng As Range
Dim Bucket_rng As Range
Dim Datevalues_rng As Range

Dim estring As Variant

Dim StartCopying As Range


Set StartCopying = Worksheets("Asia_All").Range("O4")

Region = Worksheets("Asia_All").Range("O1").Value ' Value in cell O1 is "New York" (without Quotes)
Bucket = Worksheets("Asia_All").Range("O2").Value ' Value in cell O2 is "<!--1-->0 - 1" (without Quotes)
DateValues = Worksheets("Asia_All").Range("O3").Value ' Value in cell O3 is "2007-01-02" (without Quotes)



Set Shortfall_rng = Worksheets("Asia_All").Range("$B$2:$B$142411")
Set Bucket_rng = Worksheets("Asia_All").Range("$F$2:$F$142411")
Set Datevalues_rng = Worksheets("Asia_All").Range("J$2:$J$142411")
Set Region_rng = Worksheets("Asia_All").Range("$K$2:$K$142411")


estring = Evaluate("Index(" & Shortfall_rng.Address & ", Match(" & Bucket & Region & DateValues & ", " & Bucket_rng.Address & " & " & Region_rng.Address & "&" & Datevalues_rng.Address & ", 0))")

StartCopying.Value = estring

我正在尝试使用索引匹配,但我得到了#Value!错误。请注意,如果我在单元格中手动输入索引匹配公式,它可以正常工作。只有当我尝试在excel vba中复制它时才会出现此错误。要简要说明,

Bucket_rng列包含多个值为

的库
 <!--1-->0 - 1
 <!--2-->1 - 2.5
 <!--3-->2.5 - 5
 <!--4-->5 - 10
 <!--5-->10+

Shortfall_rng列包含多个值的库,例如

-4.69
-3.94
-53.94
150.18
-18.78
-9.98
-17.04
-41.32
0.0001

Datevalues_rng列包含yyyy-mm-dd格式的多个值的coulmn

Region_rng列包含多个值的库,例如

China
New York
Australia
Japan Continent

1 个答案:

答案 0 :(得分:1)

您必须将字符串参数括在公式内的引号中。 IMO也更容易使用标准单元格的单元格地址:

ShowDialog()