简体   繁体   English

在excel VBA中使用索引匹配

[英]Using Index Match In excel VBA

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

I am trying to use Index Match ,but I am getting #Value!我正在尝试使用索引匹配,但我得到了 #Value! error.错误。 Please note that it works absolutely fine if i manually enter the index match formula in the cell.请注意,如果我在单元格中手动输入索引匹配公式,它绝对可以正常工作。 This error appears only when i try to replicate it in excel vba.To give a brief,仅当我尝试在 excel vba 中复制它时才会出现此错误。简而言之,

Bucket_rng column contains a coulmn of multiple values of Bucket_rng 列包含一个 column 的多个值

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

Shortfall_rng column contains a coulmn of multiple values like Shortfall_rng 列包含一个 column 多个值,如

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

Datevalues_rng column contains a coulmn of multiple values in yyyy-mm-dd format Datevalues_rng 列包含 yyyy-mm-dd 格式的多个值

and

Region_rng column contains a coulmn of multiple values like Region_rng 列包含一个 column 多个值,如

China
New York
Australia
Japan Continent

You'd have to enclose the string arguments in quotes inside the formula.您必须将字符串参数括在公式内的引号中。 It's easier, IMO, to use the cell addresses of the criteria cells too: IMO,使用标准单元格的单元格地址也更容易:

Dim ws                    As Worksheet
Dim DateValues            As Range
Dim Shortfall             As Range
Dim Region                As Range
Dim Bucket                As Range
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 ws = Worksheets("Asia_All")

Set StartCopying = ws.Range("O4")

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



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


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

StartCopying.Value = estring

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

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